学习webservice之cxf:cxf自定义拦截器
Posted 十黎九夏
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了学习webservice之cxf:cxf自定义拦截器相关的知识,希望对你有一定的参考价值。
server方法中
package com.rg2.webservice.impl; import org.apache.cxf.jaxws.JaxWsServerFactoryBean; import com.rg2.intercepter.MyIntercepter; import com.rg2.webservice.HelloWorld; public class Server { public static void main(String[] args) { System.out.println("web service start"); HelloWorld implementor = new HelloWorldImpl(); String address = "http://localhost/helloWorld"; // Endpoint.publish(address, implementor);//jdk实现暴露webservice接口 JaxWsServerFactoryBean factoryBean = new JaxWsServerFactoryBean(); factoryBean.setAddress(address);//设置暴露地址 factoryBean.setServiceClass(HelloWorld.class);//接口类 factoryBean.setServiceBean(implementor);//设置实现类 factoryBean.getInInterceptors().add(new LoggingInInterceptor());//添加in拦截器 日志拦截器 factoryBean.getOutInterceptors().add(new LoggingInInterceptor());//添加out拦截器 日志拦截器 factoryBean.getInInterceptors().add(new MyIntercepter()); factoryBean.create();//创建webservice接口 System.out.println("web service started"); } }
package com.rg2.intercepter; 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; import org.w3c.dom.NodeList; public class MyIntercepter extends AbstractPhaseInterceptor<SoapMessage> { public MyIntercepter() { super(Phase.PRE_INVOKE);//在调用方法之前调用自定义拦截器 } @Override public void handleMessage(SoapMessage message) throws Fault { List<Header> headers = message.getHeaders(); if(null == headers && headers.size() == 0) { throw new Fault(new IllegalArgumentException("没有Header,拦截器实施拦截")); } Header firstHeader = headers.get(0); Element ele = (Element) firstHeader.getObject(); NodeList uList = ele.getElementsByTagName("userName"); NodeList pList = ele.getElementsByTagName("password"); if(uList.getLength()!=1){ throw new Fault(new IllegalArgumentException("用户名格式不对")); } if(pList.getLength()!=1){ throw new Fault(new IllegalArgumentException("密码格式不对")); } String userName = uList.item(0).getTextContent(); String password = pList.item(0).getTextContent(); if(!userName.equals("rg2")||!password.equals("123456")){ throw new Fault(new IllegalArgumentException("用户名或者密码错误!")); } } }
Client
package com.rg2.webservice; import java.util.List; import org.apache.cxf.frontend.ClientProxy; import org.apache.cxf.interceptor.LoggingInInterceptor; import com.rg2.interceptor.AddHeaderIntercepter; public class Client { public static void main(String[] args) { HelloWorldService service = new HelloWorldService(); HelloWorld helloWorldPort = service.getHelloWorldPort(); org.apache.cxf.endpoint.Client client = ClientProxy.getClient(helloWorldPort); client.getOutInterceptors().add(new AddHeaderIntercepter("rg2","123456"));//添加自定义拦截器 client.getInInterceptors().add(new LoggingInInterceptor());//添加in拦截器 日志拦截器 client.getOutInterceptors().add(new LoggingInInterceptor());//添加out拦截器 日志拦截器 MyRoleArray getroles = helloWorldPort.getroles(); List<MyRole> roleList = getroles.item; for (int i = 0; i < roleList.size(); i++) { MyRole my = roleList.get(i); System.out.print(my.key + ":"); for (Role role : my.value) { System.out.print(role.getId()+","+role.getRoleName()); } System.out.println("==============="); } } }
package com.rg2.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.interceptor.Interceptor; import org.apache.cxf.message.Message; import org.apache.cxf.phase.AbstractPhaseInterceptor; import org.apache.cxf.phase.Phase; import org.w3c.dom.Document; import org.w3c.dom.Element; public class AddHeaderIntercepter extends AbstractPhaseInterceptor<SoapMessage> { private String userName; private String password; public AddHeaderIntercepter(String userName, String password) { super(Phase.PREPARE_SEND);// 准备发送SOAP消息的时候调用拦截器 this.userName = userName; this.password = password; } @Override public void handleMessage(SoapMessage message) throws Fault { List<Header> headerList = message.getHeaders(); Document doc = DOMUtils.createDocument(); Element ele = doc.createElement("authHeader"); Element uElement = doc.createElement("userName"); uElement.setTextContent(userName); Element pElement = doc.createElement("password"); pElement.setTextContent(password); ele.appendChild(uElement); ele.appendChild(pElement); headerList.add(new Header(new QName("rg2"), ele)); } }
以上是关于学习webservice之cxf:cxf自定义拦截器的主要内容,如果未能解决你的问题,请参考以下文章
SpringBoot+CXF 实现简单的webservice,并支持Basic验证