从.pfx证书生成BinarySecurityToken

时间:2014-02-21 20:44:28

标签: java ws-security

我收到了客户端的请求,在进行了一些数据屏蔽后,我将请求转发给了Web服务。为了让web服务授权我,我必须发送< wsse:BinarySecurityToken>元件。我有一个.pfx证书文件,从这个证书中,我需要生成安全令牌。请求的剩余部分是通过SAAJ

生成的
<wsse:BinarySecurityToken>my security token</wsse:BinarySecurityToken>

如何从.pfx文件生成“我的安全令牌”?

演示代码:

InputStream inStream = new FileInputStream("C:\\development\\certs\\cert.pfx");
KeyStore ks = KeyStore.getInstance("PKCS12");
ks.load(inStream, "PASSWORD".toCharArray());
Enumeration<String> aliases = ks.aliases();
String aliaz = "";
while(aliases.hasMoreElements()){
 aliaz = aliases.nextElement();
 if(ks.isKeyEntry(aliaz)){
      break;
 }
}
X509Certificate certificate = (X509Certificate) ks.getCertificate(aliaz);
Base64 base64 = new Base64();
String token = base64.encodeToString(certificate.getSignature())

令牌变量似乎与从SOAPUI生成的令牌不匹配。很感谢任何形式的帮助。谢谢!

1 个答案:

答案 0 :(得分:3)

更改

String token = base64.encodeToString(certificate.getSignature());

String token = base64.encodeToString(certificate.getEncoded());