0
votes

I am trying to integrate Spring with LDAP, after succesfull login authentication I am able to hit my controller but not able to see view page.

This is my security.xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <beans:beans xmlns="http://www.springframework.org/schema/security"
                 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                 xmlns:beans="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p"
                 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">
                <intercept-url pattern="/abc/**"
                access="ROLE_USER" />

            <form-login login-page="/login" authentication-success-handler-ref="authenticationSuccessHandler" authentication-failure-url="/login?error=true" default-target-url="/home/page"
                login-processing-url="/j_security_check" />

        </http>



    <beans:bean id="authenticationSuccessHandler" class="com.abc.webapp.security.AuthenticationSuccessHandler">
        </beans:bean>

        <beans:bean id="tdrUserDetailsContextMapper" class="com.XXXX.util.LDAPGrantedAuthoritiesMapper"> 
        </beans:bean>



        <authentication-manager>

<authentication-provider  ref="activeDirectoryAuthenticationProvider">

</authentication-provider>

  </authentication-manager> 

  <beans:bean id="activeDirectoryAuthenticationProvider" class="org.springframework.security.ldap.authentication.ad.ActiveDirectoryLdapAuthenticationProvider" > 
<beans:constructor-arg value="XXXXXXX.XXX" /> 
<beans:constructor-arg value="ldaps://XXXXXX:636/" /> 
<beans:property name="convertSubErrorCodesToExceptions" value="true"/> 
<beans:property name="userDetailsContextMapper" ref="tdrUserDetailsContextMapper"/>

</beans:bean>


</beans:beans>

My LDAPMapper class:

package com.abc.util;

import java.util.ArrayList;
import java.util.Collection;
import org.springframework.ldap.core.DirContextAdapter;
import org.springframework.ldap.core.DirContextOperations;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.ldap.userdetails.UserDetailsContextMapper;
public class LDAPGrantedAuthoritiesMapper  implements UserDetailsContextMapper   {

    @Override
    public UserDetails mapUserFromContext(DirContextOperations ctx,
            String username, Collection<? extends GrantedAuthority> authorities) {
        // TODO Auto-generated method stub
        Collection<GrantedAuthority> mappedAuthorities = new ArrayList<GrantedAuthority>();

        ctx.setAttributeValue("referral", "follow");

        SimpleGrantedAuthority roleUser = new SimpleGrantedAuthority("ROLE_USER"); 
         for (GrantedAuthority granted : authorities) {

                if (granted.getAuthority().equalsIgnoreCase("Vendor-TaisTech-CreditPaymentApp-R")) {

                    mappedAuthorities.add(roleUser);
                }
         }

            UserDetails userDetails= new TaisUserDetails(username, "", true, true, true, true, mappedAuthorities);


        return  taisUserDetails;
    }
    @Override
    public void mapUserToContext(UserDetails user, DirContextAdapter ctx) {
        // TODO Auto-generated method stub

    }

}

my urlrewrite.xml:

<urlrewrite default-match-type="wildcard">

    <rule>
        <from>/</from>
        <to type="redirect" last="true">/home/page</to>
    </rule>
    <rule>
        <from>/abc/**</from>
        <to last="true" type="redirect">%{context-path}/$1</to>
    </rule>

    <rule>
        <from>/login*</from>
        <to>/pages/login.jsp</to>
    </rule>
    <rule>
        <from>/logout*</from>
        <to>/pages/logout.jsp</to>
    </rule>

    <!-- Spring MVC -->
    <rule>
        <from>/**</from>
        <to>/abc/$1</to>
    </rule>
</urlrewrite>

In my Authentication Success hanlder I get a authorised user with ROLE_USER. But when I hit my controller class it returns me to: http://localhost:8080/login with 404 error.

In my logs I am able to see that user has been successfully authenticated but I am not able view any of my application page.

2

2 Answers

0
votes

Everything looks correct, i think there is an issue of urlRewrite or check requestInterceptor class if you have created.

0
votes

There was a wrong redirect on my request interceptor, after removing the unnecessary redirect it started working.