使用Axis2和Rampart使用UsernameToken配置文件1.0保护Web服务

时间:2015-09-30 10:42:47

标签: tomcat soap axis2 rampart usernametoken

这是我在堆栈溢出中的第一个问题。

我不是专业的java程序员,但我之前有使用该语言和不同IDE的经验。

我有一个场景,客户要求从给定的WSDl创建一个服务,必须使用UsernameToken Profile 1.0 OASIS Standard 200401进行身份验证。它将在生产服务器上的ssl下受到保护。

我一直在做一些研究,并试图实施不同的案例,我到达的地方似乎没有任何工作正常。

我正在使用:

  • Eclipse作为基础IDE
  • Axis2 V1.6.3
  • Rampart V1.6.2
  • Rahas V1.6.2

为了说明当前的情况,我在这里向您展示eclipse在从wsdl eclipse生成java bean服务结构作为基本IDE时为我创建的结构。

https://dl.dropboxusercontent.com/u/71031985/schema.png


配置已应用:

WebContent / WEB-INF / conf / axis2.xml中的

我启用了rampart模块和passwordCallbackClass,以便能够处理soap标头中提供的用户名和密码。

<module ref="rampart" />

<parameter name="InflowSecurity">
    <action>
        <items>UsernameToken</items>
        <passwordCallbackClass>
            serviceManager.ServiceAuthUserNameToken
        </passwordCallbackClass>
        <passwordType>PasswordText</passwordType>
    </action>
</parameter>

...

在位于/WebContent/WEB-INF/services/ProveedorCentroTFWS/META-INF/services.xml的文件中,我放置了垒垒策略,以便能够完成usernametoken要求:

    <wsp:Policy wsu:Id="UTOverTransport" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy">
<wsp:ExactlyOne>
  <wsp:All>
    <sp:TransportBinding xmlns:sp="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy">
      <wsp:Policy>
        <sp:TransportToken>
          <wsp:Policy>
            <sp:HttpsToken RequireClientCertificate="false"/>
          </wsp:Policy>
        </sp:TransportToken>
        <sp:AlgorithmSuite>
          <wsp:Policy>
            <sp:Basic128/>
          </wsp:Policy>
        </sp:AlgorithmSuite>
        <sp:Layout>
          <wsp:Policy>
            <sp:Lax/>
          </wsp:Policy>
        </sp:Layout>
        <sp:IncludeTimestamp/>
      </wsp:Policy>
    </sp:TransportBinding>
    <sp:SignedSupportingTokens xmlns:sp="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy">
        <wsp:Policy>
            <sp:UsernameToken sp:IncludeToken="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy/IncludeToken/AlwaysToRecipient" />
      </wsp:Policy>
    </sp:SignedSupportingTokens>

    <ramp:RampartConfig xmlns:ramp="http://ws.apache.org/rampart/policy"> 
        <ramp:passwordCallbackClass>serviceManager.ServiceAuthUserNameToken</ramp:passwordCallbackClass>
    </ramp:RampartConfig>

  </wsp:All>
</wsp:ExactlyOne>

当从客户提供的测试客户端执行调用(并且无法修改)时,它会发送以下soap消息:

    <?xml version='1.0' encoding='UTF-8'?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header xmlns:wsa="http://www.w3.org/2005/08/addressing">
    <wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" soapenv:mustUnderstand="1">
        <wsse:UsernameToken wsu:Id="UsernameToken-3">
            <wsse:Username>username</wsse:Username>
            <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">definedpwd</wsse:Password>
        </wsse:UsernameToken>
    </wsse:Security>
    <wsa:To>http://localhost:8080/ProveedorCentroTFWS</wsa:To>
    <wsa:MessageID>urn:uuid:f2fb54d9-8957-49a2-88a7-de6d209e6d35</wsa:MessageID>
    <wsa:Action>getActionList</wsa:Action>
</soapenv:Header>
<soapenv:Body>
    <ns3:getActionListxmlns:ns3="http://impl.ws.application.proveedorcentro.meyss.spee.es" />
</soapenv:Body>

发送soap消息后,返回的错误如下:

    java.lang.RuntimeException: Malformed uri: UsernameTokenPolicy

它遵循的堆栈跟踪是:

    org.apache.neethi.PolicyReference.getRemoteReferencedPolicy(PolicyReference.java:155)
org.apache.neethi.PolicyReference.normalize(PolicyReference.java:110)
org.apache.axis2.util.PolicyUtil.getMergedPolicy(PolicyUtil.java:267)
org.apache.axis2.description.AxisBindingMessage.calculateEffectivePolicy(AxisBindingMessage.java:294)
org.apache.axis2.description.AxisBindingMessage.getEffectivePolicy(AxisBindingMessage.java:225)
org.apache.axis2.context.MessageContext.getEffectivePolicy(MessageContext.java:1617)
org.apache.rampart.RampartMessageData.&lt;init&gt;(RampartMessageData.java:233)
org.apache.rampart.MessageBuilder.build(MessageBuilder.java:61)
org.apache.rampart.handler.RampartSender.invoke(RampartSender.java:65)
org.apache.axis2.engine.Phase.invokeHandler(Phase.java:340)
org.apache.axis2.engine.Phase.invoke(Phase.java:313)
org.apache.axis2.engine.AxisEngine.invoke(AxisEngine.java:262)
org.apache.axis2.engine.AxisEngine.sendFault(AxisEngine.java:516)
org.apache.axis2.transport.http.AxisServlet.handleFault(AxisServlet.java:433)
org.apache.axis2.transport.http.AxisServlet.doPost(AxisServlet.java:216)
javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

我检查了所有方法并试图让它接受肥皂信息,但是我无法使它工作。

感谢您的支持和关注

1 个答案:

答案 0 :(得分:0)

此问题的解决方案是提供的WSDL错过了一些部分,以便能够在UsernameToken Profile 1.0 OASIS Standard 200401下进行验证。

在IBM page下找到关于WS-Security with Metro的解决方案。

在WSDL文档中,绑定标记下是一个策略引用:

<wsp:PolicyReference URI="#UsernameTokenPolicy" wsdl:required="true"/>

阅读在线文档我看到它指向无处,在同一个WSLD文件中添加策略条目是必要的,例如下一个具有标识符Name(#UsernameTokenPolicy)或所需的策略名称的WSLD文件我们想申请:

<wsp:Policy wsu:Id="UsernameTokenPolicy" xmlns:wsp="http://www.w3.org/ns/ws-policy"
xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
<sp:SupportingTokens
    xmlns:sp="http://docs.oasis-open.org/ws-sx/ws-securitypolicy/200702">
    <wsp:Policy>
        <sp:UsernameToken
            sp:IncludeToken="http://docs.oasis-open.org/ws-sx/ws-securitypolicy/200702/IncludeToken/AlwaysToRecipient">
            <wsp:Policy>
                <sp:WssUsernameToken11 />
            </wsp:Policy>
        </sp:UsernameToken>
    </wsp:Policy>
</sp:SupportingTokens>
<wsss:ValidatorConfiguration wspp:visibility="private"
      xmlns:wsss="http://schemas.sun.com/2006/03/wss/server"
      xmlns:wspp="http://java.sun.com/xml/ns/wsit/policy">
    <wsss:Validator name="usernameValidator" classname="[packageName].[callBackValidatorName]"/>
    </wsss:ValidatorConfiguration>
</wsp:Policy>

完成此操作后,您需要创建一个带有响应的回调Validator来处理SOAP请求的标头凭据,如下一个提供的示例所示:

package [packageName];


import com.sun.xml.wss.impl.callback.PasswordValidationCallback;
import java.io.IOException;
import javax.security.auth.callback.Callback;
import javax.security.auth.callback.UnsupportedCallbackException;

public class [callBackValidatorName] implements PasswordValidationCallback.PasswordValidator{

@Override
public boolean validate(PasswordValidationCallback.Request request) throws PasswordValidationCallback.PasswordValidationException {

    PasswordValidationCallback.PlainTextPasswordRequest ptreq;

    ptreq = (PasswordValidationCallback.PlainTextPasswordRequest) request;

    return "[HARCODED_USERNAME]".equals(ptreq.getUsername()) &&
        "[HARCODED_PWD]".equals(ptreq.getPassword());
}
}

完成所有这些后,您必须能够在服务器端的Java Web服务下验证与UsernameToken Profile 1.0 OASIS Standard 200401匹配的SOAP消息。