java - 如何在 Java 中正确解码 SAML 请求(HTTP 重定向)?

标签 java saml http-redirect inflate

我正在使用 HTTP 重定向绑定(bind)处理 SAML 请求。 我在另一篇文章中读到,需要执行以下步骤才能检索 SAML 请求的原始内容(URL 中的 SAMLRequest 参数):

  1. 网址解码
  2. Base64 解码
  3. 夸大内容

虽然这些步骤对我来说很清楚,但我无法获取 XML 格式的 SAML 请求。我相信错误出在第三步,也许有不止一种方法来膨胀字节?这是执行上述三个函数的 Java 函数,给定的参数是 URL 中 SAML 参数的值。

private String decodeMessage(String SAMLContent) {
        try {
            //URLDecode, Base64 and inflate data

            //URLDecode
            SAMLContent = URLDecoder.decode(SAMLContent, "UTF-8");

            //Base64 decoding
            SAMLContent = new String(Base64.getDecoder().decode(SAMLContent), "UTF-8");

            //Inflating data
            try {
                byte[] compressed = new byte[10 * SAMLContent.getBytes().length];
                Inflater i = new Inflater(true);
                i.setInput(SAMLContent.getBytes(), 0, SAMLContent.getBytes().length);
                int finalSize = i.inflate(compressed);
                //Exception is thrown here
                SAMLContent = new String(SAMLContent.getBytes(), 0, finalSize, "UTF-8");
                i.end();


            } catch (DataFormatException ex) {
                JOptionPane.showMessageDialog(null, "DFE: " + ex.getMessage());
            }

        } catch (UnsupportedEncodingException ex) {
            JOptionPane.showMessageDialog(null, "UEE: " + ex.getMessage());
        }

        return SAMLContent;

    }

如果我复制并粘贴第一步的输出 here ,我可以在页面底部看到格式良好的 XML,因此至少 URL 解码按预期工作。 如果您有任何解决方案,请告诉我,谢谢。

最佳答案

我就是这样做的。流程是检测请求是 HTTP-Redirect,对请求进行 base64 解码,然后对其进行 inflate。以下链接是在 github 中完成所有这些操作的代码。

Receive the request

Decode the request

Inflate the XML

如果你得到

Incorrect header check

检查这个answer

您可能需要将 inflate 代码更改为:

return new String(inflatedData, 0, inflatedBytesLength, "UTF-8");

关于java - 如何在 Java 中正确解码 SAML 请求(HTTP 重定向)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60801918/

相关文章:

ssl - 使用 CN 和主题备用名称与 UPN 不同的 ADFS 客户端证书身份验证

rest - 嵌入式 Jetty : In secure https server, ContextHandler 重定向到 http URI

django - 使用 HttpResponseRedirect,但浏览器未显示正确的 URL

java - 垃圾收集工作正常进行

saml - 为什么 SAML 服务提供商应该信任 SAML 响应?

azure-active-directory - Azure AD 与 sonarqube 集成

asp.net - 将旧的 asp 文件重定向到新的 aspx (asp.net) 文件(永久重定向,SEO)

Java LinkedList 删除最后一个节点

java - 使用 FXMLLoader 时出现 IllegalAccessError

java - Collection的reverseOrder方法的时间复杂度