Spring WS“安全令牌无法通过身份验证或授权”

时间:2012-11-17 01:32:33

标签: spring-security soapui spring-ws soap-client

我正在使用SpringWS-Security为Web服务创建一个Java客户端使用者。

我的请求SOAP(我在SOAP UI中使用)

<soapenv:Envelope 
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
         xmlns:sch="http://myws.mycompany.com/myws/schema"> 

    <soapenv:Header>
        <wsse:Security soapenv:mustUnderstand="1" 
            xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">

            <wsse:UsernameToken xmlns:wsu="http://schemas.xmlsoap.org/ws/2003/06/utility">
                    <wsse:Username>myUsernameString</wsse:Username>
                    <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">123</wsse:Password>
            </wsse:UsernameToken>
        </wsse:Security>
        </soapenv:Header>

    <soapenv:Body>
        <sch:GetUserDetails idSender="5"/>
    </soapenv:Body>

</soapenv:Envelope>

我在WS中的servlet.xml。

<bean name="endpointMapping"
  class="org.springframework.ws.server.endpoint.mapping.PayloadRootQNameEndpointMapping">

  <property name="interceptors">
   <list>
    <ref local="wsSecurityInterceptor" />
   </list>
 </property>


    <bean id="wsSecurityInterceptor"
                        class="org.springframework.ws.soap.security.wss4j.Wss4jSecurityInterceptor">
        <property name="validationActions" value="UsernameToken" />
        <property name="validationCallbackHandler" ref="springSecurityCallbackHandler" />
    </bean>

    <bean id="springSecurityCallbackHandler"
                        class="org.springframework.ws.soap.security.wss4j.callback.SpringPlainTextPasswordValidationCallbackHandler">
        <property name="authenticationManager" ref="authenticationManager"/>
    </bean>

    <bean id="authenticationProvider" class="ws.security.CustomAuthenticationProviderImpl">
            <property name="userCommonService" ref="userCommonService" />
        <security:custom-authentication-provider/>
    </bean>

    <security:authentication-manager alias="authenticationManager" />.

在我的Java客户端 - applicationContext.xml

<bean name="webserviceTemplate" class="org.springframework.ws.client.core.WebServiceTemplate">
        <property name="defaultUri" value="http:/localhost:8080/myws-ws/" />
        <property name="marshaller" ref="marshaller" />
        <property name="unmarshaller" ref="unmarshaller" />
        <property name="interceptors">
            <list>
                <ref local="wsSecurityInterceptor" />
            </list>
        </property>
    </bean>

    <oxm:jaxb2-marshaller id="marshaller"
        contextPath="org.example.bean.schema" />
    <oxm:jaxb2-marshaller id="unmarshaller"
        contextPath="org.example.org.bean.schema" />

    <bean id="client" class="example.client.impl.EfactClientImpl">
        <property name="webServiceTemplate" ref="webserviceTemplate" />
    </bean>

     <bean id="wsSecurityInterceptor" class="org.springframework.ws.soap.security.wss4j.Wss4jSecurityInterceptor">
        <property name="securementActions" value="UsernameToken"/>
    </bean>

当我使用SOAP UI来使用服务时,一切都很顺利,我想我需要在Java客户端及其上下文中提供一点帮助,因为当我运行它时出现了这个错误:

The security token could not be authenticated or authorized; nested exception is: 
    javax.security.auth.callback.UnsupportedCallbackException; nested exception is org.apache.ws.security.WSSecurityException: The security token could not be authenticated or authorized; nested exception is: 
    javax.security.auth.callback.UnsupportedCallbackException

当我调试我的应用程序时,我发现这个元素崩溃了:

GetUserRequest request = new GetUserRequest();
        request.setIdentifier(user.getIdentifier());
        request.setPassword(user.getPassword());
        GetUserResponse response = new GetUserResponse();
/* Crashing here. */
response = (GetUserResponse) getWebServiceTemplate().marshalSendAndReceive(request);

仅供参考:我总是在SpringWS中看到这个安全用户列表,但如果我有很多用户试图访问该怎么办。

WS - [servlet-name] -servlet.xml

<bean id="callbackHandler" class="org.springframework.ws.soap.security.wss4j.callback.SimplePasswordValidationCallbackHandler">
    <property name="users">
      <props>
        <prop key="Bert">Ernie</prop>
        <prop key="Mickey">Mouse</prop>
      </props>
    </property>
  </bean>

如何解决此UnsupportedCallbackException异常?

2 个答案:

答案 0 :(得分:0)

您必须在通话时指定SecurementUsername和SecurementPassword。这是一个以编程方式示例:

WebServiceTemplate webServiceTemplate = new WebServiceTemplate();
webServiceTemplate.setDefaultUri(uri);

Wss4jSecurityInterceptor interceptor = new Wss4jSecurityInterceptor();
interceptor.setSecurementActions(securementActions);
interceptor.setSecurementMustUnderstand(true);
interceptor.setSecurementUsername(usuario);
interceptor.setSecurementPassword(contrasena);

webServiceTemplate.setInterceptors(new ClientInterceptor[] {interceptor});

答案 1 :(得分:0)

当我从cfx 2.2.X升级到2.7.X时,我收到此错误。由于功能升级,服务器端代码无法读取密码,因此服务器收到密码为空。确保服务器收到正确的用户名和密码,这将解决此问题。