WebService Learning Notes-CXF adding custom interceptors

Source: Internet
Author: User

Using a custom interceptor to verify the user name and password

Client: Out Interceptor

Server: Into the Interceptor


Client

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;//using xalan-2.7.1.jarimport  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);//  to intercept This.username = username;this.password = password when preparing for protocol;} /* *  message Format   <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> *  * */@ Overridepublic void handlemessage (soapmessage message)  throws Fault {List< Header> headers = message.getheaders ();D ocument document =  Domhelper.createdocument (); Element rootelement = document.createelement ("Atguigu"); Element usernameelement = document.createelement ("username"); Usernameelement.settextcontent ( username); Rootelement.appendchild (usernameelement); Element passwordelement = document.createelement ("password");p asswordelement.settextcontent ( Password); Rootelement.appendchild (passwordelement); Headers.add (New header (New qname ("Atguigu"),  rootelement)); System.out.println ("intercept ...");}

interceptorhelloworldclient.java   Client Add interceptors

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/5778915public  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  go to  cxf api   Add log Blocker org.apache.cxf.endpoint.client client =  Org.apache.cxf.frontend.ClientProxy.getClient (HW); org.apache.cxf.endpoint.endpoint cxfendpoint =  client.getendpoint ();//Add Log Interceptor Cxfendpoint.getoutinterceptors (). Add (New loggingoutinterceptor ());// Add a custom Interceptor 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);}}

Server-side

Checkuserinterceptor.java Interceptor

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;//checking the user's   interceptor public class  checkuserinterceptor extends abstractphaseinterceptor<soapmessage> {public  Checkuserinterceptor ()  {super (Phase.pre_protocol);//  ready for Protocol interception} @Overridepublic  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 ("Through interceptors ..."); return;}} Cannot pass System.out.println ("No Interceptor"); Throw new fault (New runtimeexception ("Username or password is incorrect");}}


Interceptorserverapp.java adding server-side interceptors to the service side

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  go to  cxf API  Add log Blocker endpointimpl jaxwsendpointimpl =  (Endpointimpl)  endpoint;o Rg.apache.cxf.endpoint.server server = jaxwsendpointimpl.getserver (); O Rg.apache.cxf.endpoint.endpoint cxfendpoint = server.getendpoint ();                 //Add Log Interceptor Loggingininterceptor logging = new loggingininterceptor (); Cxfendpoint.getininterceptors (). Add (logging);//Adding Custom Interceptors Cxfendpoint.getininterceptors (). Add (new  Checkuserinterceptor ()); System.out.println ("web service started");}}


Refer to the Still Silicon Valley tutorial

This article is from "Avatar" blog, please make sure to keep this source http://shamrock.blog.51cto.com/2079212/1563384

WebService Learning Notes-CXF adding custom interceptors

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.