如果你想使用基本的HTTP標頭權威性:
@WebEndpoint(name = "WSHttpBinding_ICustomerService")
public ICustomerService getWSHttpBindingICustomerService() {
WebServiceFeature wsAddressing = new AddressingFeature(true);
ICustomerService service =
super.getPort(new QName("http://xmlns.example.com/services/Customer",
"WSHttpBinding_ICustomerService"), ICustomerService.class,
wsAddressing);
Map<String, Object> context = ((BindingProvider)service).getRequestContext();
Map<String, List<String>> headers = new HashMap<String, List<String>>();
headers.put("Username", Collections.singletonList("yourusername"));
headers.put("Password", Collections.singletonList("yourpassword"));
return service;
}
如果服務使用NTLM
(Windows身份驗證)(解釋here):
@WebEndpoint(name = "WSHttpBinding_ICustomerService")
public ICustomerService getWSHttpBindingICustomerService() {
WebServiceFeature wsAddressing = new AddressingFeature(true);
ICustomerService service =
super.getPort(new QName("http://xmlns.example.com/services/Customer",
"WSHttpBinding_ICustomerService"), ICustomerService.class,
wsAddressing);
NtlmAuthenticator auth = new NtlmAuthenticator(username, password);
Authenticator.setDefault(auth);
return service;
}
有沒有這個曾經自己,但看過其他用途吧:
@WebEndpoint(name = "WSHttpBinding_ICustomerService")
public ICustomerService getWSHttpBindingICustomerService() {
WebServiceFeature wsAddressing = new AddressingFeature(true);
ICustomerService service =
super.getPort(new QName("http://xmlns.example.com/services/Customer",
"WSHttpBinding_ICustomerService"), ICustomerService.class,
wsAddressing);
Map<String, Object> context = ((BindingProvider)service).getRequestContext();
context.put(BindingProvider.USERNAME_PROPERTY, "yourusername");
context.put(BindingProvider.PASSWORD_PROPERTY, "yourpassword");
return service;
}
謝謝!您的第一個示例中的變量標題已設置但未使用。有一些缺失。 –