java SOAP 客户端基本身份验证:HTTP 响应“401:未经授权”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17722277/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 14:46:55  来源:igfitidea点击:

SOAP client basic auth: HTTP response '401: Unauthorized'

javaxmlauthenticationsoap

提问by SzZ

I'm trying to creat a SOAP client that has to call a server that uses http basic authentication. I get the following error:

我正在尝试创建一个 SOAP 客户端,该客户端必须调用使用 http 基本身份验证的服务器。我收到以下错误:

    org.apache.cxf.interceptor.Fault: Could not send Message.
at org.apache.cxf.interceptor.MessageSenderInterceptor$MessageSenderEndingInterceptor.handleMessage(MessageSenderInterceptor.java:64)
...
Caused by: org.apache.cxf.transport.http.HTTPException: HTTP response '401: Unauthorized' when communicating with http://localhost:8080/SpringMVCTest/services/ContractService?wsdl=ContractService.wsdl

My app-config.xml is:

我的 app-config.xml 是:

    <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"  
    xmlns:security="http://www.springframework.org/schema/security"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
    http://www.springframework.org/schema/security  
    http://www.springframework.org/schema/security/spring-security-3.1.xsd">  

    <security:http auto-config="true">  
        <security:intercept-url pattern="/services/*"/>  
        <security:http-basic/>  
    </security:http>  

    <security:authentication-manager>
       <security:authentication-provider>
           <security:user-service>
           <security:user name="wsuser1" password="pw123" authorities="wsuser" />
           </security:user-service>
       </security:authentication-provider>
    </security:authentication-manager>

    <bean id="client" class="hu.bz.ikti.insurance.service.insurer.ContractService"
        factory-bean="clientFactory" factory-method="create"/>

    <bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
        <property name="serviceClass" value="hu.bz.ikti.insurance.service.insurer.ContractService"/>
        <property name="address" value="http://localhost:8080/SpringMVCTest/services/ContractService?wsdl=ContractService.wsdl"/>
    </bean>

</beans>

The http basic auth is configured in the servers web.xml:

http 基本身份验证在服务器 web.xml 中配置:

   <security-constraint>  
    <web-resource-collection>  
      <url-pattern>/services/*</url-pattern>  
    </web-resource-collection>  
    <auth-constraint>  
      <role-name>wsuser</role-name>  
    </auth-constraint>  
  </security-constraint>  
  <login-config>  
    <auth-method>BASIC</auth-method>  
  </login-config>  
  <security-role>  
    <role-name>webservice</role-name>  
  </security-role>

In tomcat-users.xml the user is added:

在 tomcat-users.xml 中添加了用户:

<user username="wsuser1" password="pw123" roles="wsuser"/>

I can open the wsdl in the browser giving username/password. What can cause this 401: Unauthorized error in the client?

我可以在提供用户名/密码的浏览器中打开 wsdl。什么会导致这个 401: Unauthorized error in the client?

回答by Syon

According to the CXF documentation here:

根据此处的 CXF 文档:

https://cxf.apache.org/docs/jax-ws-configuration.html(see Configuring a Spring Client (Option 2))

https://cxf.apache.org/docs/jax-ws-configuration.html(请参阅配置 Spring 客户端(选项 2))

The correct way to set the username and password is by using the usernameand passwordproperties in your clientFactorybean configuration.

设置用户名和密码的正确方法是在bean 配置中使用usernamepassword属性clientFactory

So add these inside your clientFactorybean:

所以在你的clientFactorybean 中添加这些:

<property name="username" value="yourUsername"/>
<property name="password" value="yourPassword"/>