【发布时间】:2019-12-10 13:41:29
【问题描述】:
我正在尝试向JAX-WS 网络服务发送请求,该服务受 HTTP 基本身份验证保护。服务的web.xml文件包含以下语句:
<security-constraint>
<web-resource-collection>
<web-resource-name>secured services</web-resource-name>
<url-pattern>/services/PortalWebservice/*</url-pattern>
</web-resource-collection>
<auth-constraint>
<role-name>onlineadapter</role-name>
</auth-constraint>
</security-constraint>
<security-role>
<role-name>onlineadapter</role-name>
</security-role>
<login-config>
<auth-method>BASIC</auth-method>
<realm-name>onlineadapter</realm-name>
</login-config>
我的 tomcat-user.xml 包含这个:
<role rolename="onlineadapter"/>
<user username="onlineadapter" password="onlineadapter" roles="onlineadapter"/>
然后我尝试在运行时创建一个客户端,如下所示:
int timeOut = 60000;
IPortaladapterWebservice service = new PortaladapterWebservice(new URL("http://localhost:8080/OnlineAdapter/services/PortaladapterWebservice")).getPortaladapterWebservicePort();
((BindingProvider) service).getRequestContext().put(BindingProviderProperties.CONNECT_TIMEOUT, timeOut);
if (aUser != null && !aUser.equals("") && aPassword != null && !aPassword.equals(""))
{
((BindingProvider) service).getRequestContext().put(BindingProvider.USERNAME_PROPERTY, "onlineadapter");
((BindingProvider) service).getRequestContext().put(BindingProvider.PASSWORD_PROPERTY, "onlineadapter");
}
问题:每当我调用getPortaladapterWebservicePort 时,我都会收到以下错误:
com.sun.xml.ws.wsdl.parser.InaccessibleWSDLException: 2 counts of InaccessibleWSDLException.
java.io.IOException: Server returned HTTP response code: 401 for URL: http://localhost:8080/OnlineAdapter/services/PortaladapterWebservice
java.io.IOException: Server returned HTTP response code: 401 for URL: http://localhost:8080/OnlineAdapter/services/PortaladapterWebservice?wsdl
我猜状态码 401 与我的客户未获得请求授权有关。那么为什么在我什至能够设置用户和密码之前出现错误?这在JAX-WS 客户端中是如何完成的?
【问题讨论】:
标签: java tomcat jax-ws basic-authentication