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

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

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

  1. URL解码
  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解码可以按预期进行。如果您有任何解决方案,请告诉我,谢谢。

java saml http-redirect inflate
1个回答
1
投票

这就是我的做法。流程是检测到请求是HTTP重定向的,base64对该请求进行解码然后将其膨胀。以下链接是在github中完成所有这些操作的代码。

Receive the request

Decode the request

Inflate the XML

如果有

错误的标题检查

检查此answer

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

return new String(inflatedData, 0, inflatedBytesLength, "UTF-8");
© www.soinside.com 2019 - 2024. All rights reserved.