Spring security with multiple login pages

Securityspringspring-security

I am using Spring security to secure login to the application admin section with a username and password. But now my client need to have another login screen for the application clients section, where they will have their own usernames / passwords to login to the clients section. So far I've already implemented the admin section login successfully with the following spring-security.xml settings:

<security:http auto-config="true" use-expressions="true">
    <security:form-login login-page="/login"
        default-target-url="/admin/dashboard" always-use-default-target="true"
        authentication-failure-url="/login/admin?error_msg=wrong username or password" />
    <security:intercept-url pattern="/admin/*" access="hasRole('ROLE_ADMIN')" />        
    <security:logout logout-success-url="/login"/>
</security:http>

<security:authentication-manager>
    <security:authentication-provider
        user-service-ref="adminServiceImpl">
    </security:authentication-provider>
</security:authentication-manager>

I've searched the web a lot trying to find how I can add the client section login screen, intercept-url(s), security authentication provider but couldn't find any info, so can someone please help me with any link to any tutorial / example, guide on how to do so?

Thanks

Best Answer

According to the Spring Security docs:

From Spring Security 3.1 it is now possible to use multiple http elements to define separate security filter chain configurations for different request patterns. If the pattern attribute is omitted from an http element, it matches all requests.

Each element creates a filter chain within the internal FilterChainProxy and the URL pattern that should be mapped to it. The elements will be added in the order they are declared, so the most specific patterns must again be declared first.

So, essentially you need two <http> elements each with a different pattern attribute.

There's a detailed tutorial here: https://blog.codecentric.de/en/2012/07/spring-security-two-security-realms-in-one-application/