Settings for "WebService" CXF interceptors and custom CXF interceptors

Source: Internet
Author: User
Tags soap wsdl

WebService Series Articles:
"WebService" takes you into the WebService world.
"WebService" Custom WebService service and its invocation
"WebService" WSDL configuration in detail and using annotations to modify the WSDL configuration
"WebService" CXF processing complex types such as JavaBean and map data

A CXF interceptor is similar to a previously learned servlet interceptor, starting or ending with a piece of code, performing some logic, and so on. We can set the interceptor before calling the WS service, or we can set the interceptor after calling the WS service, of course, the interceptor can also add multiple, CXF has its own built-in interceptor, first to write a simple cxf comes with the interceptor instance familiar with how to add in Cxf, and then customize the CXF interceptor.

1. CXF built-in interceptor settings

Or use the previous section of WS to add the following interceptors on the original basis, as follows:

After startup, the client accesses the server console output (I will not zoom out for clarity):

Can be seen, the request will be intercepted by the interceptor, the end of the request will be intercepted, from the printed log message can be seen, send a SOAP message, the returned data due to the scope of the screenshot I do not intercept, this is the server added interceptors.
How does the Client add interceptors? Since the client side cannot get the Interceptor group directly, we need to obtain a proxy for the client and then get the Interceptor group through this proxy, as follows:

Client access, look at the output of the client console (for clarity, I will not zoom out):

As you can see, the client will print out the log message if the interceptor is set, and the execution order of the interceptor on the client and the server is reversed. This is the CXF built-in interceptor, and below we come from the interceptor that defines the CXF.

2. Custom CXF interceptors

To customize the interceptor, let's get a requirement and use the interceptor to authenticate the permissions. Custom interceptors need to be inherited AbstractPhaseInterceptor<SoapMessage> , where SoapMessage is used to encapsulate the SOAP message, and we'll look at how to customize the Cxf interceptor, starting with the server, adding a custom interceptor below the two built-in interceptors defined in the code above:

factoryBean.getInInterceptors().add(new MyInterceptor());

Then the focus is on this myinterceptor, as follows:

 Public  class myinterceptor extends abstractphaseinterceptor<soapmessage > {     Public Myinterceptor() {Super(Phase.pre_invoke);//Call a custom interceptor before calling the method} Public void Handlemessage(SoapMessage message)throwsFault {list//Get headers based on SOAP messages        if(Headers = =NULL&& headers.size () = =0) {Throw NewFault (NewIllegalArgumentException ("No header, interceptor interception.")); } Header Firstheader = Headers.get (0);//We'll just send one head over.Element elm = (Element) firstheader.getobject ();//Turn the head into an element objectNodeList userlist = Elm.getelementsbytagname ("username");//Get values based on tagsNodeList pwdlist = Elm.getelementsbytagname ("Password");//Identity Verification        if(Userlist.getlength ()! =1) {//Only one user            Throw NewFault (NewIllegalArgumentException ("User name is not formatted")); }if(Pwdlist.getlength ()! =1) {//Only one password            Throw NewFault (NewIllegalArgumentException ("The password is not in the correct format")); } String username = Userlist.item (0). Gettextcontent ();//Because it's one, so get the first oneString password= Pwdlist.item (0). Gettextcontent ();if(!username.equals ("Admin") || !password.equals ("123")) {Throw NewFault (NewIllegalArgumentException ("User name or password error")); }    }}

The above code logic is very simple, wait for the client to pass a SOAP message, we will enclose the user name and password in the head to pass, then on this side, by parsing the SOAP message in the head of the data, to authenticate. So next, complete the interceptor on the client side.
Client side to customize an out interceptor, because this is to send data, ibid, first in the original client-defined two built-in CXF interceptor above the above add a custom interceptor, as follows:

client.getOutInterceptors().add(new AddHeaderInterceptor("admin""123"));//添加自定义拦截器

In the custom interceptor, pass in the username and password, focusing on this custom interceptor, as follows:

 Public  class addheaderinterceptor extends abstractphaseinterceptor<  SoapMessage> {    PrivateString username;PrivateString password; Public Addheaderinterceptor(string username, string password) {Super(Phase.prepare_send);//When you are ready to send a SOAP message, call the Interceptor         This. Username = Username; This. Password = password; } Public void Handlemessage(SoapMessage message)throwsFault {list//define three objectsElement Elm = Doc.createelement ("Authheader"); Element Userelm = doc.createelement ("username"); Element Pwdelm = doc.createelement ("Password");//Assign a value to the user name and password objectUserelm.settextcontent (username); Pwdelm.settextcontent (password);//Add the user name and password object to the ElmElm.appendchild (Userelm);        Elm.appendchild (Pwdelm); Headerlist.add (NewHeader (NewQName ("Head"), Elm));//Add this elm element to the header of the SOAP message}}

As can be seen from the above code, first through the constructor of the user name and password into, and then get the SOAP message will be sent to the head, and then thought to construct a few elements, the user name and password encapsulated into the element, and placed in the header of the SOAP message, This will be the SOAP message will carry the user name and password message, so that can be taken out on the server above, identity authentication, so that the back and forth to connect. Test results I will not post, you can view the results of the console printing, focusing on the SOAP message, which encapsulates a DOM object, encapsulating the user name and password.

-Willing to share and progress together!
--My Blog home: http://blog.csdn.net/eson_15

Settings for "WebService" CXF interceptors and custom CXF 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.