package org.shi.cxf; import java.io.FileNotFoundException; import java.io.PrintWriter; import javax.xml.ws.Endpoint; import org.apache.cxf.ext.logging.LoggingOutInterceptor; import org.apache.cxf.interceptor.LoggingInInterceptor; import org.apache.cxf.jaxws.EndpointImpl; import org.shi.cxf.intecepter.AuthInteceptor; import org.shi.cxf.ws.HelloWorld; import org.shi.cxf.ws.QueryCatsByUser; import org.shi.cxf.ws.impl.HelloWorldImpl; import org.shi.cxf.ws.impl.QueryCatsByUserImpl; /** * Web Service 服務器端 主啓動類 * @author xiaoshi * */ public class WSServiceStart { public static void main(String[] args) throws FileNotFoundException { //須要暴露的服務 HelloWorld hw = new HelloWorldImpl(); QueryCatsByUser queryCatsByUser = new QueryCatsByUserImpl(); // 調用Endpoint 的 publish方法發佈 Web Service 服務 EndpointImpl ep = (EndpointImpl) Endpoint.publish("http://127.0.0.1/shiWS", hw); //自定義攔截器用來來接用戶名密碼等的信息 ep.getInInterceptors().add(new AuthInteceptor()); System.out.println("web Service 發佈成功!"); } }
攔截器的實現java
package org.shi.cxf.intecepter; import java.util.List; import org.apache.cxf.binding.soap.SoapMessage; import org.apache.cxf.headers.Header; import org.apache.cxf.interceptor.Fault; import org.apache.cxf.phase.AbstractPhaseInterceptor; import org.apache.cxf.phase.Phase; /** * 自定義攔截器實現用戶名密碼等的攔截 * @author xiaoshi * */ public class AuthInteceptor extends AbstractPhaseInterceptor<SoapMessage>{ public AuthInteceptor() { super(Phase.PRE_INVOKE);//該攔截器將會 在 「調用以前」 攔截SOAP 消息 } @Override public void handleMessage(SoapMessage msg) throws Fault { System.out.println("------AuthInteceptor攔截到-----" + msg); List<Header> list = msg.getHeaders(); if(list == null || list.size()<1) { throw new Fault(new IllegalArgumentException("用戶不合法,靜止訪問!")); } System.out.println(list.toString()); } }
而後用戶訪問的時候會被攔截到web
2 .客戶端自定義攔截器apache
package org.shi.cxf; import org.apache.cxf.endpoint.Client; import org.apache.cxf.frontend.ClientProxy; import org.apache.cxf.interceptor.LoggingOutInterceptor; import org.shi.cxf.interceptor.AddHeaderInterceptor; import org.shi.cxf.ws.HelloWorld; import org.shi.cxf.ws.impl.HelloWorldServiceName; /** * web service 客戶端 啓動方法 * @author xiaoshi * */ public class WSClientStart { public static void main(String[] args) { HelloWorldServiceName servieFactory = new HelloWorldServiceName(); //此處返回的只是遠程Web Service的代理; HelloWorld hw = servieFactory.getHelloWorldImplPort(); //自定義攔截器實現請求攔截(就是out攔截) Client client = ClientProxy.getClient(hw); client.getOutInterceptors().add(new AddHeaderInterceptor("shiye","123"));//設置消息頭 client.getOutInterceptors().add(new LoggingOutInterceptor());//打印消息請求 System.out.println(hw.sayHi("施爺")); } }
package org.shi.cxf.interceptor; import java.util.List; import javax.xml.namespace.QName; import org.apache.cxf.binding.soap.SoapMessage; import org.apache.cxf.headers.Header; import org.apache.cxf.helpers.DOMUtils; import org.apache.cxf.interceptor.Fault; import org.apache.cxf.phase.AbstractPhaseInterceptor; import org.apache.cxf.phase.Phase; import org.w3c.dom.Document; import org.w3c.dom.Element; public class AddHeaderInterceptor extends AbstractPhaseInterceptor<SoapMessage>{ private String userName; private String pwd; public AddHeaderInterceptor(String userName,String pwd) { super(Phase.PREPARE_SEND); //在準備發送時啓用攔截器 this.pwd = pwd; this.userName = userName; } @Override public void handleMessage(SoapMessage msg) throws Fault { List<Header> list = msg.getHeaders(); //建立Document對象 Document doc = DOMUtils.createDocument(); Element element = doc.createElement("authHeader"); //建立服務器端要求的參數類型格式 Element idElement = doc.createElement("userName"); idElement.setTextContent(userName); Element pwdElement = doc.createElement("pwd"); pwdElement.setTextContent(pwd); element.appendChild(idElement); element.appendChild(pwdElement); Header header = new Header(new QName("auth"),element); list.add(header);//添加消息頭 } }