Search code examples
javaspring-securitynamespacesmultiple-forms

Multiple login form with different namespace in Spring Security


I want to create two form-logins with different namespace, one for User login (namespace default "/") , one for Admin login with namespace "/admin". When I run program, the form-login for User run fine. But form-login for Admin run fail.

This is my file spring security:

<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-3.0.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.1.xsd">

<http pattern="/login*" security="none" />
<http pattern="/admin/" security="none" />

<http pattern="/admin/**">
    <intercept-url pattern='admin/**' access='ROLE_ADMIN' />
    <access-denied-handler error-page="/user/403.jsp" />
    <form-login login-page="/adminLogin" default-target-url="/adminAccess"
        authentication-failure-url="/adminLogin?error" username-parameter="username"
        password-parameter="password" />
    <logout logout-success-url="/adminLogin?logout" />
    <http-basic />
</http>
<http>
    <intercept-url pattern="/user" access="ROLE_USER" />
    <intercept-url pattern="/**" access="ROLE_USER,ROLE_ADMIN" />
    <access-denied-handler error-page="/user/403.jsp" />
    <form-login login-page="/login" default-target-url="/userAccess"
        authentication-failure-url="/login?error" username-parameter="username"
        password-parameter="password" />
    <logout logout-success-url="/login?logout" />
</http>

<beans:bean id="daoAuthenticationProvider"
    class="org.springframework.security.authentication.dao.DaoAuthenticationProvider">
    <beans:property name="userDetailsService" ref="userDetailsService" />
</beans:bean>

<beans:bean id="authenticationManager"
    class="org.springframework.security.authentication.ProviderManager">
    <beans:property name="providers">
        <beans:list>
            <beans:ref local="daoAuthenticationProvider" />
        </beans:list>
    </beans:property>
</beans:bean>

<authentication-manager erase-credentials="false">
    <authentication-provider user-service-ref="userDetailsService">
    </authentication-provider>
</authentication-manager>

When I login in form-login for Admin (namespace "/admin"), The error happend with messeage "HTTP Status 404 - There is no Action mapped for namespace [/admin] and action name [j_spring_security_check] associated with context path [/Java-framework]. "

The order filter in my web.xml file:

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

<!-- Define filter for Struts 2 -->
<filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>

<filter>
    <filter-name>encodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
        <param-name>encoding</param-name>
        <param-value>UTF-8</param-value>
    </init-param>
    <init-param>
        <param-name>forceEncoding</param-name>
        <param-value>true</param-value>
    </init-param>
</filter>

<filter-mapping>
    <filter-name>encodingFilter</filter-name>
    <url-pattern>/* </url-pattern>
</filter-mapping>

<filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

Action in my struts.xml file:

<package name="default" namespace="/" extends="struts-default">
    <action name="login">  
        <result>USER_LOGIN_SPRING_SECURITY</result>  
    </action> 
    <action name="userAccess" class="springAdminAction" method="execute">
        <result name="success">USER_SPRING_SECURITY</result>
    </action>

<package name="admin" namespace="/admin" extends="default-web-admin">
<action name="adminAccess" class="springAdminAction" method="execute">
        <result name="success">ADMIN_SPRING_SECURITY</result>
    </action>       
<action name="adminLogin">
        <result>ADMIN_LOGIN_SPRING_SECURITY</result>  
    </action>


Solution

  • Look like the pattern admin/** is additive in <http pattern="/admin/**">.

    <http pattern="/admin/**">
        <intercept-url pattern='/admin/**' access='ROLE_ADMIN' />
    

    This prompt the Spring Security protected url of /admin/admin/** instead of /admin/**.

    Please try following see whether it can help:

    <http pattern="/admin/**">
        <intercept-url pattern='/**' access='ROLE_ADMIN' />
    

    Edited:

    From your error:

    HTTP Status 404 - There is no Action mapped for namespace [/admin] and action name [j_spring_security_check] associated with context path [/Java-framework].

    the URL POST to server should be /admin/j_spring_security_check, but the default URL where Spring Security Form Login to trigger the authentication process is /j_spring_security_check.

    This cause Spring Security by pass the authentication and the request of /admin/j_spring_security_check will pass to Struts filters and you got that error.

    This URL of j_spring_security_check can be overridden via the login-processing-url attribute on <form-login>:

    login-processing-url="/admin/j_spring_security_check"
    

    Please try this with followings:

    <form-login login-page="/adminLogin" 
                login-processing-url="/admin/j_spring_security_check" 
                default-target-url="/adminAccess"
                authentication-failure-url="/adminLogin?error" 
                username-parameter="username"
                password-parameter="password" />
    

    Edited2:

    From your another error,

    HTTP Status 404 - There is no Action mapped for namespace [/admin] and action name [j_spring_security_logout] associated with context path [/Java-framework]

    The cause is similar which the default URL for Form Logout is j_spring_security_logout. You can override it via logout-url.

    Your logout filter will be look like:

    <logout logout-url="/admin/j_spring_security_logout" 
            logout-success-url="/adminLogin?logout" />