***客户端拦截器***
public class AccountInterceptor extends AbstractPhaseInterceptor<SoapMessage>{
private String name;
private String password;
public AccountInterceptor(String name,String password) {
//Phase值决定了拦截器什么时候拦截到消息
//PRE_PROTOCOL准备请求时拦截
super(Phase.PRE_PROTOCOL);
this.name = name;
this.password = password;
}
//一旦被拦截,首先调用此方法
@SuppressWarnings("deprecation")
@Override
public void handleMessage(SoapMessage msg) throws Fault {
List<Header> headers = msg.getHeaders();
//在客户端请求时,会将用户名密码带过去
//怎么带用户名和密码到服务器,将用户名和密码设置在请求头中
org.w3c.dom.Document document = DOMHelper.createDocument();
Element ele = document.createElement("account");//创建标签<account></account>
Element eleName = document.createElement("name");//创建标签<name></name>
eleName.setTextContent(name);//给<name>设值,值为客户端传进来的用户名
ele.appendChild(eleName);
Element elePwd = document.createElement("password");//创建标签<password></password>
elePwd.setTextContent(password);//给<password>设值,值为客户端传进来的密码
ele.appendChild(elePwd);
//设置标签<account>的account
headers.add(new Header(new QName("account"),ele));
//如果拦截了,打印以下信息!
System.out.println("客户端拦截了");
}
}
***添加自定义拦截器到客户端出拦截器中***
public class TestWebService {
public static void main(String[] args) {
HelloWSImplService factory = new HelloWSImplService();
HelloWSImpl helloWSImpl = factory.getHelloWSImplPort();
//设置用户名和密码
String name = "webService";
String password = "123456";
//在调用服务器方法前配置拦截器
//获取发送请求的客户端对象
Client client = ClientProxy.getClient(helloWSImpl);
//添加自定义拦截器到客户端出拦截器中
List<Interceptor<? extends Message>> outInterceptors = client.getOutInterceptors();
outInterceptors.add(new AccountInterceptor(name,password));
String result = helloWSImpl.sayHello("webService");
System.out.println(result);
}
}
*****************以上是客户端********************
*****************以上是服务器********************
//服务器拦截器
public class CheckAccountInterceptor extends AbstractPhaseInterceptor<SoapMessage>{
public CheckAccountInterceptor() {
super(Phase.PRE_PROTOCOL);
}
@Override
public void handleMessage(SoapMessage message) throws Fault {
//获取客户端请求头
//account为客户端设置的qname
Header header = message.getHeader(new QName("account"));
if(header != null){
Element account = (Element) header.getObject();
//通过标签名获取值<name></name>
String name = account.getElementsByTagName("name").item(0).getTextContent();
String password = account.getElementsByTagName("password").item(0).getTextContent();
if("webService".equals(name) && "123456".equals(password)){
System.out.println("验证通过......");
}
}
System.out.println("没有通过拦截器!");
throw new Fault(new RuntimeException("用户名或者密码错误!"));
}
}
//将自定义拦截器添加到服务器的入拦截器
public class RealeaseWS {
public static void main(String[] args) {
String address = "http://localhost:8989/WebService_Server";
Endpoint endpoint = Endpoint.publish(address, new HelloWSImpl());
EndpointImpl endpointImpl = (EndpointImpl)endpoint;
//将自定义拦截器添加到服务器的入拦截器
List<Interceptor<? extends Message>> inInterceptors = endpointImpl.getInInterceptors();
inInterceptors.add(new CheckAccountInterceptor());
System.out.println("webService发布成功!");
}
}
运行程序:
如果没有通过,则出现以下信息:
客户端拦截了
Exception in thread "main" javax.xml.ws.soap.SOAPFaultException: 用户名或者密码错误!
at org.apache.cxf.jaxws.JaxWsClientProxy.invoke(JaxWsClientProxy.java:156)
at com.sun.proxy.$Proxy27.sayHello(Unknown Source)
at com.test.TestWebService.main(TestWebService.java:34)
Caused by: org.apache.cxf.binding.soap.SoapFault: 用户名或者密码错误!