【发布时间】:2020-02-13 13:56:42
【问题描述】:
我需要使用第三方网络服务。我有一个 WSDL 文件和 .pkcs12 密钥库文件和密码。 使用该 WSDL 文件,我在项目中添加了 Web 引用。 读取密钥库文件。 创建 X509certificate2 类的新实例,并在添加到服务类后导入证书。我正在尝试在我的服务中调用方法
service.mymethod(param1)--> (At this line its throwing error stating that ws-security header not found)
通过谷歌搜索我发现的错误 StackOverflow 链接以添加安全标头 在点击该链接之后是完整的代码
//reading PCKS12 certificate
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
var path = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Content\\myKeyStoreFile.pkcs12");
var data = System.IO.File.ReadAllBytes(path);
//Importing Certificate
X509Certificate2 certificate = new X509Certificate2();
certificate.Import(data, "password", X509KeyStorageFlags.DefaultKeySet);
//adding WS-Security Headers
UsernameToken token = new UsernameToken("keyname", "password", PasswordOption.SendHashed);
service.RequestSoapContext.Security.Tokens.Add(token);
//adding certificate to service
service.ClientCertificates.Add(certificate);
//calling proxy class(service method)
service.methodname(param1);-->(its throwing System.web.service.protocols.soapheaderexception:'nested exception is org.apache.wss4j.common.ext.WSSSecurityException Original Exception was javax.security.auth.callback.unsupportedcallbackexception)
我有一个 java 代码(在 spring boot 中实现)供参考。 Wss4jSecurityInterceptor securityInterceptor = new Wss4jSecurityInterceptor();
//crypto varible contains .pkcs12 file path and password properties
Crypto crypto = null;
try {
crypto = CryptoFactory.getInstance(cryptoPropertyFile);
}catch(WSSecurityException e) {
e.printStackTrace();}
securityInterceptor.setSecurementActions("Encrypt Signature");
securityInterceptor.setSecurementEncryptionUser(trustedCertKeyAlias);
securityInterceptor.setSecurementEncryptionCrypto(crypto);
securityInterceptor.setSecurementEncryptionParts("{Content {http://schemas.xmlsoap.org/soap/envelope/}Body");
securityInterceptor.setSecurementUsername(privateKeyAlias);
securityInterceptor.setSecurementPassword(privateKeyPassword);
securityInterceptor.setSecurementSignatureCrypto(crypto);
securityInterceptor.setSecurementSignatureKeyIdentifier("DirectReference");
securityInterceptor.setSecurementSignatureUser(privateKeyAlias);
securityInterceptor.setSecurementSignatureParts("{Content}{http://schemas.xmlsoap.org/soap/envelope/}Body");
securityInterceptor.setValidationActions("Encrypt");
securityInterceptor.setValidationDecryptionCrypto(crypto);
KeyStoreCallbackHandler keyStoreCallbackHandler = new KeyStoreCallbackHandler();
keyStoreCallbackHandler.setPrivateKeyPassword(privateKeyPassword);
securityInterceptor.setValidationCallbackHandler(keyStoreCallbackHandler);
LogHttpHeaderClientInterceptor logHttpHeaderClientInterceptor = new LogHttpHeaderClientInterceptor();
ClientInterceptor[] interceptors = {securityInterceptor, logHttpHeaderClientInterceptor};
template.setInterceptors(interceptors);
谁能告诉我如何在 dotnet 中添加拦截器。我做了一些研究,但找不到任何解决方案。 dotnet 中是否有与 Wss4jSecurityInterceptor 类似的东西。
【问题讨论】:
标签: java c# asp.net security ws-security