Spring security 自定义SamluserDetails服务未填充自定义UserDetails
我有一个Spring项目,我正在将当前的身份验证转换为使用SAML2 就身份验证而言,我已经完成了所有工作,但是我很难让SAML2扩展将我的自定义UserDetails对象插入到Spring安全上下文身份验证对象中 我有一个自定义UserDetails服务,定义如下:Spring security 自定义SamluserDetails服务未填充自定义UserDetails,spring-security,saml-2.0,spring-saml,Spring Security,Saml 2.0,Spring Saml,我有一个Spring项目,我正在将当前的身份验证转换为使用SAML2 就身份验证而言,我已经完成了所有工作,但是我很难让SAML2扩展将我的自定义UserDetails对象插入到Spring安全上下文身份验证对象中 我有一个自定义UserDetails服务,定义如下: public class SAMLAuthManager implements SAMLUserDetailsService { private static final Logger logger = Logger.g
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;
}
}
}
返回类型为org.springframework.security.saml.SAMLCredential
的对象
我检查了Spring是否使用自定义对象(SecurityContextHolder.getContext().getAuthentication().getPrincipal()
)填充主体,但没有,这只是填充了用户名的字符串
有什么想法吗?
感谢主体在默认情况下被强制为字符串(以便始终允许复制主体,该主体以前是不可序列化的NameID) 这可以通过将
SAMLAuthenticationProvider
中的forceprincipalsString
设置为false
来更改,这将使Spring SAML将SAMLUserDetailsService
提供的对象作为主体包含在身份验证
对象中
调用
SAMLUserDetailsService
的结果总是在SecurityContextHolder.getContext().getAuthentication().getDetails()
下可用,因此我看到Spring使用自定义UserDetails对象填充SecurityContextHolder.getContext().getAuthentication().getDetails()
。这与我使用的其他身份验证管理器(例如LDAP)的工作方式不同。有什么原因吗?通常我看到Spring安全性使用类型为org.springframework.Security.web.authentication.WebAuthenticationDetails
的对象填充SecurityContextHolder().getContext().getDetails(),所以在我看来Spring SAML扩展的工作方式可能有所不同?
<bean id="samlAuthenticationProvider" class="org.springframework.security.saml.SAMLAuthenticationProvider">
<property name="userDetails" ref="samlUserDetails" />
</bean>
SecurityContextHolder.getContext().getAuthentication().getCredentials();