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

用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, [color=red]ServerPasswordHandler[/color].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,
[color=red]ClientPasswordCallback[/color].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");
覆盖掉。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值