在 servlet 环境(Tomcat Web 服务器)中使用 OPENSAML2 时出现编组错误

发布于 2024-12-07 17:13:13 字数 624 浏览 1 评论 0原文

我正在尝试使用 OpenSAML2 库设置我的服务提供商。我正在尝试设置一个 servlet,它接受来自用户浏览器的 HttpRequest,并根据我想要创建 SAML 身份验证请求数据包的请求参数。

我能够创建 SAML 数据包,但是我想对 SAML 进行 Base64 编码,以便我可以将用户浏览器重定向到身份提供程序服务器。为此,我尝试使用“org.opensaml.saml2.core.impl.AuthnRequestMarshaller#marshall(AuthnRequest)”来编组 SAML 身份验证请求。

当我在 Web 服务器环境(独立 JAVA 应用程序)之外尝试时,效果很好。但是,当我将其作为 servlet 组件运行时,我收到一条错误消息“没有可用于 {urn:oasis:names:tc:SAML:2.0:assertion}Issuer,{urn:oasis:names:tc:SAML:2.0 的子级”的编组器:protocol}AuthnRequest",其中 Issuer 是 AuthnRequest 对象的组件。

我正在使用 Tomcat 5.5.34 网络服务器。如果有人能帮助我弄清楚这里发生了什么,那就太好了。它也将帮助其他尝试编写自己的服务提供者的人。令人惊讶的是,可用于此目的的文档非常少。

谢谢, 考斯图布

I am trying to setup my Service provider using OpenSAML2 libraries. I am trying to set up a servlet which would accept HttpRequest from user's browser and based on the request parameters I want to create a SAML Authentication request packet.

I am able to create the SAML packet, however I want to Base64 encode the SAML so that I can redirect the user browser to the Identity provider server. In order to do that I am trying to marshall the SAML Authentication Request using 'org.opensaml.saml2.core.impl.AuthnRequestMarshaller#marshall(AuthnRequest)'.

This works fine when I try it outside of the web server environment (standalone JAVA application). However when I run it as a servlet component I get an error saying "No marshaller available for {urn:oasis:names:tc:SAML:2.0:assertion}Issuer, child of {urn:oasis:names:tc:SAML:2.0:protocol}AuthnRequest", where Issuer is a component of the AuthnRequest object.

I am using Tomcat 5.5.34 webserver. It would be nice if someone can help me figure out whats happening here. It would help others trying to write their own Service Providers as well. Surprisingly very less documentation is available for this.

Thanks,
Kaustubh

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

长亭外,古道边 2024-12-14 17:13:13

您需要在 Tomcat common/endorsed 目录中包含以下 jars..resolver

  • -2.9.1.jar
  • serializer-2.9.1.jar
  • xalan-2.7.1.jar
  • xercesImpl-2.9.1.jar
  • xml-apis-2.9.1。罐子

这里是一个示例SAML2 服务提供者[还包括示例 WAR]。

You need to have following jars inside Tomcat common/endorsed directory..

  • resolver-2.9.1.jar
  • serializer-2.9.1.jar
  • xalan-2.7.1.jar
  • xercesImpl-2.9.1.jar
  • xml-apis-2.9.1.jar

Here is an example of SAML2 service provider [also includes the sample WAR].

莫多说 2024-12-14 17:13:13

普拉巴斯的回答很中肯。我遇到了同样的问题,并通过在 Sun iJDK 中支持 xalan 和 xerces 解决了这个问题。查看 Shibboleth 网站 的官方指南。查看“安装库”下的 openSAML 部署要求。

Prabath's answer is on the dot. I faced the same issue, and resolved it by endorsing xalan and xerces in the Sun iJDK. Check out the official guide from the Shibboleth site. Look under "Installing the Library" for openSAML deployment requirements.

执妄 2024-12-14 17:13:13

我可以通过引入这行代码来解决这个问题 - DefaultBootstrap.bootstrap();

private AuthnRequest buildSAMLRequest(String samlReqID) throws Exception {

    AuthnRequestBuilder authnRequestBuilder = new AuthnRequestBuilder();
    AuthnRequest authnRequest = authnRequestBuilder.buildObject();

    DefaultBootstrap.bootstrap();
    authnRequest.setID(samlReqID);
    authnRequest.setIssuer(createSAMLIssuer());
    authnRequest.setDestination(DESTINATION);
    authnRequest.setSignature(createSAMLSignature());
    authnRequest.setAssertionConsumerServiceURL(ASSERTION_CONSUMER_SERVICE_URL);
    authnRequest.setProtocolBinding(PROTOCOL_BINDING);
    authnRequest.setIssueInstant(DateTime.now());

    return authnRequest;
}


private void parseRequest(AuthnRequest authnRequest) throws Exception {
    Element responseElement = marshall(authnRequest);
    String responseStr = convertElementToString(responseElement);
    System.out.println("SAML Response is: " + responseStr);
    String base64Response = generateBase64EncodedString(responseStr.getBytes());
    System.out.println("Base 64 Encoded Response: ");
    System.out.println(base64Response);
}

private String generateBase64EncodedString(byte[] bytes) {
    Base64.Encoder base64Enc = Base64.getEncoder();
    return base64Enc.encodeToString(bytes);
}

private String convertElementToString(Element element) {
    Writer outputWriter = new StringWriter();
    XMLHelper.writeNode(element, outputWriter);
    return outputWriter.toString();
}

private Element marshall(AuthnRequest response) throws Exception {
    AuthnRequestMarshaller marshaller = new AuthnRequestMarshaller();
    return marshaller.marshall(response);
}

private Issuer createSAMLIssuer() {
    IssuerBuilder issuerBuilder = new IssuerBuilder();
    Issuer issuer = issuerBuilder.buildObject();
    issuer.setValue(ISSUER);
    return issuer;
}

public static void main(String[] args) throws Exception {
    GenSAMLRequest gen = new GenSAMLRequest();
    AuthnRequest authnRequest = gen.buildSAMLRequest(UUID.randomUUID().toString());
    gen.parseRequest(authnRequest);
}

I am able to resolve this by introducing this line of code - DefaultBootstrap.bootstrap();.

private AuthnRequest buildSAMLRequest(String samlReqID) throws Exception {

    AuthnRequestBuilder authnRequestBuilder = new AuthnRequestBuilder();
    AuthnRequest authnRequest = authnRequestBuilder.buildObject();

    DefaultBootstrap.bootstrap();
    authnRequest.setID(samlReqID);
    authnRequest.setIssuer(createSAMLIssuer());
    authnRequest.setDestination(DESTINATION);
    authnRequest.setSignature(createSAMLSignature());
    authnRequest.setAssertionConsumerServiceURL(ASSERTION_CONSUMER_SERVICE_URL);
    authnRequest.setProtocolBinding(PROTOCOL_BINDING);
    authnRequest.setIssueInstant(DateTime.now());

    return authnRequest;
}


private void parseRequest(AuthnRequest authnRequest) throws Exception {
    Element responseElement = marshall(authnRequest);
    String responseStr = convertElementToString(responseElement);
    System.out.println("SAML Response is: " + responseStr);
    String base64Response = generateBase64EncodedString(responseStr.getBytes());
    System.out.println("Base 64 Encoded Response: ");
    System.out.println(base64Response);
}

private String generateBase64EncodedString(byte[] bytes) {
    Base64.Encoder base64Enc = Base64.getEncoder();
    return base64Enc.encodeToString(bytes);
}

private String convertElementToString(Element element) {
    Writer outputWriter = new StringWriter();
    XMLHelper.writeNode(element, outputWriter);
    return outputWriter.toString();
}

private Element marshall(AuthnRequest response) throws Exception {
    AuthnRequestMarshaller marshaller = new AuthnRequestMarshaller();
    return marshaller.marshall(response);
}

private Issuer createSAMLIssuer() {
    IssuerBuilder issuerBuilder = new IssuerBuilder();
    Issuer issuer = issuerBuilder.buildObject();
    issuer.setValue(ISSUER);
    return issuer;
}

public static void main(String[] args) throws Exception {
    GenSAMLRequest gen = new GenSAMLRequest();
    AuthnRequest authnRequest = gen.buildSAMLRequest(UUID.randomUUID().toString());
    gen.parseRequest(authnRequest);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文