. WebService自定义拦截器只需要继承AbstractPhaseInterceptor即可
. 编写自定义拦截器之后,需要在发布的类和调用类中添加拦截器,分布使用JaxWsServerFactoryBean实例.getInInterceptors().add(自定义拦截器实例);和
JaxWsProxyFactoryBean.getOutInterceptors().add(自定义拦截器实例),进行拦截器的添加。
. 一个简单的用户验证的拦截器,具体的代码如下:
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
服务端代码:
package com.tabchanj.cxf.service; import javax.jws.WebService; /** * 服务接口 * @author tab * */ @WebService public interface HelloService { String sayHi(String name); }
package com.tabchanj.cxf.service; import javax.jws.WebService; /** * 服务接口实现 * @author tab * */ @WebService(endpointInterface = "com.tabchanj.cxf.service.HelloService") public class HelloServiceImpl implements HelloService { public String sayHi(String name) { System.out.println(name + "你好,吃了吗?"); return name + "你好,吃了吗?"; } }
package com.tabchanj.cxf.interceptor; 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; import org.w3c.dom.Element; /** * 服务端用户名验证拦截器 * @author tab * */ public class AuthInterceptor extends AbstractPhaseInterceptor<SoapMessage> { public AuthInterceptor() { super(Phase.PRE_INVOKE); } public void handleMessage(SoapMessage message) throws Fault { // 获取header List<Header> headers = message.getHeaders(); if (headers == null || headers.size() == 0) { throw new Fault(new IllegalArgumentException("用户验证信息不能为空")); } Header header = headers.get(0); Element authinfo = (Element) header.getObject(); String username = authinfo.getElementsByTagName("username").item(0).getTextContent(); String password = authinfo.getElementsByTagName("password").item(0).getTextContent(); System.out.println("username:"+username+"================password:"+password); if (!("admin".equals(username) && "0".equals(password))) { throw new Fault(new IllegalArgumentException("用户验证信息错误")); } } }
package com.tabchanj.cxf.service; import org.apache.cxf.interceptor.LoggingOutInterceptor; import org.apache.cxf.jaxws.JaxWsServerFactoryBean; import com.tabchanj.cxf.interceptor.AuthInterceptor; /** * 服务发布 * @author tab * */ public class Publish { public static void main(String[] args) { // 创建JaxWsServiceFactoryBean对象 JaxWsServerFactoryBean bean = new JaxWsServerFactoryBean(); // s设置发布地址 bean.setAddress("http://192.168.1.3:9119/hi"); // 设置服务发布的接口 bean.setServiceClass(HelloService.class); // 设置服务的发布对象 bean.setServiceBean(new HelloServiceImpl()); // ===================拦截器===================== bean.getInInterceptors().add(new LoggingOutInterceptor());// 配置日志记录拦截器 bean.getInInterceptors().add(new AuthInterceptor());// 配置用户权限拦截器 // ===================拦截器===================== // 发布 bean.create(); System.out.println("发布成功"); } }
客户端代码:
package com.tabchanj.cxf.interceptor; 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; /** * 客户端拦截器,用于将用户信息封装到WSDL的header中 * @author tab * */ public class LicenseInterceptor extends AbstractPhaseInterceptor<SoapMessage> { private String username; private String password; public LicenseInterceptor(String username, String password) { super(Phase.PREPARE_SEND);//表示在向服务端发送消息之前执行本拦截器 this.username = username; this.password = password; } public void handleMessage(SoapMessage message) throws Fault { // 获取用户数据 // 将数据封装到header中 // 建立header的xml结构 Document document = DOMUtils.createDocument(); Element authinfoEl = document.createElement("authinfo"); Element usernameEl = document.createElement("username"); Element passwordEl = document.createElement("password"); usernameEl.setTextContent(username); passwordEl.setTextContent(password); authinfoEl.appendChild(usernameEl); authinfoEl.appendChild(passwordEl); // 将xml结构添加到header中 message.getHeaders().add(new Header(new QName("xx"), authinfoEl)); } }
package com.tabchanj.cxf.service; import org.apache.cxf.interceptor.LoggingOutInterceptor; import org.apache.cxf.jaxws.JaxWsProxyFactoryBean; import org.junit.Test; import com.tabchanj.cxf.interceptor.LicenseInterceptor; /** * 客户端的服务调用类 * * @author tab * */ public class Call { @Test public void test() { // 1.创建JaxWsProxyFactoryBean的对象,用于接收服务 JaxWsProxyFactoryBean proxy = new JaxWsProxyFactoryBean(); // 2.设置服务的发布地址,表示去哪里过去服务 proxy.setAddress("http://192.168.1.3:9119/hi"); // 3.设置服务的发布接口,使用本地的代理接口 proxy.setServiceClass(HelloService.class); // --------------------------------拦截器----------------------------------------- proxy.getOutInterceptors().add(new LoggingOutInterceptor());// 日志记录拦截器 proxy.getOutInterceptors().add(new LicenseInterceptor("admin", "0"));// 配置客户端用户信息配置拦截器 // ------------------------------------------------------------------------- // 4.通过create方法返回接口代理实例 HelloService service = (HelloService) proxy.create(); // 5.调用远程方法 System.out.println(service.sayHi("tom")); } }
时间: 2024-10-14 00:56:06