使用 cxf 客户端发送 BASIC 身份验证凭据
Send BASIC auth credentials using cxf client
我正在使用 cxf 网络服务客户端发送身份验证凭据,它说:
javax.xml.ws.WebServiceException: Could not send Message.
Caused by: org.apache.cxf.transport.http.HTTPException: HTTP response
'401: Unauthorized' when communicating with
http://localhost:8080/AccountFacadeService/AccountService
我的客户是:
QName SERVICE_NAME = new QName("http://webservice.account.com/", "AccountFacadeService");
URL WSDL_LOCATION = http://localhost:8080/AccountFacadeService/AccountService?wsdl;
AccountFacadeService stub = new AccountFacadeService(WSDL_LOCATION, SERVICE_NAME);
AccountService port = stub.getAccountServicePort();
((BindingProvider) port).getRequestContext().put(BindingProvider.USERNAME_PROPERTY, "user");
((BindingProvider) port).getRequestContext().put(BindingProvider.PASSWORD_PROPERTY, "pass");
我还缺少 headers 的其他内容吗?
好吧,经过数小时的深入研究,我终于找到了问题的答案。
- 确保正确配置了 glassfish 服务器控制台安全领域中的
role
,并在 glassfish-ejb-jar.xml
中维护了相同的内容
- 身份验证凭据的传递方式与我在 post 中传递的方式相同。有时客户端无法为您创建 header 您可以尝试使用密码验证器。
final String username = "user";
final String password = "pass";
Authenticator.setDefault(new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(
username,
password.toCharArray());
}
});
我正在使用 cxf 网络服务客户端发送身份验证凭据,它说:
javax.xml.ws.WebServiceException: Could not send Message.
Caused by: org.apache.cxf.transport.http.HTTPException: HTTP response '401: Unauthorized' when communicating with http://localhost:8080/AccountFacadeService/AccountService
我的客户是:
QName SERVICE_NAME = new QName("http://webservice.account.com/", "AccountFacadeService");
URL WSDL_LOCATION = http://localhost:8080/AccountFacadeService/AccountService?wsdl;
AccountFacadeService stub = new AccountFacadeService(WSDL_LOCATION, SERVICE_NAME);
AccountService port = stub.getAccountServicePort();
((BindingProvider) port).getRequestContext().put(BindingProvider.USERNAME_PROPERTY, "user");
((BindingProvider) port).getRequestContext().put(BindingProvider.PASSWORD_PROPERTY, "pass");
我还缺少 headers 的其他内容吗?
好吧,经过数小时的深入研究,我终于找到了问题的答案。
- 确保正确配置了 glassfish 服务器控制台安全领域中的
role
,并在glassfish-ejb-jar.xml
中维护了相同的内容
- 身份验证凭据的传递方式与我在 post 中传递的方式相同。有时客户端无法为您创建 header 您可以尝试使用密码验证器。
final String username = "user";
final String password = "pass";
Authenticator.setDefault(new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(
username,
password.toCharArray());
}
});