No bean named 'springSecurityFilterChain' is defined

(198 Views)


In this article, we are discussing the spring org.springframework.beans. factory. NoSuchBeanDefinitionException: No bean named 'springSecurityFilterChain' is defined.
This exception is very common if you are the beginner for spring security.

Here is the complete Exception Stack Trace:

org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'springSecurityFilterChain' is defined at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:529) at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1095) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:277) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1097) at org.springframework.web.filter.DelegatingFilterProxy.initDelegate(DelegatingFilterProxy.java:326) at org.springframework.web.filter.DelegatingFilterProxy.initFilterBean(DelegatingFilterProxy.java:236) at org.springframework.web.filter.GenericFilterBean.init(GenericFilterBean.java:194) at org.apache.catalina.core.ApplicationFilterConfig.initFilter(ApplicationFilterConfig.java:281) at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:262) at org.apache.catalina.core.ApplicationFilterConfig.<init>(ApplicationFilterConfig.java:107) at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:4775) at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5452) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901) at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877) at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:633) at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:656) at org.apache.catalina.startup.HostConfig$DeployDescriptor.run(HostConfig.java:1635) at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471) at java.util.concurrent.FutureTask.run(FutureTask.java:262) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) at java.lang.Thread.run(Thread.java:744)

Cause Of Exception:

The cause of this exception is that ? Spring Security looks for a bean named springSecurityFilterChain (by default), and cannot find it in your application classpath.
This bean is required by the main Spring Security Filter ? the DelegatingFilterProxy ? defined in the web.xml:

<filter> <filter-name>springSecurityFilterChain</filter-name> <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> </filter> <filter-mapping> <filter-name>springSecurityFilterChain</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>

This is just a proxy that delegates all its logic or incoming request to the springSecurityFilterChain bean.

Solution:

The most common reason this bean is missing from the context is that the security XML configuration has no element defined:

<?xml version="1.0" encoding="UTF-8"?> <beans:beans xmlns="http://www.springframework.org/schema/security" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans" xmlns:sec="http://www.springframework.org/schema/security" xsi:schemaLocation=" http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-4.2.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd"> </beans:beans>

If the XML configuration is using the security namespace as the example above, then declaring a simple element will ensure that the filter bean is created and everything starts up correctly:

<http auto-config='true'> <intercept-url pattern="/**" access="ROLE_USER" /> </http>

Another possible reason is that the security configuration is not imported at all into the overall context of the web application.

If the security XML config file is named securityConfig.xml, make sure the resource is imported in the main spring configuration file

Like this we have to import that securityConfig.xml file in java config class

@ImportResource({"classpath:securityConfig.xml"})

Or in XML:

<import resource="classpath:securityConfig.xml" />

Finally, the default name of the filter bean can be changed in the web.xml. It is usually used as an existing Filter with Spring Security:

<filter> <filter-name>springSecurityFilterChain</filter-name> <filter-class> org.springframework.web.filter.DelegatingFilterProxy </filter-class> <init-param> <param-name>targetBeanName</param-name> <param-value>customFilter</param-value> </init-param> </filter>

Conclusion:

In this article we discussed about the Spring Security problem, the missing filter chain bean and showed the solutions to this common issue.

If you have any other doubt, feel free to ask us in the comments section, we will try to solve it too.

    

Solution Worked 0 UpvotesUpvote

        

Solution Didn't Worked 0 DownvotesDownvote



Comments



Search