自定义SAMLUserDetailsS​​ervice不填充自定义UserDetails

ale*_*cci 5 spring-security saml-2.0 spring-saml

我有一个Spring项目,我正在转换当前的身份验证以使用SAML2。

我已经完成了身份验证的所有工作,但是我很难获得SAML2扩展以将我的自定义UserDetails对象插入到Spring Security Context身份验证对象中。

我有一个自定义的UserDetailsS​​ervice,定义如下:

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

}
Run Code Online (Sandbox Code Playgroud)

我还在我的安全上下文配置中配置了此userDetails:

    <bean id="samlAuthenticationProvider" class="org.springframework.security.saml.SAMLAuthenticationProvider">
        <property name="userDetails" ref="samlUserDetails" />
    </bean>
Run Code Online (Sandbox Code Playgroud)

但是,当我检查SecurityContextHolder时,请进行身份验证,此行:

SecurityContextHolder.getContext().getAuthentication().getCredentials();
Run Code Online (Sandbox Code Playgroud)

返回类型为的对象org.springframework.security.saml.SAMLCredential。

我检查了一下Spring是否用自定义对象(SecurityContextHolder.getContext().getAuthentication().getPrincipal())填充了Principal,但是没有,只是String填充了用户名。

有任何想法吗?谢谢

Vla*_*fer 5

默认情况下,主体被强制为String(为了始终允许复制主体,该主体以前是不可序列化的NameID)。

这可以通过设置来改变forcePrincipalAsString在SAMLAuthenticationProvider给false,这将使得春季SAML包括提供你的对象SAMLUserDetailsService作为主要Authentication对象。

的调用结果SAMLUserDetailsService始终在下提供SecurityContextHolder.getContext().getAuthentication().getDetails()。