标签:拦截器
使用自定义拦截器实现用户名和密码的校验
客户端:出拦截器
服务器:入拦截器
客户端
AddUserInterceptor.java
package com.demo.interceptors; 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.interceptor.Fault; import org.apache.cxf.phase.AbstractPhaseInterceptor; import org.apache.cxf.phase.Phase; import org.apache.xml.utils.DOMHelper;//使用xalan-2.7.1.jar import org.w3c.dom.Document; import org.w3c.dom.Element; public class AddUserInterceptor extends AbstractPhaseInterceptor<SoapMessage> { private String username; private String password; public AddUserInterceptor(String username, String password) { super(Phase.PRE_PROTOCOL);// 准备协议化时拦截 this.username = username; this.password = password; } /* * 消息格式 <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Header> <atguigu> <username>umgsai</username> <password>123456</password> </atguigu> </soap:Header> <soap:Body> <ns1:sayHi xmlns:ns1="http://demo.com/"> <text>test~~~</text> </ns1:sayHi> </soap:Body> </soap:Envelope> * * */ @Override public void handleMessage(SoapMessage message) throws Fault { List<Header> headers = message.getHeaders(); Document document = DOMHelper.createDocument(); Element rootElement = document.createElement("atguigu"); Element usernameElement = document.createElement("username"); usernameElement.setTextContent(username); rootElement.appendChild(usernameElement); Element passwordElement = document.createElement("password"); passwordElement.setTextContent(password); rootElement.appendChild(passwordElement); headers.add(new Header(new QName("atguigu"), rootElement)); System.out.println("拦截..."); } }
InterceptorHelloWorldClient.java 客户端添加拦截器
package com.demo; import org.apache.cxf.interceptor.LoggingOutInterceptor; import org.apache.cxf.jaxws.JaxWsProxyFactoryBean; import com.demo.HelloWorld; import com.demo.User; import com.demo.interceptors.AddUserInterceptor; //http://blog.csdn.net/fhd001/article/details/5778915 public class InterceptorHelloWorldClient { public static void main(String[] args) { JaxWsProxyFactoryBean svr = new JaxWsProxyFactoryBean(); svr.setServiceClass(HelloWorld.class); svr.setAddress("http://localhost:8080/helloWorld"); HelloWorld hw = (HelloWorld) svr.create(); // jaxws API 转到 cxf API 添加日志拦截器 org.apache.cxf.endpoint.Client client = org.apache.cxf.frontend.ClientProxy .getClient(hw); org.apache.cxf.endpoint.Endpoint cxfEndpoint = client.getEndpoint(); //添加日志拦截器 cxfEndpoint.getOutInterceptors().add(new LoggingOutInterceptor()); //添加自定义的拦截器 cxfEndpoint.getOutInterceptors().add(new AddUserInterceptor("umgsai", "123456")); User user = new User(); user.setUsername("Umgsai"); user.setDescription("test"); System.out.println(hw.sayHiToUser(user)); //String sayHi = hw.sayHi("test~~~"); //System.out.println(sayHi); } }
服务器端
CheckUserInterceptor.java 拦截器
package com.demo.interceptors; import javax.xml.namespace.QName; 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; import org.w3c.dom.Element; //检查用户的 拦截器 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("atguigu")); if (header != null) { Element element = (Element) header.getObject(); String username = element.getElementsByTagName("username").item(0).getTextContent(); String password = element.getElementsByTagName("password").item(0).getTextContent(); if (username.equals("umgsai")&&password.equals("123456")) { System.out.println("通过拦截器..."); return; } } //不能通过 System.out.println("没有通过拦截器"); throw new Fault(new RuntimeException("用户名或密码不正确")); } }
InterceptorServerApp.java 将服务器端拦截器添加到服务端
package com.demo; import javax.xml.ws.Endpoint; import org.apache.cxf.interceptor.LoggingInInterceptor; import org.apache.cxf.jaxws.EndpointImpl; import com.demo.interceptors.CheckUserInterceptor; public class InterceptorServerApp { public static void main(String[] args) { System.out.println("Starting web service... "); HelloWorldImpl implementor = new HelloWorldImpl(); String address = "http://localhost:8080/helloWorld"; Endpoint endpoint = Endpoint.publish(address, implementor); // jaxws API 转到 cxf API 添加日志拦截器 EndpointImpl jaxwsEndpointImpl = (EndpointImpl) endpoint; org.apache.cxf.endpoint.Server server = jaxwsEndpointImpl.getServer(); org.apache.cxf.endpoint.Endpoint cxfEndpoint = server.getEndpoint(); //添加日志拦截器 LoggingInInterceptor logging = new LoggingInInterceptor(); cxfEndpoint.getInInterceptors().add(logging); //添加自定义拦截器 cxfEndpoint.getInInterceptors().add(new CheckUserInterceptor()); System.out.println("Web service started"); } }
参考尚硅谷教程
本文出自 “阿凡达” 博客,请务必保留此出处http://shamrock.blog.51cto.com/2079212/1563384
标签:拦截器
原文地址:http://shamrock.blog.51cto.com/2079212/1563384