简体   繁体   English

带有身份验证的Java WebService调用

[英]Java WebService call with authentication

I'm working on webservice client which requires authentication and xml signing. 我正在使用需要身份验证和xml签名的Web服务客户端。 I've read a lot of articles but it looks like my one looks different. 我读了很多文章,但看起来好像与我不同。

I need to send a request containing tag with some certificate details. 我需要发送包含带有一些证书详细信息的标签的请求。 I received from Service provider few files (certificate.crt , certificate.p12 , certificate.pem) 我从服务提供商那里收到了一些文件(certificate.crt,certificate.p12,certificate.pem)

I managed to attach crt file into request using follString providerName = 我设法使用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);

But RSA key is generated and different each call. 但是会生成RSA密钥,并且每个调用不同。 It is obvious because I'm using KeyPairGenerator. 很明显,因为我正在使用KeyPairGenerator。 Is it possible to import my private key from P12 file instead? 是否可以从P12文件导入我的私钥?

When I run this application on tomcat do I need to configure it somehow to enable the HTTPS calls to the service? 当我在tomcat上运行此应用程序时,是否需要以某种方式对其进行配置以启用对该服务的HTTPS调用?

You can use KeySotre 您可以使用KeySotre

Load the file: 加载文件:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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