使用Apache CXF验证SOAP usernameToken时出现问题

时间:2018-09-17 11:39:19

标签: java soap cxf ws-security

我正在尝试在Apache CXF 3.0.4下开发SOAP Web服务,但是在使用WS-SecurityPolicy验证每个SOAP消息标头中包含的UsernameToken时遇到了麻烦。 根据{{​​3}},如果cxf-rt-ws-policy上有cxf-rt-ws-securityclasspath模块可用,则WS-SecurityPolicy将自动启用。经过适当的处理,它可以进行必要的工作来处理安全性。 组态。 我通过“端点属性注释”进行配置 通过以下方式:

@WebService(targetNamespace = "http://tempuri.org/", name = "MyService")

@EndpointProperties(value = {
        @EndpointProperty(key = "ws-security.callback-handler", value = "org.tempuri.ServerPasswordCallback")
        //@EndpointProperty(key = "ws-security.validate.token", value = "false")
    }
)


public interface MyService {
...
}

ServerPasswordCallback是:

public class ServerPasswordCallback implements CallbackHandler {

    public ServerPasswordCallback() {
        System.out.println("Instantiating ServerPasswordCallback");
    }

    public void handle(Callback[] callbacks) throws IOException,
            UnsupportedCallbackException {

        System.out.println("Validating on ServerPasswordCallback");

        WSPasswordCallback pc = (WSPasswordCallback) callbacks[0];

        if (pc.getIdentifier().equals("joe")) {
            // set the password on the callback. This will be compared to the
            // password which was sent from the client.
            pc.setPassword("password");
        }
    }

}

问题是我得到以下异常:

Caused by: org.apache.wss4j.common.ext.WSSecurityException: The security token could not be authenticated or authorized
        at org.apache.wss4j.dom.validate.UsernameTokenValidator.verifyDigestPassword(UsernameTokenValidator.java:176)
        at org.apache.wss4j.dom.validate.UsernameTokenValidator.verifyPlaintextPassword(UsernameTokenValidator.java:136)
        at org.apache.wss4j.dom.validate.UsernameTokenValidator.validate(UsernameTokenValidator.java:94)

已发送邮件的标题为:

<?xml version="1.0"?>
<soapenv:Header>
  <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">
    <wsse:UsernameToken wsu:Id="UsernameToken-3B91E43693FA4F34C61536922750459149">
      <wsse:Username>joe</wsse:Username>
      <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">password</wsse:Password>
      <wsse:Nonce EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary">ey+3J+OKoHlhfqREn7Q8jw==</wsse:Nonce>
      <wsu:Created>2018-09-14T10:59:10.459Z</wsu:Created>
    </wsse:UsernameToken>
    <wsu:Timestamp wsu:Id="TS-3B91E43693FA4F34C61536922750459148">
      <wsu:Created>2018-09-14T10:59:10.459Z</wsu:Created>
      <wsu:Expires>2018-09-14T10:59:15.459Z</wsu:Expires>
    </wsu:Timestamp>
  </wsse:Security>
</soapenv:Header>

奇怪的是,似乎ServerPasswordCallback从未实例化,handle()从未被调用。 如果在“端点属性”注释中将ws-security.validate.token设置为false,则即使发生此异常,也会引发前一个异常。 属性应阻止令牌验证。 这个事实使我认为注释不起作用,但是我不知道为什么。 这是验证UsernameToken的正确方法吗? 端点属性注释正确吗?

我无法设置documentation中建议的Endpoint属性,因为我无法访问Endpoint实例。 一个示例项目可用documentation

1 个答案:

答案 0 :(得分:0)

最后我想出了一个解决方案:

menuItem['component']

为了使注释生效,必须在EndpointProperties中包含以下注释:

menuItem:MenuItem = {title:"t",icon:"i",component:1,click:"c"}