带有身份验证的Java Web Service调用

时间:2013-06-21 07:55:20

标签: java xml tomcat webservice-client

我正在处理需要身份验证和xml签名的webservice客户端。 我读过很多文章,但看起来我的文章看起来不一样了。

我需要发送包含标签的请求,其中包含一些证书详细信息。 我从服务提供商那里收到了几个文件(certificate.crt,certificate.p12,certificate.pem)

我设法使用follString providerName =

将crt文件附加到请求中
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调用?

1 个答案:

答案 0 :(得分:0)

您可以使用KeySotre

加载文件:

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