我正在使用 HTTP 重定向绑定(bind)处理 SAML 请求。 我在另一篇文章中读到,需要执行以下步骤才能检索 SAML 请求的原始内容(URL 中的 SAMLRequest 参数):
- 网址解码
- Base64 解码
- 夸大内容
虽然这些步骤对我来说很清楚,但我无法获取 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 中完成所有这些操作的代码。
如果你得到
Incorrect header check
检查这个answer
您可能需要将 inflate 代码更改为:
return new String(inflatedData, 0, inflatedBytesLength, "UTF-8");
关于java - 如何在 Java 中正确解码 SAML 请求(HTTP 重定向)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60801918/