** jdk写的程序不支持拦截器,需要使用cxf框架,导入cxf的jar包**
拦截器分为In拦截器和out拦截器
为服务端添加日志拦截器
JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean();
//设置Service Class
factory.setServiceClass(HelloWS.class);
factory.setAddress(address);
//设置ServiceBean对象
factory.setServiceBean(new HelloWS());
//添加请求和响应的拦截器,日志拦截器
factory.getInInterceptors().add(new LoggingInInterceptor()); //请求
factory.getOutInterceptors().add(new LoggingOutInterceptor()); //响应
factory.create();
启动之后 日志拦截器就会拦截请求与响应,将日志输出来
** 自定义拦截器 **
自定拦截器需要实现Imterceptor接口。实际上一般会继承AbstractPhaseInterceptor抽象类,CXF框架本身定义了很多拦截器,很多可以直接使用
- 在客户端程序新建一个类,继承AbstractPhaseInterceptor类,重写handleMessage方法
public class CheckUserInterceptor extends AbstractPhaseInterceptor<SoapMessage>{
public CheckUserInterceptor() {
//调用父类的构造器初始化AbstractPhaseInterceptor
super(Phase.PRE_PROTOCOL);
}
@Override
public void handleMessage(SoapMessage message) throws Fault {
Header header = message.getHeader(new QName("zlb"));
if(header != null){
Element element = (Element) header.getObject();
String userName = element.getElementsByTagName("name").item(0).getTextContent();
String userPass = element.getElementsByTagName("password").item(0).getTextContent();
if(userName.equals("test") && userPass.equals("123456")){
System.out.println("用户名密码正确!");
return;
}
throw new RuntimeException("用户名密码不正确!");
}
}
以上自定义的拦截器,会在当前的请求头里添加一段请求头信息
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Header>
<zlb>
<name>test</name>
<password>123456</password>
</zlb>
</soap:Header>
<soap:Body>
<ns2:sayHello xmlns:ns2="http://service.zlb.com/">
<arg0>tom</arg0>
</ns2:sayHello>
</soap:Body>
</soap:Envelope>
- 将自定义的拦截器添加进去
//WSImport 方式
HelloWSService factory = new HelloWSService();
HelloWS ws = factory.getHelloWSPort();
//将自定义的拦截器加入
Client client = ClientProxy.getClient(ws);
List<Interceptor<? extends Message>> outInterceptor = client.getOutInterceptors();
outInterceptor.add(new LoginInterceptor("test", "123456"));
ws.sayHello("tom");
- 服务端拦截器获取请求头信息
public class CheckUserInterceptor extends AbstractPhaseInterceptor<SoapMessage>{
public CheckUserInterceptor() {
super(Phase.PRE_PROTOCOL);
}
@Override
public void handleMessage(SoapMessage message) throws Fault {
Header header = message.getHeader(new QName("zlb"));
if(header != null){
Element element = (Element) header.getObject();
String userName = element.getElementsByTagName("name").item(0).getTextContent();
String userPass = element.getElementsByTagName("password").item(0).getTextContent();
if(userName.equals("test") && userPass.equals("123456")){
System.out.println("用户名密码正确!");
return;
}
throw new RuntimeException("用户名密码不正确!");
}
}
}
- 将服务端自定义的拦截器添加进去
//地址
String address = "http://127.0.0.1:8989/WebServices_service";
//java JDK版 发布
/*Endpoint.publish(address, new HelloWS());*/
JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean();
//设置Service Class
factory.setServiceClass(HelloWS.class);
factory.setAddress(address);
//添加请求和响应的拦截器,日志拦截器
factory.getInInterceptors().add(new LoggingInInterceptor()); //请求
factory.getInInterceptors().add(new CheckUserInterceptor()); //自定义拦截器
factory.getOutInterceptors().add(new LoggingOutInterceptor()); // 响应
factory.create();