【发布时间】:2016-02-13 06:55:49
【问题描述】:
我有一个 Spring 项目,我正在将我当前的身份验证转换为使用 SAML2。
就身份验证而言,我的一切工作正常,但我很难获得 SAML2 扩展以将我的自定义 UserDetails 对象插入 Spring Security Context 身份验证对象。
我有一个自定义的 UserDetailsService,定义如下:
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;
}
}
}
我还在我的安全上下文配置中配置了这个 userDetails:
<bean id="samlAuthenticationProvider" class="org.springframework.security.saml.SAMLAuthenticationProvider">
<property name="userDetails" ref="samlUserDetails" />
</bean>
但是,当我检查 SecurityContextHolder,发布身份验证时,这一行:
SecurityContextHolder.getContext().getAuthentication().getCredentials();
返回org.springframework.security.saml.SAMLCredential 类型的对象。
我检查了 Spring 是否使用自定义对象 (SecurityContextHolder.getContext().getAuthentication().getPrincipal()) 填充了 Principal,但没有,这只是填充了用户名的 String。
有什么想法吗? 谢谢
【问题讨论】:
-
好的 - 所以我看到 Spring 使用自定义 UserDetails 对象填充
SecurityContextHolder.getContext().getAuthentication().getDetails()。这与我使用的其他身份验证管理器的工作方式不同(例如 LDAP 管理器)。这有什么原因吗? -
通常我看到 Spring Security 用
org.springframework.security.web.authentication.WebAuthenticationDetails类型的对象填充 SecurityContextHolder().getContext().getDetails() 所以在我看来 Spring SAML 扩展可能会做不同的事情?
标签: spring-security saml-2.0 spring-saml