7

我正在尝试与 CXF 的 WS-security implementation(usernametoken) 结合在一起。我已经完成了http://cxf.apache.org/docs/ws-security.html中所说的一切。我的 PasswordCallbackHandler 似乎正在工作,但困扰我的是一部分:

    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");
    }

如前所述

请注意,对于 CXF 2.3.x 及之前的版本,纯文本密码(或任何其他未知密码类型)的特殊情况的密码验证委托给回调类,请参阅 org.apache.ws.security。 WSS4J 项目的processor.UsernameTokenProcessor#handleUsernameToken() 方法javadoc。在这种情况下,ServerPasswordCallback 应该类似于以下内容:

所以直到 cxf 2.3.x 都是这样完成的

   if (pc.getIdentifer().equals("joe") {
       if (!pc.getPassword().equals("password")) {
            throw new IOException("wrong password");
       }
    }

我的问题是:我不想 pc.setPassword("plainTextPassword") 因为我想将它存储在任何资源中。这种高达 2.3.x 的设计允许我这样做,因为我可以手动加密它。有没有办法在回调中设置加密密码或对存储的加密密码进行 usernametoken 身份验证?

我正在使用 cxf 2.5.x

4

2 回答 2

6

答案(我已经尝试过)可以在这个博客页面中找到:

http://coheigea.blogspot.com/2011/06/custom-token-validation-in-apache-cxf.html

本质是创建org.apache.ws.security.validate.UsernameTokenValidator的子类,重写verifyPlaintextPassword方法。在该方法中,传递了 UsernameToken(提供 getName 和 getPassword)。如果它们无效,则抛出异常。

要在 spring 配置中安装自定义验证器,请添加例如

  <jaxws:properties>
    <entry key="ws-security.ut.validator">
        <bean class="com.example.webservice.MyCustomUsernameTokenValidator" />
    </entry>
  </jaxws:properties>

进入 <jaxws:endpoint/>。

于 2012-09-11T19:48:23.063 回答
2

回调处理程序用于提供明文密码或验证明文密码已知的摘要密码。

但是如果你不知道明文,即它的一种散列方式,那么回调接口是不合适的,你应该创建一个实现Validator接口的类。

这是我使用 JPA 存储库的接口的示例实现,其中密码已经存储为 BCrypt 哈希。

此处ws-security.ut.validator记录的属性一起使用

即作为 CXF 属性 <entry key="ws-security.ut.validator" value-ref="com.package.CustomUsernameTokenValidator" />

public class CustomUsernameTokenValidator implements Validator {
    @Autowired
    ProfileRepository profileRepository;
    @Override
    public Credential validate(Credential credential, RequestData requestData) throws WSSecurityException {
        Profile profile = profileRepository.findByName(credential.getUsernametoken().getName());
        if (profile != null) {
            if (BCrypt.checkpw(credential.getUsernametoken().getPassword(), profile.getPassword())) {
                return credential;
            }
        }
        throw new WSSecurityException(WSSecurityException.ErrorCode.FAILED_AUTHENTICATION);     
    }
}
于 2015-03-22T21:52:37.603 回答