2013-06-21 39 views
0

我正在開發需要身份驗證和xml簽名的webservice客戶端。 我讀過很多文章,但看起來像我看起來不一樣。具有身份驗證的Java WebService調用

我需要發送一個包含帶有一些證書詳細信息的標籤的請求。 我從服務提供商幾個文件(certificate.crt,certificate.p12,certificate.pem)接收

我設法使用CRT文件附加到請求follString的providerName =

System.getProperty("jsr105Provider", "org.jcp.xml.dsig.internal.dom.XMLDSigRI"); 

     XMLSignatureFactory fac = XMLSignatureFactory.getInstance("DOM", (Provider) Class.forName(providerName).newInstance()); 

     Reference ref = fac.newReference("", fac.newDigestMethod(
       DigestMethod.SHA1, null), Collections.singletonList(fac 
       .newTransform(Transform.ENVELOPED, (XMLStructure) null)), null, 
       null); 

     // Create the SignedInfo 
     SignedInfo si = fac.newSignedInfo(
       fac.newCanonicalizationMethod(CanonicalizationMethod.EXCLUSIVE_WITH_COMMENTS, (C14NMethodParameterSpec) null), 
       fac.newSignatureMethod(SignatureMethod.RSA_SHA1, null), Collections.singletonList(ref)); 

     KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA"); 
     kpg.initialize(512); 
     KeyPair kp = kpg.generateKeyPair(); 

     KeyInfoFactory kif = fac.getKeyInfoFactory(); 
     KeyValue kv = kif.newKeyValue(kp.getPublic()); 

     CertificateFactory cf = CertificateFactory.getInstance("X.509"); 
     Certificate cert = cf.generateCertificate(new FileInputStream(new File("certificate.crt"))); 

     X509Data x509d = kif.newX509Data(Collections.singletonList(cert)); 
     KeyInfo ki = kif.newKeyInfo(Arrays.asList(x509d, kv)); 

     Document doc = (Document) result.getNode(); 

     DOMSignContext dsc = new DOMSignContext(kp.getPrivate(), doc.getDocumentElement()); 

     XMLSignature signature = fac.newXMLSignature(si, ki); 
     signature.sign(dsc); 

但是生成RSA密鑰和不同的每個電話。很明顯,因爲我使用KeyPairGenerator。是否有可能從P12文件導入我的私鑰?

當我運行在Tomcat這個應用程序,我需要以某種方式將其配置爲啓用HTTPS調用的服務?

回答

0

您可以使用KeySotre

加載文件:

KeyStore ks = KeyStore.getInstance("PKCS12"); 
FileInputStream ksin = new FileInputStream("myfile.p12"); 
ks.load(ksin, "password"); 
getKey("keyalis", "password");