Search code examples
javaweb-servicesrestful-authenticationspring-web

No bean named 'springSecurityFilterChain' is defined exception for spring restful web-service authentication


I am trying to add authentication in my web service using spring. I have referred this resource for the sample code. But unfortunately, I am getting an exception.

org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'springSecurityFilterChain' is defined

I have gone through many references on google. But I didn't found any solution. So can anyone please suggest any solution what I am doing wrong here?

I am using following configurations.

Spring version - 3.1.1

Application context - web.xml

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

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

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

<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <!--<url-pattern>/*</url-pattern>-->
    <url-pattern>*.htm</url-pattern>
</servlet-mapping>
<session-config>
    <session-timeout>
        30
    </session-timeout>
</session-config>
<welcome-file-list>
    <welcome-file>redirect.jsp</welcome-file>
</welcome-file-list>

dispatcher-servlet.xml

<mvc:annotation-driven/>

<context:annotation-config/>

<context:component-scan base-package="com.em.yms.*" />

<import resource="spring-security.xml"/>

<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    <property name="mappings">
        <props>
            <prop key="index.htm">indexController</prop>
        </props>
    </property>
</bean>


<bean id="viewResolver"
      class="org.springframework.web.servlet.view.InternalResourceViewResolver"
      p:prefix="/WEB-INF/jsp/"
      p:suffix=".jsp" />

<bean name="indexController"
      class="org.springframework.web.servlet.mvc.ParameterizableViewController"
      p:viewName="index" />

spring-security.xml

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

<authentication-manager alias="authenticationManager">
    <authentication-provider>
        <user-service id="authenticationService">
            <user name="user" password="123456" authorities="ROLE_USER"/>
        </user-service>
    </authentication-provider>
</authentication-manager> 

Solution

  • Try importing your spring-security.xml file in your application.xml file instead or your dispatched-servlet.xml file.

    I think the issue is in the order of loading of the context files. In a J2EE container, the order of loading is listener, filter, server. As your application context file is loaded by a listener it will be loaded before the spring security filter. But as the dispatcher servlet is loaded after the filter, if you import your spring security file in it's dispatched-servlet.xml file it will not be available at filter instantiation time ...

    Regards,

    Loïc