SpringBoot的自动配置给我们开发带来了极大的便利,但有些时候也带来了一些问题。
问题场景:
该项目是基于Springboot + dubbo的微服务架构,框架结构web + facade + service,某个模块的facade引用了Spring Security的pom文件为了继承其中的接口,导致实现该facade接口的service启动时报springsecurity中XXXConfiguration中XXXMethod找不到;本意为实现springsecurity中相关接口被web层调用,wen层才是真正需要继承springsecurity,故该service需要排除springsecurity相关自动配置;
解决过程:
首先贴上报错信息:
***************************
APPLICATION FAILED TO START
***************************
Description:
Parameter 0 of method setAuthenticationConfiguration in org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter required a bean of type 'org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration' that could not be found.
Action:
Consider defining a bean of type 'org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration' in your configuration.
根据报错信息知道springboot在配置WebSecurityConfigurerAdapter时发生了错误,springboot还是很人性化的,启动时开启debug日志级别会打印所有AutoConfiguration信息, 搜索启动日志查找WebSecurityConfigurerAdapter;
2019-01-15 20:10:30.880 [main] DEBUG o.s.b.factory.support.DefaultListableBeanFactory - Finished creating instance of bean 'managementContextResolver'
2019-01-15 20:10:30.887 [main] DEBUG o.s.beans.factory.annotation.InjectionMetadata - Registered injected element on class [org.springframework.boot.actuate.autoconfigure.ManagementWebSecurityAutoConfiguration$ManagementWebSecurityConfigurerAdapter$$EnhancerBySpringCGLIB$$3db9a4ce]: AutowiredMethodElement for public void org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter.setApplicationContext(org.springframework.context.ApplicationContext)
2019-01-15 20:10:30.887 [main] DEBUG o.s.beans.factory.annotation.InjectionMetadata - Registered injected element on class [org.springframework.boot.actuate.autoconfigure.ManagementWebSecurityAutoConfiguration$ManagementWebSecurityConfigurerAdapter$$EnhancerBySpringCGLIB$$3db9a4ce]: AutowiredMethodElement for public void org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter.setTrustResolver(org.springframework.security.authentication.AuthenticationTrustResolver)
2019-01-15 20:10:30.888 [main] DEBUG o.s.beans.factory.annotation.InjectionMetadata - Registered injected element on class [org.springframework.boot.actuate.autoconfigure.ManagementWebSecurityAutoConfiguration$ManagementWebSecurityConfigurerAdapter$$EnhancerBySpringCGLIB$$3db9a4ce]: AutowiredMethodElement for public void org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter.setContentNegotationStrategy(org.springframework.web.accept.ContentNegotiationStrategy)
2019-01-15 20:10:30.888 [main] DEBUG o.s.beans.factory.annotation.InjectionMetadata - Registered injected element on class [org.springframework.boot.actuate.autoconfigure.ManagementWebSecurityAutoConfiguration$ManagementWebSecurityConfigurerAdapter$$EnhancerBySpringCGLIB$$3db9a4ce]: AutowiredMethodElement for public void org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter.setAuthenticationConfiguration(org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration)
2019-01-15 20:10:30.888 [main] DEBUG o.s.beans.factory.annotation.InjectionMetadata - Registered injected element on class [org.springframework.boot.actuate.autoconfigure.ManagementWebSecurityAutoConfiguration$ManagementWebSecurityConfigurerAdapter$$EnhancerBySpringCGLIB$$3db9a4ce]: AutowiredMethodElement for public void org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter.setObjectPostProcessor(org.springframework.security.config.annotation.ObjectPostProcessor)
2019-01-15 20:10:30.888 [main] DEBUG o.s.b.factory.support.DefaultListableBeanFactory - Eagerly caching bean 'org.springframework.boot.actuate.autoconfigure.ManagementWebSecurityAutoConfiguration$ManagementWebSecurityConfigurerAdapter' to allow for resolving potential circular references
2019-01-15 20:10:30.888 [main] DEBUG o.s.beans.factory.annotation.InjectionMetadata - Processing injected element of bean 'org.springframework.boot.actuate.autoconfigure.ManagementWebSecurityAutoConfiguration$ManagementWebSecurityConfigurerAdapter': AutowiredMethodElement for public void org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter.setApplicationContext(org.springframework.context.ApplicationContext)
2019-01-15 20:10:30.888 [main] DEBUG o.s.b.f.a.AutowiredAnnotationBeanPostProcessor - Autowiring by type from bean name 'org.springframework.boot.actuate.autoconfigure.ManagementWebSecurityAutoConfiguration$ManagementWebSecurityConfigurerAdapter' to bean named 'org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@5f0e9815'
2019-01-15 20:10:30.888 [main] DEBUG o.s.beans.factory.annotation.InjectionMetadata - Processing injected element of bean 'org.springframework.boot.actuate.autoconfigure.ManagementWebSecurityAutoConfiguration$ManagementWebSecurityConfigurerAdapter': AutowiredMethodElement for public void org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter.setTrustResolver(org.springframework.security.authentication.AuthenticationTrustResolver)
2019-01-15 20:10:30.889 [main] DEBUG o.s.beans.factory.annotation.InjectionMetadata - Processing injected element of bean 'org.springframework.boot.actuate.autoconfigure.ManagementWebSecurityAutoConfiguration$ManagementWebSecurityConfigurerAdapter': AutowiredMethodElement for public void org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter.setContentNegotationStrategy(org.springframework.web.accept.ContentNegotiationStrategy)
根据日志信息得知是ManagementWebSecurityAutoConfiguration负责加载WebSecurityConfigurerAdapter配置,在springboot启动类加上排除即可。
@SpringBootApplication(exclude={SecurityAutoConfiguration.class, OAuth2AutoConfiguration.class,SecurityFilterAutoConfiguration.class,ManagementWebSecurityAutoConfiguration.class})