2015-10-08 38 views
0

我必須向安全的Web服務發送SOAP請求,並向我提供了base64格式的客戶端證書。我如何使用它來保護對Java中的Web服務的調用?我正在使用CXF客戶端和Grails 1.3.7。我希望能夠以這樣的加密或證書添加到我的SOAP請求:使用base64證書的WSS4JInInterceptor

Map<String, Object> outProps = new HashMap<String, Object>(); 
outProps.put(WSHandlerConstants.ENC_PROP_FILE, "client-keystore.properties"); 
outProps.put(WSHandlerConstants.ENC_KEY_ID, "DirectReference"); 
outProps.put(WSHandlerConstants.ACTION, WSHandlerConstants.ENCRYPT); 
outProps.put(WSHandlerConstants.ENCRYPTION_USER, "myAlias"); 
outProps.put(WSHandlerConstants.ENC_SYM_ALGO, "http://www.w3.org/2001/04/xmlenc#aes128-cbc"); 
outProps.put(WSHandlerConstants.ENC_KEY_TRANSPORT, "http://www.w3.org/2001/04/xmlenc#rsa-1_5"); 
WSS4JOutInterceptor wssOut = new WSS4JOutInterceptor(outProps); 
client.getEndpoint().getOutInterceptors().add(wssOut); 

使Web服務可以驗證我的請求。我不太熟悉在soap消息上使用攔截器。

回答

0

您可以BASE-64解碼證書,並將其導入到您在client-keystore.properties中引用的密鑰庫中。

+0

如果base64證書爲pfx格式,該怎麼辦? – aeris

0

我想出了另一個解決方案。我做的第一件事就是閱讀從屬性文件中的Base64字符串,分析它,並保存到一個PFX文件:

// certStr = base64 string from properties file 
byte[] cert = DatatypeConverter.parseBase64Binary(certStr); 
FileOutputStream fos = new FileOutputStream(new File("C:/mycert.pfx")); 
fos.write(cert); 
fos.flush(); 
fos.close(); 

然後用它作爲一個密鑰:

// keystore file 
File keyFile = new File("C:/mycert.pfx"); 
KeyStore keyStore = KeyStore.getInstance("PKCS12"); 
InputStream keyInput = new FileInputStream(keyFile); 
keyStore.load(keyInput, pKeyPassword.toCharArray()); 
keyInput.close(); 
KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance("SunX509"); 
keyManagerFactory.init(keyStore, pKeyPassword.toCharArray()); 
KeyManager[] km = keyManagerFactory.getKeyManagers(); 
SSLContext context = SSLContext.getInstance("TLS"); 
context.init(km, null, new java.security.SecureRandom()); 
SSLSocketFactory sslFactory = context.getSocketFactory(); 
conn.setSSLSocketFactory(sslFactory); 

證書添加到我的SOAP請求,我不必加密請求。參考文獻here