getting exception: No bean named 'springSecurityFilterChain' is defined
Asked Answered
L

5

46

I am learning spring security from reference material. release 3.1.2.RELEASE. As stated in that I have configured security:http tag like this

security-context.xml

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

web.xml

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:*-context.xml</param-value>
  </context-param>

  <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>

  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

  <servlet>
    <servlet-name>security</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>security</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

security-servlet.xml

<context:component-scan base-package="com.pokuri.security.mvc.controllers"/>

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
        <property name="prefix" value="/WEB-INF/page/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

But I am getting this exception when I start the application. If I remove security configuration my spring web application working fine. I went through the same kind of questions in stackoverflow. But no luck.

Licko answered 25/8, 2012 at 16:3 Comment(1)
Add it to the root application context or DispatcherServlet application context. You can do that easily by extending AbstractAnnotationConfigDispatcherServletInitializer.Crowson
D
68

I think that the reason of your problem can be in that your xml configuration file for spring security isn't loaded when you start your web app.

To fix this you should specify all your XML config files in web.xml like that:

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/spring-security.xml, /WEB-INF/applicationContext.xml</param-value>
</context-param>

If you have your config files in classpath (not WEB-INF folder or it's subfolders) then you can specify list of config files in such way;

...
<param-value>
    classpath:applicationContext.xml,
    classpath:spitter-security.xml
</param-value>
...

And also you need to add special listener that will load your config files:

<listener>
    <listener-class>
        org.springframework.web.context.ContextLoaderListener
    </listener-class>
</listener>
Demodulator answered 25/8, 2012 at 19:49 Comment(4)
but I have context-parameter and listener configuration in web.xmlLicko
yeah this is the problem with my context-parameter. I have given parameter value as classpath*:*-context.xml which is not correct way to pick up security-context.xml. So changed wildcard to classpath:**/*-context.xml. Now everything working fineLicko
Note: I had to add it to the root application context (not the app servlet context).Kemeny
@Demodulator I have a similar problem. Are you willing to help me out with it? Here is the link: #22644077Amuck
S
13

I just added the bean definition in applicationContext.xml as Spring asked:

<bean id="springSecurityFilterChain" class="org.springframework.web.filter.DelegatingFilterProxy"/>
Spacetime answered 23/6, 2015 at 19:13 Comment(0)
F
5

add this your web.xml

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/root-context.xml, /WEB-INF/spring-security.xml</param-value>
</context-param>

<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

        <!-- filter declaration for Spring Security -->
<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>
Fetch answered 20/2, 2015 at 20:14 Comment(0)
Z
1

In case it helps anyone, I had renamed one of my packages but Eclipse doesn't auto-update your @ComponentScan paths, so make sure you change that too:

@ComponentScan(basePackages = "com.package.spring")
Zapata answered 27/8, 2018 at 8:19 Comment(0)
F
0

For those who learning spring without xml may be this helps.

I faced the same exception when learning the spring without xml.

Found that i have register the spring security filter by extending the class AbstractSecurityWebApplicationInitializer but did not configure the spring security. To configure the spring security i added below code and it works.

@Configuration
@EnableWebSecurity
public class SpringSecurityConfigInitializer extends 
WebSecurityConfigurerAdapter {

@Override
protected void configure(AuthenticationManagerBuilder auth) throws 
Exception {
    UserBuilder users = User.withDefaultPasswordEncoder();
auth.inMemoryAuthentication().withUser(users.username("test").
password("test123").roles("EMPLOYEE"));
 }

 }

Above is just sample for my case but the exception was due to the missing configuration.

Flexion answered 10/2, 2022 at 17:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.