spring-security Spring WS“无法验证或授权安全令牌”

js4nwp54  于 2022-11-11  发布在  Spring
关注(0)|答案(2)|浏览(276)

我正在使用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客户端中-应用程序上下文.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);

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

WS - [服务小程序名称]-服务小程序.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异常?

acruukt9

acruukt91#

必须在调用时指定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});
vpfxa7rd

vpfxa7rd2#

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

相关问题