我有通过 https 进行通信的 java 服务。我想从 C# 连接到此服务。
我遇到了这个异常:
System.ServiceModel.FaultException: WSDoAllReceiver:传入消息确实 不包含所需的安全标头。
有人知道出了什么问题吗?
C#代码:
EndpointAddress address = new EndpointAddress(
new Uri("https://JavaStore:8443/JavaStore/services/B2BService"),
EndpointIdentity.CreateDnsIdentity("JavaStore"),
new AddressHeaderCollection()
);
BasicHttpBinding binding = new BasicHttpBinding();
binding.Security.Mode = BasicHttpSecurityMode.Transport;
var client = new ReharB2BService.B2BServicePortTypeClient(binding, address);;
client.Open();
client.getAllItems();
查看这篇文章,了解如何在 WCF 调用中包含安全标头(该示例针对 Java 托管 Web 服务):http://isyourcode.blogspot.com/2010/05/using-oasis-username-token-profile-在.html
如果您使用 Spring Web 服务,则需要添加 securityInterceptor。
@Configuration
public class Config {
@Value("${client.default-uri}")
private String defaultUri;
@Value("${client.user.name}")
private String userName;
@Value("${client.user.password}")
private String userPassword;
@Bean
public Jaxb2Marshaller marshaller() {
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
marshaller.setContextPath("com.example.eppmsoapclient");
return marshaller;
}
@Bean
public Wss4jSecurityInterceptor securityInterceptor() {
Wss4jSecurityInterceptor security = new Wss4jSecurityInterceptor();
security.setSecurementActions("UsernameToken");
security.setSecurementUsername(userName);
security.setSecurementPassword(userPassword);
security.setSecurementPasswordType("PasswordText");
return security;
}
@Bean
public SOAPClient soapClient(Jaxb2Marshaller marshaller) {
SOAPClient client = new SOAPClient();
client.setDefaultUri(defaultUri);
client.setMarshaller(marshaller);
client.setUnmarshaller(marshaller);
client.setInterceptors(new ClientInterceptor[]{ securityInterceptor() });
return client;
}
}
public class SOAPClient extends WebServiceGatewaySupport {
public Response doExecute(Request request) {
Response response = (Response) getWebServiceTemplate()
.marshalSendAndReeive(request);
return response;
}
}
所需的依赖项:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web-services</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.ws</groupId>
<artifactId>spring-ws-security</artifactId>
</dependency>