【发布时间】:2020-09-03 00:13:12
【问题描述】:
这不是一个问题,而是我想分享的一个发现,我在上个月在不使用 OpenSAML 库的情况下替换解析 SAML 2.0 响应的逻辑时遇到了一些困难
这里我们需要解码和扩充 SAML 响应。命名空间将相同,因此可用于所有 SAML 响应解析。
我使用了 DOM 解析器。此代码仅用于获取用户名。可以使用相同的方法读取签名并解析签名,我将在另一篇文章中展示。
private String decodeSAMLResponseUserIdandSession(String samlEncoded,
String type) throws IOException, ParserConfigurationException,
SAXException {
byte[] decodedValue = DatatypeConverter.parseBase64Binary(samlEncoded);
String tmlXml = new String(decodedValue, "utf-8");
if (!tmlXml.contains(":Response")) {
System.out.println("Inside the Unzip");
decodedValue = inflateValue(decodedValue);
}
String tmlXml2 = new String(decodedValue, "utf-8");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
dbFactory.setNamespaceAware(true);
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
StringReader strreader = new StringReader(tmlXml2);
InputSource isstream = new InputSource(strreader);
Document doc = dBuilder.parse(isstream);
doc.getDocumentElement().normalize();
NodeList nList = doc.getElementsByTagNameNS("urn:oasis:names:tc:SAML:2.0:assertion", "Subject");
System.out.println("----------------------------");
for (int temp = 0; temp < nList.getLength(); temp++) {
Node nNode = nList.item(temp);
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement1 = (Element) nNode;
System.out.println("First Name : "
+ eElement1.getElementsByTagNameNS("urn:oasis:names:tc:SAML:2.0:assertion", "NameID").item(0).getTextContent().toLowerCase());
}
}
}
private static byte[] inflateValue(byte[] decodedValue) throws IOException {
ByteArrayInputStream bis = new ByteArrayInputStream(decodedValue);
Inflater unzipper = new Inflater(true);
InflaterInputStream zipStream = new InflaterInputStream(bis, unzipper);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
int read = zipStream.read(buf);
while (read > 0) {
bos.write(buf, 0, read);
read = zipStream.read(buf);
}
zipStream.close();
bos.close();
【问题讨论】:
-
只是出于好奇,您为什么选择这样做,而不是使用受信任和维护的软件包?选择 DIY SAML 处理似乎很愚蠢。
-
为了避免这种情况support.oracle.com/knowledge/Middleware/2253462_1.html#FIX 我没有选择更改 EAR 中的 prefer-web-inf 类,因为 EAR 来自第三方,我无权更改它使我的 SAML 插件代码工作。此外,Weblogic 管理员不允许它删除 weblogic 模块中的 openSaml。
-
呃。这听起来像是一场噩梦。甲骨文对破坏事情很有帮助...听起来可能值得看看另一个中间件提供商...编写正确解析 SAML 响应的 DIY 代码当然是可能的,但签名处理变得更加疯狂。祝你好运。
标签: xml saml saml-2.0 opensaml