springboot项目中运行spring security 报 Error creating bean with name 'springSecurityFilterChain'。。。。错误
springboot版本:1.5.9
spring security版本:1.5.9
问题描述:
启动springboot项目,直接编译报错,内容:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'springSecurityFilterChain' defined in class path resource [org/springframework/security/config/annotation/web/configuration/WebSecurityConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.servlet.Filter]: Factory method 'springSecurityFilterChain' threw exception; nested exception is java.lang.NullPointerException
简单理解:在启动springboot项目时,会自动去加载SecurityConfig这个类 里面的配置(SecurityConfig类是自己写的,用来继承WebSecurityConfigurerAdapter 实现自定义登录),其他底层的不多说,如图:
解决方案:在springboot启动类中加入手动注解,
下图
注意:该注解一定是扫描到 SecurityConfig 这个类的文件夹
这样子再次启动springboot就无问题了
原因:是 [org/springframework/security/config/annotation/web/configuration/WebSecurityConfiguration.class] 在类路径资源中定义名称为“springSecurityFilterChain”的Bean创建错误,不像springboot 2.xxx版本是自动扫描的,所以这里需要手动添加
建议:使用springboot版本2.xxx 以上,方便快捷