1

I'm having trouble setting Spring Security in a Maven Webapp Project. Whenever I run the app on the server it displays the following error :

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

Now I know this question has already been asked and I have been trying the various solutions proposed since yesterday with no avail.

Note that spring-security.xml and spring-database.xml are inside a folder named spring in the WEB-INF directory while the applicationContext.xml is in the WEB-INF dir.

Here are my config files :

web.xml :

<web-app>


<display-name>Archetype Created Web Application</display-name>
<context-param>
    <param-name>contextConfiguration</param-name>
    <param-value>
            /WEB-INF/spring/spring-security.xml
            /WEB-INF/spring/spring-database.xml
            /WEB-INF/applicationContext.xml
    </param-value>
</context-param>

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

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

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

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

spring-security.xml :

<beans:beans xmlns="http://www.springframework.org/schema/security"
xmlns:beans="http://www.springframework.org/schema/beans" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security.xsd">

<http auto-config="true" use-expressions="true">
    <access-denied-handler error-page="/403" />
    <intercept-url pattern="/admin**" access="hasRole('ROLE_ADMIN')" />
    <form-login 
        login-page="/login" 
        default-target-url="/admin" 
        authentication-failure-url="/login?error" 
        username-parameter="username"
        password-parameter="password" />
    <logout logout-success-url="/login?logout" invalidate-session="true"/>
    <!-- enable csrf protection -->
    <csrf/>
</http>


<authentication-manager>
  <authentication-provider>
    <jdbc-user-service data-source-ref="dataSource"
      users-by-username-query=
        "select login,pwd, enabled from utilisateur where login=?"
      authorities-by-username-query=
        "select login, role from role where login =?" />
  </authentication-provider>
</authentication-manager>

applicationContext.xml :

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

<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->

<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />

<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />

<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <beans:property name="prefix" value="/WEB-INF/vues/" />
    <beans:property name="suffix" value=".jsp" />
</beans:bean>

<context:component-scan base-package="com.myApp.controllers" />

</beans:beans>

spring-database.xml :

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

    <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">

        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/wfmconf" />
        <property name="username" value="root" />
        <property name="password" value="The_Master123456" />
    </bean>

</beans>

Edit :

i have already specified both the applicationContext and spring-security configuration files as context parameteres with the correct URL in my web.xml

bdero
  • 11
  • 4
  • possible duplicate of [getting exception: No bean named 'springSecurityFilterChain' is defined](http://stackoverflow.com/questions/12123516/getting-exception-no-bean-named-springsecurityfilterchain-is-defined) – Jens Apr 28 '15 at 08:47
  • "Note that spring-security.xml and spring-database.xml are inside a folder named spring ... ", but you have /WEB-INF/spring-security.xml. Is this just a typo? – ikumen Apr 28 '15 at 10:32
  • yes it is just a typo, i have edited my question accordingly. – bdero Apr 28 '15 at 11:44

1 Answers1

0

I have found my problem. the context-param name should be contextConfigLocation instead of contextConfiguration and the applicationContext.xml should be specified as an init-param to the servlet.

so the correct web.xml would be :

<web-app>


<display-name>Archetype Created Web Application</display-name>
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
            /WEB-INF/spring/spring-security.xml
            /WEB-INF/spring/spring-database.xml
    </param-value>
</context-param>

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

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

<servlet>
    <servlet-name>appServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

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

bdero
  • 11
  • 4