【问题标题】:How to use a X509 certificate as a public key in SAML response如何在 SAML 响应中使用 X509 证书作为公钥
【发布时间】:2018-09-07 16:02:15
【问题描述】:

我正在从事 SP 方面的工作。我们收到了来自 IDP 的 SAML2 响应。

看起来有点像这里的例子:

SAML: Why is the certificate within the Signature?

现在我正在使用 OpenSaml2 来解析和处理这个 xml 文件中的内容。

我需要从响应中提取证书并将其用作凭据。

到目前为止,我已经这样做了:

Response response = (Response) xmlObject;
SAMLSignatureProfileValidator profileValidator = new 
SAMLSignatureProfileValidator();
Signature signature = response.getSignature();
Credential credential = null;
profileValidator.validate(signature);
SignatureValidator validator = new SignatureValidator(credential);
validator.validate(signature);

在上面的代码中,凭证暂时为“null”,但我需要它是证书中的公钥。知道我该怎么做吗?

听说 opensaml2 有 KeyInfoCredentialResolver 等方法可以帮助解决这个问题,但还没有看到一个简单的实现。

【问题讨论】:

    标签: java xml saml saml-2.0 opensaml


    【解决方案1】:

    我可以通过以下方式解决这个问题:

    X509Certificate certificate = signature.getKeyInfo().getX509Datas().get(0).getX509Certificates().get(0);
    
            if (certificate != null) {
                //Converts org.opensaml.xml.signature.X509Certificate to java.security.cert.Certificate
                String lexicalXSDBase64Binary = certificate.getValue();
                byte[] decoded = DatatypeConverter.parseBase64Binary(lexicalXSDBase64Binary);
    
                try {
                    CertificateFactory certFactory = CertificateFactory.getInstance("X.509");
                    Certificate cert = certFactory.generateCertificate(new ByteArrayInputStream(decoded));
                    return cert;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-03
      • 2013-01-06
      • 2017-06-13
      • 1970-01-01
      • 2012-07-17
      • 1970-01-01
      相关资源
      最近更新 更多