【问题标题】:SOAP client basic auth: HTTP response '401: Unauthorized'SOAP 客户端基本身份验证:HTTP 响应“401:未经授权”
【发布时间】:2013-07-17 08:00:21
【问题描述】:

我正在尝试创建一个 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

我的 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>

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>

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

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

我可以在提供用户名/密码的浏览器中打开 wsdl。 什么会导致客户端出现这个 401: Unauthorized 错误?

【问题讨论】:

    标签: java xml authentication soap


    【解决方案1】:

    根据此处的 CXF 文档:

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

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

    所以将这些添加到你的 clientFactory bean 中:

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

    【讨论】:

    • 没有安全性:前缀我得到一个 xml 解析错误:org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException org.xml.sax.SAXParseException: cvc-complex-type.2.4.a: Invalid content was found starting with element 'user' 我添加了基于 web 示例的前缀。也许我需要一个 xsi:schemaLocation 用于用户(可能用于身份验证等)元素。
    • 根本没有说要触摸安全前缀。您需要将属性添加到您的 clientFactory bean。
    • @Syon。这是一个非常有用的建议。我为此挣扎了一天,然后找到了这个答案,瞧,它就像一个魅力。谢谢!
    猜你喜欢
    • 2014-08-24
    • 1970-01-01
    • 1970-01-01
    • 2011-06-22
    • 1970-01-01
    • 2015-07-16
    • 1970-01-01
    • 2018-07-09
    • 2012-04-10
    相关资源
    最近更新 更多