Custom SAMLUserDetailsService does not populate custom UserDetails

I have a Spring project and I am converting my current authentication to use SAML2.

Everything works for me, up to authentication, but I am having difficulty getting the SAML2 extension to insert my custom UserDetails object into the Spring Security Context authentication object.

I have a custom UserDetailsService defined below:

public class SAMLAuthManager implements SAMLUserDetailsService {

    private static final Logger logger = Logger.getLogger(JDBCAuthManager.class);

    @Override
    public Object loadUserBySAML(SAMLCredential credential) throws UsernameNotFoundException {
        logger.info("Credential attributes: " + credential.getAttributes());
        for (int x = 0; x < credential.getAttributes().size(); x++) {
            Attribute attr = credential.getAttributes().get(x);
            List<XMLObject> attrValues = attr.getAttributeValues();
            StringBuilder strBuilder = new StringBuilder();
            for (int g = 0; g < attrValues.size(); g++) {
                XMLObject currObj = attrValues.get(g);
                strBuilder.append(currObj.toString()).append(",");
            }
            strBuilder.deleteCharAt(strBuilder.length() - 1);
            logger.info(attr.getFriendlyName() + ", " + strBuilder.toString());
        }
        String username = credential.getNameID().getValue();
            userWrapper.setStaff(s);
            logger.info("Returning wrapper: " + userWrapper);
            return userWrapper;
        } else {
            return null;
        }
    }

}

I also configured this userDetails in my security context configuration:

    <bean id="samlAuthenticationProvider" class="org.springframework.security.saml.SAMLAuthenticationProvider">
        <property name="userDetails" ref="samlUserDetails" />
    </bean>

However, when I check SecurityContextHolder, after authentication, this line:

SecurityContextHolder.getContext().getAuthentication().getCredentials();

returns an object of type org.springframework.security.saml.SAMLCredential.

, SecurityContextHolder.getContext().getAuthentication().getPrincipal() Spring (SecurityContextHolder.getContext().getAuthentication().getPrincipal()), , String .

?

+5
1

String ( Principal, NameID).

, forcePrincipalAsString SAMLAuthenticationProvider false, Spring SAML , SAMLUserDetailsService Authentication.

SAMLUserDetailsService SecurityContextHolder.getContext().getAuthentication().getDetails().

+5

Source: https://habr.com/ru/post/1615510/


All Articles