【问题标题】:Tomcat HTTP Base Authentification and JAX-WS: Client Generation failsTomcat HTTP 基本身份验证和 JAX-WS:客户端生成失败
【发布时间】: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


    【解决方案1】:

    好的,我找到了一种方法来使用Authenticator,它必须在查询端口之前设置:

        if (aUser != null && !aUser.equals("") && aPassword != null && !aPassword.equals(""))
        {
            Authenticator.setDefault(getDefaultAuthenticator(aUser, aPassword));
        }
    
        service = new PortaladapterWebservice(new URL(mUrl)).getPortaladapterWebservicePort();
        ((BindingProvider) service).getRequestContext().put(BindingProviderProperties.CONNECT_TIMEOUT, 60000);
    

    其中getDefaultAuthenticator()方法定义为:

    private Authenticator getDefaultAuthenticator (String aUser, String aPassword)
        {
            return new Authenticator() 
            {
                @Override
                protected PasswordAuthentication getPasswordAuthentication()
                {
                    return new PasswordAuthentication(aUser, aPassword.toCharArray());
                }
            };
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-12-28
      • 1970-01-01
      • 1970-01-01
      • 2015-04-27
      • 2017-03-02
      • 1970-01-01
      • 1970-01-01
      • 2019-04-16
      相关资源
      最近更新 更多