论坛首页 Java企业应用论坛

用cxf做用户名和密码的检测

浏览 3981 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2008-11-17   最后修改:2008-11-17
SOA
用cxf做用户名和密码的检测

在server端,主要为得到JaxWsServerFactoryBean后配置Interceptors,

<code>

......
JaxWsServerFactoryBean svrFactory = new JaxWsServerFactoryBean();
......

Map<String, Object> inProps = new HashMap<String, Object>();

inProps.put(WSHandlerConstants.ACTION,WSHandlerConstants.USERNAME_TOKEN);
inProps.put(WSHandlerConstants.PASSWORD_TYPE, WSConstants.PW_TEXT);
inProps.put(WSHandlerConstants.PW_CALLBACK_CLASS, ServerPasswordHandler.class.getName());

WSS4JInInterceptor wssIn = new WSS4JInInterceptor(inProps);

svrFactory.getInInterceptors().add(wssIn);
svrFactory.getInInterceptors().add(new SAAJInInterceptor());

......

其中ServerPasswordHandler为真正的用户名密码检查处。

public class ServerPasswordHandler implements CallbackHandler {

@Override
public void handle(Callback[] callbacks) throws IOException,
UnsupportedCallbackException {
WSPasswordCallback pc = (WSPasswordCallback) callbacks[0];

if (pc.getIdentifer().equals("userName")) {

if (!pc.getPassword().equals("password")) {

throw new RuntimeException("security error.");

}
} else {
throw new RuntimeException("security error.");
}

}
}

在client端基本和server端对称,只要在client端配置Interceptors就可以工作了。

<code>

.......
JaxWsProxyFactoryBean factory=......;
.......
Map<String, Object> outProps = new HashMap<String, Object>();
outProps.put(WSHandlerConstants.ACTION,
WSHandlerConstants.USERNAME_TOKEN);
outProps.put(WSHandlerConstants.USER, "userName");
outProps.put(WSHandlerConstants.PASSWORD_TYPE, WSConstants.PW_TEXT);
outProps.put(WSHandlerConstants.PW_CALLBACK_CLASS,
ClientPasswordCallback.class.getName());

WSS4JOutInterceptor wssOut = new WSS4JOutInterceptor(outProps);
factory.getOutInterceptors().add(wssOut);
factory.getOutInterceptors().add(new SAAJOutInterceptor());

其中ClientPasswordCallback实际配置password的地方。

public class ClientPasswordCallback implements CallbackHandler {

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

WSPasswordCallback pc = (WSPasswordCallback) callbacks[0];
pc.setIdentifier("userName");
pc.setPassword("password");
}
}

PS:outProps.put(WSHandlerConstants.USER, "userName");一定要设置,即使后来它会被
pc.setIdentifier("userName");
pc.setPassword("password");
覆盖掉。
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics