【问题标题】:Spring Boot / Security - Cannot customize security when running on embedded TomcatSpring Boot / Security - 在嵌入式 Tomcat 上运行时无法自定义安全性
【发布时间】:2014-08-27 05:58:19
【问题描述】:

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

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

整个项目托管here,相关部分为pom.xmlDemoApplication.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

【问题讨论】:

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


【解决方案1】:

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

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

【讨论】:

  • 所以,我尝试拉出 UserDetails 对象,但没有成功——尽管 UserDetailsS​​ervice 实现仍然使用 Spring Data 存储库,但这应该没问题,对吧?还尝试了服务的延迟实例化,但没有骰子。删除 @EnableGlobalMethodSecurity 有效,但我不能使用 @Secured 注释。任何解决方法来启用这些另一种方式?没有尝试子类化GlobalMethodSecurityConfiguration,但我有一种感觉,我最终会在同一条船上......
  • GlobalMethodSecurityConfiguration 绝对是推荐的,但即使这样我也无法让你的代码工作。我可能会多花点时间。
  • 那太好了,谢谢。我能想到的唯一其他解决方案是自定义注释 + AOP 建议,这与你们的出色工作相比确实逊色。
  • 这是一个有趣的观察。如果您删除 SecurityProperties @Bean 您的应用程序将启动(不知道它应该做什么,所以我不知道它是否正常工作)。这令人惊讶,但可能并非不合理(@ConfigurationProperties 也必须很早就实例化)。由于您实际上并未使用SecurityProperties,因此将其删除(或将其放在单独的@Configuration 中)应该没有什么坏处。
  • 太棒了!当我得到一些以验证没有其他副作用时,我会尝试一下。非常感谢!
猜你喜欢
  • 1970-01-01
  • 2020-04-06
  • 2017-03-12
  • 2016-08-19
  • 1970-01-01
  • 2020-07-10
  • 2017-06-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多