Getting authentication object is null even after successfully login by IDP using SAML

本小妞迷上赌 提交于 2019-12-04 12:40:52

After struggling for almost a week, finally I have fixed this issue.

While debugging through eclipse I found the root cause inside SAMLAuthenticationProvider there is a method getEntitlements which was causing problem.

   protected Collection<? extends GrantedAuthority> getEntitlements(SAMLCredential credential, Object userDetail) {
        if (userDetail instanceof UserDetails) {
            List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
            authorities.addAll(((UserDetails) userDetail).getAuthorities());
            return authorities;
        } else {
            return Collections.emptyList();
        }
    }

Here It is checking whether userDetail object is an instanceOf UserDetails class then return all authority list otherwise empty list of authority will be return.

It is fine with form based authentication which returns UserDetails object but If a user logged in through IDP initiated SSO then object of type UsernamePasswordAuthenticationToken will be return. Hence It is getting empty list of grantedAuthourity with userDetail object.

So I extends SAMLAuthenticationProvider inside my application and override the below method

@Override
public Collection<? extends GrantedAuthority> getEntitlements(SAMLCredential credential, Object userDetail)
    {
        logger.info("****** object is instance of UserDetails :"+ (userDetail instanceof UserDetails));

        if (userDetail instanceof UserDetails) 
        {
            List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
            authorities.addAll(((UserDetails) userDetail).getAuthorities());
            return authorities;
        } 
        else if(userDetail instanceof UsernamePasswordAuthenticationToken) 
        {
             List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
             authorities.addAll(((UsernamePasswordAuthenticationToken) userDetail).getAuthorities());
             return authorities;

        } else {
            return Collections.emptyList();
        }
    }

Then I give my custom authenticationProvider reference into saml-security.xml file with my custom SAMLUserDetailsService class reference.

   <bean id="samlAuthenticationProvider" class="com.mercatus.security.MercatusSAMLAuthenticationProvider">
        <property name="userDetails" ref="samlUserDetailsService" />
    </bean>

    <bean id="samlUserDetailsService" class="com.mercatus.security.MercatusSAMLUserDetailsService"/>

The above configuration saved me. I am able to access protected resource after login.

I spent a whole week debugging inside FilterChainProxy, many other filters and here and there because of intercepter URL it was redirecting to FilterChainProxy.

I am posting detailed info because It may be helpful for others who are facing similar issue.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!