简体   繁体   中英

Generate BinarySecurityToken from .pfx certificate

I'm receiving a request from the client and after some data masking, I forward the request to a web services. In order for the webservice to authorize me, I have to send in the < wsse:BinarySecurityToken > element. I have a .pfx certificate file and from this certificate, I need to generate the security token. The remaining part of the request is generated through SAAJ

<wsse:BinarySecurityToken>my security token</wsse:BinarySecurityToken>

How to generate "my security token" from .pfx file?

Demo code :

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())

The token variable doesn't seem to match with the token generated from SOAPUI. Any help is very much appreciated. Thank You!

Change

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

to

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM