从1.4.2更新到Shiro 1.6.0,导致java.lang.IllegalStateException:未设置ServletContext

问题描述

在Spring Boot 2.3.3。上运行。尝试将Shiro从1.4.2更新到1.5.3,然后更新到1.6.0,并在启动时获取以下堆栈跟踪(Eclipse中的嵌入式服务器)

43:14.333 [main] ERROR o.s.boot.SpringApplication - Application run Failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'resourceHandlerMapping' defined in class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]: Bean instantiation via factory method Failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.servlet.HandlerMapping]: Factory method 'resourceHandlerMapping' threw exception; nested exception is java.lang.IllegalStateException: No ServletContext set
    at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:655)
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:635)
    at org.springframework.beans.factory.support.AbstractAutowireCapablebeanfactory.instantiateUsingFactoryMethod(AbstractAutowireCapablebeanfactory.java:1336)
    at org.springframework.beans.factory.support.AbstractAutowireCapablebeanfactory.createBeanInstance(AbstractAutowireCapablebeanfactory.java:1176)
    at org.springframework.beans.factory.support.AbstractAutowireCapablebeanfactory.doCreateBean(AbstractAutowireCapablebeanfactory.java:556)
    at org.springframework.beans.factory.support.AbstractAutowireCapablebeanfactory.createBean(AbstractAutowireCapablebeanfactory.java:516)
    at org.springframework.beans.factory.support.Abstractbeanfactory.lambda$doGetBean$0(Abstractbeanfactory.java:324)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:226)
    at org.springframework.beans.factory.support.Abstractbeanfactory.doGetBean(Abstractbeanfactory.java:322)
    at org.springframework.beans.factory.support.Abstractbeanfactory.getBean(Abstractbeanfactory.java:202)
    at org.springframework.beans.factory.support.DefaultListablebeanfactory.preInstantiateSingletons(DefaultListablebeanfactory.java:897)
    at org.springframework.context.support.AbstractApplicationContext.finishbeanfactoryInitialization(AbstractApplicationContext.java:879)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:551)
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:143)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:758)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:750)
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:315)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1237)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226)

Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.servlet.HandlerMapping]: Factory method 'resourceHandlerMapping' threw exception; nested exception is java.lang.IllegalStateException: No ServletContext set
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185)
    at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:650)
    ... 20 common frames omitted
Caused by: java.lang.IllegalStateException: No ServletContext set
    at org.springframework.util.Assert.state(Assert.java:76)
    at org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport.resourceHandlerMapping(WebMvcConfigurationSupport.java:534)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(UnkNown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(UnkNown Source)
    at java.lang.reflect.Method.invoke(UnkNown Source)
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154)
    ... 21 common frames omitted

Shiro 1.4.2上的任何问题... GitHub上的发行说明尚未更新,因此不确定是什么原因导致了最新版本中的问题。

Shiro依赖项:

<shiro.version>1.6.0</shiro.version>
    <dependency>
        <groupId>org.apache.shiro</groupId>
        <artifactId>shiro-spring-boot-web-starter</artifactId>
        <version>${shiro.version}</version>
    </dependency>
    
    <dependency>
        <groupId>com.github.theborakompanioni</groupId>
        <artifactId>thymeleaf-extras-shiro</artifactId>
        <version>2.0.0</version>
    </dependency>

配置/组件:

@Configuration
public class ShiroConfig {

private static String LOGGING_PREFIX = "[ShiroConfig] ";
protected static final Logger logger = LoggerFactory.getLogger(ShiroConfig.class);

@Bean
public ShiroFilterChainDeFinition shiroFilterChainDeFinition() {
    DefaultShiroFilterChainDeFinition chainDeFinition = new DefaultShiroFilterChainDeFinition();
    chainDeFinition.addpathDeFinition("/css/**","anon");
    chainDeFinition.addpathDeFinition("/js/**","anon");
    chainDeFinition.addpathDeFinition("/images/**","anon");
    chainDeFinition.addpathDeFinition("/signin.html","authc");
    chainDeFinition.addpathDeFinition("/secure/actuator/health","anon");
    chainDeFinition.addpathDeFinition("/secure/nomination/**","authc,roles[nominator]");
    chainDeFinition.addpathDeFinition("/secure/review/**",anyRoles[reviewer,nationalreviewer]");
    chainDeFinition.addpathDeFinition("/secure/actuator/**",roles[sysadmin]");
    chainDeFinition.addpathDeFinition("/secure/sysadmin/**",roles[sysadmin]");
    chainDeFinition.addpathDeFinition("/secure/**","authc");

    return chainDeFinition;
}

@Bean(name = "hashService")
public HashService hashService() {
    DefaultHashService hashService = new DefaultHashService();
    hashService.setHashAlgorithmName("SHA-512");
    hashService.setHashIterations(50000);
    hashService.setPrivateSalt(new SimpleByteSource("xxxxxxxx"));
    return hashService;
}

@Bean(name = "passwordService")
public PasswordService passwordService() {
    DefaultPasswordService passwordService = new DefaultPasswordService();
    passwordService.setHashService(hashService());
    return passwordService;
}

@Bean(name = "passwordMatcher")
public PasswordMatcher passwordMatcher() {
    PasswordMatcher matcher = new PasswordMatcher();
    matcher.setPasswordService(passwordService());
    return matcher;
}

@Bean
public Realm realm() {
    Customrealm realm = new Customrealm();
    realm.setCredentialsMatcher(passwordMatcher());
    realm.setCachingEnabled(true);
    return realm;
}

@Bean(name = "authenticationListener")
public AuthenticationListener authenticationListener() {
    return new AuthenticationListenerImpl();
}

@Bean(name = "cacheManager")
public CacheManager cacheManager() {
    return new MemoryConstrainedCacheManager();
}

@Bean
@DependsOn(value = "lifecycleBeanPostProcessor")
public DefaultAdvisorAutoproxyCreator defaultAdvisorAutoproxyCreator() {
    DefaultAdvisorAutoproxyCreator proxy = new DefaultAdvisorAutoproxyCreator();
    proxy.setProxyTargetClass(true);
    return proxy;
}

@Bean
public Authenticator authenticator() {
    ModularRealmAuthenticator authenticator = new ModularRealmAuthenticator();
    List<AuthenticationListener> listeners = new ArrayList<AuthenticationListener>();
    listeners.add(authenticationListener());
    authenticator.setAuthenticationListeners(listeners);
    return authenticator;

}

@Bean
public Filter anyRoles() {
    return new AnyOfRolesAuthorizationFilter();
}

}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)