Help Center/ Partner Center/ Developer Guide/ Appendix/ Sample Code for Parsing the SAMLRequest
Updated on 2022-05-20 GMT+08:00

Sample Code for Parsing the SAMLRequest

  • Sample code of the "URL decoding > decoding with Base64 > decompressing (zip.inflate)" process
    package org.apache;
    import java.io.*;
    import org.opensaml.xml.util.Base64;
    import java.util.zip.InflaterInputStream;
    import java.util.zip.Inflater;
    class SamlRequestDecoder {
    	public static void main(String[] args) throws UnsupportedEncodingException {
    		String samlRequest = "hZJBb9swDIX%2Fiqa77dho0kyIU2QNigboNq9xd9hlkGWm1ipLsSjF7b%2Bv7KRbhwHZlSK%2FR73HxdVzq8gBLEqjc5rGE0pAC1NL%2FZjTh%2FImmtOr5QJ5q7I9W3nX6HvoPKAjYVAjO77k1FvNDEeJTPMWkDnBtqvPdyyLJ2xvjTPCKEpWiGBdkLo2Gn0Ldgv2IAU83N%2FltHFujyxJeFCJG897kEIZX8fCtGPRy2SQSwbwPyRK1mErqbkbfzLAAqvpXajG07SHauT4MJaEhWovXDLiEwHaefuSgMAPyjxKfdvHXAwYSm6MFTB%2BO6c7rhAo2axz%2BtM12ZOqZ7rreDdT1aHqU9F1T5Wqs%2FYSm19TefkcWrHgiPIAf4YRPWw0Oq5dTrNJOo%2FSNMpm5eQjS1M2nccXF7MflBQnyz5JfYzinL%2FVsQnZbVkWUfF1W1Ly%2FS3S0EBPAbJR3b5P7jyYv5lMl2fDWSTv%2Bb%2FP5UsAbtaFUVK8kJVSpr%2B2wF1ww1kPo7ktd%2BdXGCqyjnZjK3OWa5QhL0q2xYD%2F5rmSOwn2P%2BdDk%2BVpyb%2BvePkK";
    		byte[] decodedBytes = Base64.decode(java.net.URLDecoder.decode(samlRequest, "utf-8"));
    		try {
    			ByteArrayInputStream bytesIn = new ByteArrayInputStream(decodedBytes);
    			InflaterInputStream in = new InflaterInputStream(bytesIn, new Inflater(true));
    			byte[] buffer = new byte[decodedBytes.length];
    			ByteArrayOutputStream out = new ByteArrayOutputStream();
    			for (int bytesRead = 0; bytesRead != -1; bytesRead = in.read(buffer)) {
    				out.write(buffer, 0, bytesRead);
    			}
    			System.out.println(new String(out.toByteArray(), "UTF-8"));
    		}
    		catch (Exception e) {
    		}
    	}
    }
  • The parsed samlRequest.xml content is similar to the following:
    <?xml version="1.0" encoding="UTF-8"?>
    <saml2p:AuthnRequest xmlns:saml2p="urn:oasis:names:tc:SAML:2.0:protocol" AssertionConsumerServiceURL="https://auth.huaweicloud.com/authui/saml/SAMLAssertionConsumer" Destination="https://avatar.huawei.com/nidp/saml2/sso" ForceAuthn="false" ID="_dck4mm08qmdhc8k4nuir07hghetdqqg8umg5" IsPassive="false" IssueInstant="2018-11-20T03:19:38.368Z" ProtocolBinding="urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST" Version="2.0">
        <saml2:Issuer xmlns:saml2="urn:oasis:names:tc:SAML:2.0:assertion">https://auth.huaweicloud.com/</saml2:Issuer>
        <saml2p:NameIDPolicy AllowCreate="true" Format="urn:oasis:names:tc:SAML:2.0:nameid-format:transient" SPNameQualifier="https://auth.huaweicloud.com/" />
    </saml2p:AuthnRequest>