将SAML2 AuthnRequest转换为Java类

时间:2015-08-25 14:09:07

标签: java saml-2.0 unmarshalling opensaml

我使用OpenSAML 2.6.5作为SAML2库。 我还没有找到有关如何编组表示来自服务提供商的AuthnRequest的字符串(XML文档)的文档。 任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:1)

要从SP读取请求,您需要对传入的String进行编码和解组,如下所示:

    DefaultBootstrap.bootstrap(); //crucial in SAML2
    byte[] decodedSamlAsBytes = Base64.decode(incomingEncodedSaml);

    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = documentBuilderFactory.newDocumentBuilder();

    Document document = docBuilder.parse(new ByteArrayInputStream(decodedSamlAsBytes));
    Element element = document.getDocumentElement();

    UnmarshallerFactory unmarshallerFactory = Configuration.getUnmarshallerFactory();
    Unmarshaller unmarshaller = unmarshallerFactory.getUnmarshaller(element);
    XMLObject requestXmlObj = unmarshaller.unmarshall(element);
    AuthnRequest request = (AuthnRequest) requestXmlObj;