spring-security saml2:如何获取当前用户?

问题描述 投票:0回答:1

我正在使用5.2.0.RELEASE版本的spring-security和spring-security-saml2-service-provider。我正在尝试通过IDP进行身份验证以获取当前断言,以便将其映射到我们本地系统中的用户。我使用此代码获取Saml2Authentication对象

@Component
@Log4j
public class EventListener implements ApplicationListener<InteractiveAuthenticationSuccessEvent> {
    @Override
    public void onApplicationEvent(InteractiveAuthenticationSuccessEvent interactiveAuthenticationSuccessEvent) {
        Object principal = interactiveAuthenticationSuccessEvent.getAuthentication().getPrincipal();
        Saml2Authentication authentication = (Saml2Authentication)interactiveAuthenticationSuccessEvent.getAuthentication()

但是我无法吸引用户。我可以得到saml2响应(authentication.getSaml2Response),但是我不确定如何获取带有用户ID的断言。

真的,我想在Java代码中执行Retrieve Attributes and NameID from a SAML Response (XML) 。不确定弹簧安全性中是否有什么可以帮助我的。

更新因此,经过一些工作,我使用OpenSAML库获取属性并解析SAMLv2响应。我不知道这是否是正确的方法

@Override
    public void onApplicationEvent(InteractiveAuthenticationSuccessEvent interactiveAuthenticationSuccessEvent) {
        Saml2Authentication authentication = (Saml2Authentication) interactiveAuthenticationSuccessEvent.getAuthentication();
        try {

            Document messageDoc;
            BasicParserPool basicParserPool = new BasicParserPool();
            basicParserPool.initialize();
            InputStream inputStream = new ByteArrayInputStream(authentication.getSaml2Response().getBytes());
            messageDoc = basicParserPool.parse(inputStream);
            Element messageElem = messageDoc.getDocumentElement();
            Unmarshaller unmarshaller = XMLObjectProviderRegistrySupport.getUnmarshallerFactory().getUnmarshaller(messageElem);
            XMLObject samlObject = unmarshaller.unmarshall(messageElem);
            Response response = (Response) samlObject;
            response.getAssertions().forEach(assertion -> {
                assertion.getAttributeStatements().forEach(attributeStatement ->
                {
                    attributeStatement.getAttributes().forEach(attribute -> {
                        log.error("Names:" + attribute.getName() + getAttributesList(attribute.getAttributeValues()));
                    });
                });
            });
        } catch (Exception e) {
            log.error(e);
        }
    }

    private List<String> getAttributesList(Collection<XMLObject> collection) {
        return collection.stream().map(this::getAttributeValue)
                .collect(Collectors.toList());
    }

    private String getAttributeValue(XMLObject attributeValue) {
        return attributeValue == null ?
                null :
                attributeValue instanceof XSString ?
                        getStringAttributeValue((XSString) attributeValue) :
                        attributeValue instanceof XSAnyImpl ?
                                getAnyAttributeValue((XSAnyImpl) attributeValue) :
                                attributeValue.toString();
    }

    private String getStringAttributeValue(XSString attributeValue) {
        return attributeValue.getValue();
    }

    private String getAnyAttributeValue(XSAnyImpl attributeValue) {
        return attributeValue.getTextContent();
    }
spring-security saml-2.0 spring-saml spring-security-saml2
1个回答
1
投票

默认情况下,Spring SAML Security使用Subject元素中的NameID值设置“用户名”。如果为“临时NameID格式”,则每个SAML规范的每个SSO流的值都必须是不同/唯一的值。

您可以实现自己的UserDetailsService,该方法可以在方法org.springframework.security.saml.userdetails.SAMLUserDetailsService中实现loadUserBySAML,您可以提取SAML属性

@Override
public Object loadUserBySAML(SAMLCredential credential) throws UsernameNotFoundException {
  final List<Attribute> attributesFromAttributeStatement = credential.getAttributes();

  final String userName = getUserNameValueFromAttribute(attributesFromAttributeStatement);

  // if needed calculate authorities
  final List<GrantedAuthority> grantedAuthorities = getGrantedAuthorities();
  final User authenticatedUser = new User(userName, "", grantedAuthorities);
  return authenticatedUser;
}
© www.soinside.com 2019 - 2024. All rights reserved.