听到拦截器,其实我们就应该想到它的作用:即在我们每次访问请求的时候都会被拦截,先去处理一些其它的事情。比如说在webService上,我们对发布的服务有权限要求,只有有权限的才可以访问我们的服务。而在此处,其实现就要用到我们的拦截器了,具体如下:
服务端
1、添加拦截器类(用户接受客户端消息)
public class AuthInterceptor extends AbstractPhaseInterceptor<SoapMessage>{
public AuthInterceptor() {
//super表示显示调用父类有参数的构造器
//显示调用父类构造器,程序将不会隐式调用父类无参数的构造器
super(Phase.PRE_INVOKE); //该拦截器将会“调用之前”拦截SOAP消息
}
//实现自己的拦截器时,需要实现handleMessage方法
//handleMessage方法中的形参就是被拦截到的Soap消息
//一旦程序获取了SOAP消息,剩下的事情就可以解析SOA消息,或修改SOAP消息
@Override
public void handleMessage(SoapMessage msg) throws Fault {
//从这里可以看出,我们已经拦截到了SOAP消息
System.out.println("-------------" +msg);
//得到SOAP消息所有Header
List<Header> headers=msg.getHeaders();
//如果根本没有Header
if (headers==null || headers.size()<1){
throw new Fault(new IllegalArgumentException("根本没有Header,别笑调用!"));
}
//假如要求第一个Header携带 了用户名、密码信息
Header firstHeader=headers.get(0);
Element element=(Element)firstHeader.getObject();
NodeList userIds=element.getElementsByTagName("userId");
NodeList userPasses=element.getElementsByTagName("userPass");
if (userIds.getLength() !=1){
throw new Fault(new IllegalArgumentException("用户名格式不对!"));
}
if (userPasses.getLength() !=1){
throw new Fault(new IllegalArgumentException("密码格式不对!"));
}
//获取用户名和密码
String userId=userIds.item(0).getTextContent();
String userPass=userPasses.item(0).getTextContent();
if (!userId.equals("lj") || !userPass.equals("lj")){
throw new Fault(new IllegalArgumentException("用户名或密码不正确!"));
}
}
}
2、发布webService的时候将拦截器配置上
public class ServerMain {
public static void main(String[] args) throws IOException {
HelloWorld hw=new HelloWorldWs();
//调用Endpoint的publish方法发布Web Service
EndpointImpl ep= (EndpointImpl)Endpoint.publish("http://192.168.1.111:8088/HelloWorld", hw);
//============第一种,将日志以文件形式输出=================================================
// //添加In拦截器
// ep.getInInterceptors().add(new LoggingInInterceptor(new PrintWriter(new FileWriter("in.txt"))));
// //添加Out拦截器
// ep.getOutInterceptors().add(new LoggingOutInterceptor(new PrintWriter(new FileWriter("out.txt"))));
//=============第二种,将日志输出到控制台=================================================
// //添加In拦截器
// ep.getInInterceptors().add(new LoggingInInterceptor());
// //添加Out拦截器
// ep.getOutInterceptors().add(new LoggingOutInterceptor());
//===========第三种,自定义拦截器,此处添加的是上面的拦截器===============================
ep.getInInterceptors().add(new AuthInterceptor());
System.out.println("webService 发布成功!");
}
}
客户端:
1、添加拦截器类(用户向webService传递消息):
public class AddHeaderInterceptor extends AbstractPhaseInterceptor<SoapMessage>{
private String userId;
private String userPass;
public AddHeaderInterceptor(String userId,String userPass) {
//在准备发送soap消息时启用该拦截器
super(Phase.PREPARE_SEND);
this.userId=userId;
this.userPass=userPass;
}
@Override
public void handleMessage(SoapMessage msg) throws Fault {
List<Header> headers=msg.getHeaders();
//创建Docuemnt对象
Document document=DOMUtils.createDocument();
Element element=document.createElement("authHeader");
Element idElement=document.createElement("userId");
idElement.setTextContent(userId);
Element passElement=document.createElement("userPass");
passElement.setTextContent(userPass);
element.appendChild(idElement);
element.appendChild(passElement);
/**
* 上面代码生成了一个如下XML文档片段
* <authHeader>
* <userId>aaa</userId>
* <userPass>bbb</userPass>
* </authHeader>
*/
//把element元素包装成Header,并添加到SOAP消息的Header列表中
headers.add(new Header(new QName("lj"),element));
}
}
2、客户端调用webService的时候添加此拦截器
public class ClientMain {
public static void main(String[] args) {
HelloWorld factory=new HelloWorld();
com.tgb.web.webservice.HelloWorld hWorld= factory.getHelloWorldWsPort();
// //客户端调用日志拦截器
// Client client=ClientProxy.getClient(hWorld);
// client.getInFaultInterceptors().add(new LoggingInInterceptor());
// client.getInFaultInterceptors().add(new LoggingOutInterceptor());
//客户端调用拦截器
Client client=ClientProxy.getClient(hWorld);
client.getOutInterceptors().add(new AddHeaderInterceptor("lj","lj"));
client.getOutInterceptors().add(new LoggingOutInterceptor());
System.out.println(hWorld.sayHi("张三"));
System.out.println("===================================================");
User user=new User();
user.setId(30);
user.setName("sun");
user.setPass("3322");
List<Cat> cats=hWorld.getCatsByUser(user);
for(Cat cat :cats){
System.out.println(cat.getName());
}
System.out.println("===================================================");
//调用CXF处理过的类型方法
StringCat SC=hWorld.getAllCats();
for(Entry entry:SC.getEntries()){
System.out.println(entry.getKey()+"-"+entry.getValue().getName());
}
}
}
由此我们就可以实现我们的权限访问功能了,是不是很强大啊!