SpringBoot&x2B;基于方法的分层角色安全性:需要ServletContext

SpringBoot&x2B;基于方法的分层角色安全性:需要ServletContext,spring,spring-mvc,spring-boot,spring-security,Spring,Spring Mvc,Spring Boot,Spring Security,我添加了基于方法的安全性,并添加了角色层次结构。我在生成过程中不断遇到以下异常: org.springframework.beans.BeanInstantiationException:未能 实例化[org.springframework.web.servlet.HandlerMapping]:工厂 方法“defaultServletHandlerMapping”引发异常;嵌套 异常为java.lang.IllegalArgumentException:一个ServletContext是 需要

我添加了基于方法的安全性,并添加了角色层次结构。我在生成过程中不断遇到以下异常:

org.springframework.beans.BeanInstantiationException:未能 实例化[org.springframework.web.servlet.HandlerMapping]:工厂 方法“defaultServletHandlerMapping”引发异常;嵌套 异常为java.lang.IllegalArgumentException:一个ServletContext是 需要配置默认servlet处理

我尝试了很多不同的配置选项,但都不起作用。。。 下面是我的基本web安全配置类(如您所见,我添加了与角色层次结构相关的bean):

我为此挣扎了好几个小时。我尝试过的相关主题如下:

感谢您的帮助好的,找到了

我仔细研究了类注释,找到了一个简单的解决方案:我从GlobalMethodSecurityConfig中删除了
@EnableGlobalMethodSecurity
,并将其移动到了WebSecurityConfiguration,它在以前的位置。看起来是这样的:

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter { (...) }
这是:

@Configuration
public class GlobalMethodSecurityConfig extends GlobalMethodSecurityConfiguration { (...) }
最有趣的部分是,被接受的答案告诉我什么不适合我

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter { (...) }
@Configuration
public class GlobalMethodSecurityConfig extends GlobalMethodSecurityConfiguration { (...) }