今天配置spring security时,运行报出No bean named ‘springSecurityFilterChain’ available错误,报错信息如下
根据错误信息可以看出是服务器启动springSecurityFilterChain没有找到"springSecurityFilterChain"bean。
因为spring security是使用java配置的,因此我在spring mvc的xml配置文件中添加了对spring security配置类的扫描,但是运行时却报错没有发现该bean
因此通过网络查阅资料后发现,因为我们在web.xml中注册spring mvc 的dispatcherServlet时,是使用加载的配置文件,而通过初始化的对象
只能在servlet中使用,但是我们这里的springSercurityFilterChain bean是需要在springSercurity的Filter中使用,因此我们需要在中初始化,中初始化的对象全局有效
因此将springSercurity的配置类放在applicationContext.xml配置文件中注册
具体操作如下:
在web.xml添加如下
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>