1、所需文件
service.jks,放在src下面(与client.jks是一对,具体生成方式可上网查询)
services.xml
server of receiveMessage ------服务描述信息
com.test.ReceiveMessageServer -------服务类,连带包结构
org.apache.axis2.extensions.spring.receivers.SpringAppContextAwareObjectSupplier
-------这是axis2与spring的集成,所以将服务类配成了spring bean,这里直接引用spring bean 的名称
-------安全策略的配置
xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy">
-------判断密码的回调方法的路径,后面有具体代码
3、代码
判断密码的回调方法的具体代码:
import java.io.IOException;
import javax.security.auth.callback.Callback;
import javax.security.auth.callback.CallbackHandler;
import javax.security.auth.callback.UnsupportedCallbackException;
import org.apache.ws.security.WSPasswordCallback;
public class MsgCallbackHandler implements CallbackHandler {
private final static String SERVER_ALIAS = "service";
private final static String SERVER_ALIAS_PASSWORD = "servicePW";
public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
for (int i = 0; i < callbacks.length; i++) {
// To use the private key to sign messages, we need to provide the private key password
WSPasswordCallback pwcb = (WSPasswordCallback) callbacks[i];
if (pwcb.getIdentifier().equals(SERVER_ALIAS)) {
pwcb.setPassword(SERVER_ALIAS_PASSWORD);
return;
}
}
}
}
服务方法的具体代码:
public class ReceiveMessageServer {
public String receiveMessage(Message message) {
if (message !=null && StringUtils.isEmpty(message.getMsgId())) {
System.out.println("成功接收信息!");
return "0";
}else{
System.out.println("接收信息失败!");
return "1";
}
}