Spring Boot/Security - 在嵌入式 Tomcat 上运行时无法自定义安全性

标签 spring spring-security spring-boot embedded-tomcat-7

我有一个 Spring Boot 应用程序,该应用程序无法在嵌入式 tomcat 上运行,但在托管 tomcat 安装上运行良好。该设置在 Spring Boot 1.0.0.RELEASE 之前有效,但我无法确定发生了什么变化。

本质上发生的事情是我的应用程序没有“ServletContext”的概念,因此某些部分在加载时会爆炸。我看到其他一些帖子提到实例化配置的顺序(特别是安全性)很重要,但我似乎无法解决这个问题。从 SecurityConfig.java 中删除 @Configuration 注释可以加载应用程序,但随后我无法根据需要自定义安全性。此外,向该类添加 @Order 没有任何效果。

整个项目托管here ,相关部分为 pom.xml , DemoApplication.java & SecurityConfig.java

如有任何帮助,我们将不胜感激

堆栈跟踪:

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'defaultServletHandlerMapping' defined in class org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public org.springframework.web.servlet.HandlerMapping org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport.defaultServletHandlerMapping()] threw exception; nested exception is java.lang.IllegalArgumentException: A ServletContext is required to configure default servlet handling
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:597)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1094)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:989)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:504)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:703)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482)
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:120)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:683)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:313)
    at org.springframework.boot.builder.SpringApplicationBuilder.run(SpringApplicationBuilder.java:142)
    at com.github.lemniscate.stack.boot.DemoApplication.main(DemoApplication.java:64)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public org.springframework.web.servlet.HandlerMapping org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport.defaultServletHandlerMapping()] threw exception; nested exception is java.lang.IllegalArgumentException: A ServletContext is required to configure default servlet handling
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:188)
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:586)
    ... 21 more
Caused by: java.lang.IllegalArgumentException: A ServletContext is required to configure default servlet handling
    at org.springframework.util.Assert.notNull(Assert.java:112)
    at org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer.<init>(DefaultServletHandlerConfigurer.java:54)
    at org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport.defaultServletHandlerMapping(WebMvcConfigurationSupport.java:346)
    at org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration$$EnhancerBySpringCGLIB$$d9a689bd.CGLIB$defaultServletHandlerMapping$23(<generated>)
    at org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration$$EnhancerBySpringCGLIB$$d9a689bd$$FastClassBySpringCGLIB$$aa3e36b5.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228)
    at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:312)
    at org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration$$EnhancerBySpringCGLIB$$d9a689bd.defaultServletHandlerMapping(<generated>)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:166)
    ... 22 more

最佳答案

我们在一次聊天中找到了 @EnableGlobalMethodSecurity 和使用 JPA 来实现 UserDetails 的组合(两者都可以单独工作)。有一个 Spring Security 问题开放来跟踪至少部分内容:https://jira.spring.io/browse/SEC-2661 .

解决方法:不要使用 @EnableGlobalMethodSecurity 或不要将 JPA 用于 UserDetails,或者可能使 UserDetailsS​​ervice 在使用时延迟初始化,而不是注入(inject)身份验证构建器时。

关于Spring Boot/Security - 在嵌入式 Tomcat 上运行时无法自定义安全性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24598959/

相关文章:

spring-mvc - 如何在SpringBootTest中向自动连线的testRestTemplate添加基本身份验证; Spring Boot 1.4

java - Spring 本地和系统属性

java - 使用 Spring 动态加载属性文件

java - Spring security 复制了 RemeberMeProvider

java - 当在 hibernate 中创建多个子实体时,如何仅创建一次父实体?

spring-boot - Spring Boot 2 war 文件尝试加载 el-api v3.0 类 (NoClassDefFoundError : javax/el/ELManager) on tomcat 7 (el-api 2. 2)

java - Spring 框架: is possible to create two beans of the same @Component without @Configuration?

java - JdbcSQL异常 : Unique index or primary key violation

java - Spring Security - AD 服务器离线时无法捕获 LDAP 异常

spring - 配置 Spring Security 5 Oauth 2 以使用 access_token uri 参数