我正在尝试用 Java 对 Saml 2.0 请求进行编码,但是当我使用在线工具对其进行解码时,我得到了奇怪的字符。
这是输入字符串:
<samlp:AuthnRequest
xmlns:samlp="urn:oasis:names:tc:SAML:2.0:protocol"
ID="_8d7bd828-6f91-477a-b158-22d693f56972"
Version="2.0"
IssueInstant="2013-04-19T14:07:53Z"
AssertionConsumerServiceURL="http://test">
<saml:Issuer xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion">
test
</saml:Issuer>
<samlp:NameIDPolicy
Format="urn:oasis:names:tc:SAML:2.0:nameid-format:unspecified"
AllowCreate="true" />
</samlp:AuthnRequest>
这是我正在使用的代码:
byte[] xmlBytes = baos.toByteArray();
//Deflate
ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();
DeflaterOutputStream deflaterOutputStream = new DeflaterOutputStream(
byteOutputStream);
try {
deflaterOutputStream.write(xmlBytes, 0, xmlBytes.length);
deflaterOutputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//BASE64 encode
Base64 base64Encoder = new Base64();
byte[] base64EncodedByteArray = base64Encoder.encode(xmlBytes);
String base64EncodedMessage = new String(base64EncodedByteArray);
// URL encode
String urlEncodedMessage = null;
try {
urlEncodedMessage = URLEncoder.encode(base64EncodedMessage,"utf-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return urlEncodedMessage;
当我通过在线解码工具运行生成的字符串时,我得到以下结果:
x?}?Mk?@??J????ILL@??`?0??^"??B???Gm?}W?`/^?03????,,????
?=Z}??pT??[iA?-8??q
?I
??N
?h???GYN?\pZ?"?Y:??Pt9mS??<-D?$zCc?V
Ah?A??+Zq??8??|?d??$ZZ???J+?4
?/)?u????Kf?d>9??2.cf?-X?<?&??zq?K??A?O???ImZ??`76W???n?~???6
w?m?????
^???$????ie?;??3-I??k??7????Srtion">
test
</saml:Issuer>
<samlp:NameIDPolicy
Format="urn:oasis:names:tc:SAML:2.0:nameid-format:unspecified"
AllowCreate="true" />
</samlp:AuthnRequest>