这不是问题,这是我想分享的发现,我上个月在不使用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();
使用OpenSAML库没有问题,因为Oracle问题指出那里使用了多个版本的OpenSAML,例如:2.0.0和3.0.0。因此,只要您坚持使用一个版本,就不会使用opensaml jar。希望这对您有所帮助。