Webserive Study Record 5-Interceptor Complete Login Check

Source: Internet
Author: User
Tags soap tagname

  Talk about CXF in the interceptor, can be divided into system interceptors (such as log interceptors) and custom interceptors, can also be divided into interceptors and into interceptors, can also be divided into server interceptors and client interceptors.

The following will implement a login to verify the Interceptor, where the user name is passed as a method parameter, the password is placed in the header of the XML sent to the server.

  Service side

The code structure is as follows:

ValidUser:

ApplicationContext

Cxfservices

<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans" xmlns: Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws= "Http://cxf.apache.org/jaxws" xmlns:soap= "http// Cxf.apache.org/bindings/soap "xsi:schemalocation=" Http://www.springframework.org/schema/beans/http Www.springframework.org/schema/beans/spring-beans.xsd Http://cxf.apache.org/jaxws Http://cxf.apache.org/schem As/jaxws.xsd http://cxf.apache.org/bindings/soap http://cxf.apache.org/schemas/configuration/soap.xsd ">&lt ;! --1. Use the configuration of the jaxws:endpoint tag to publish a WebService 2. Configure the service with the Implementor property to provide a relative path to the implementation class 3.address property configuration external Access 4. Using Jaxws:ininterceptors The label is configured with 2 log interceptors, which are used to print the log information at the time of the call 5. Note: In this configuration file, you need to include JAXWS with the soap namespace--><!--<import resource= "classpath*:meta-inf/cxf /cxf.xml "/> <import resource=" Classpath*:meta-inf/cxf/cxf-extension-soap.xml "/> <import resource=" Classpath*:meta-inf/cxf/cxf-servlet.xml "/>--><bean id=" Jaxwsservicefactorybean "class= "Org.apache.cxf.jaxws.support.JaxWsServiceFactoryBean" ><property name= "Wrapped" value= "true"/> </bean><jaxws:endpoint id= "Logincheckservice" implementor= "Login.check.ws.impl.LoginCheck" address= "/ Logincheck "><jaxws:ininterceptors><bean id=" Inlogginginterceptor "class=" Org.apache.cxf.interceptor.LoggingInInterceptor "/><bean id=" Outlogginginterceptor "class=" Org.apache.cxf.interceptor.LoggingOutInterceptor "/><bean id=" Logincheckinterceptor "class=" Login.check.ws.impl.LoginCheck "/></jaxws:ininterceptors><jaxws:servicefactory><ref bean="   Jaxwsservicefactorybean "/></jaxws:servicefactory></jaxws:endpoint></beans>

Web. xml file

<?xml version= "1.0" encoding= "UTF-8"? ><web-app xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns= "Http://java.sun.com/xml/ns/javaee" xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee/http Java.sun.com/xml/ns/javaee/web-app_3_0.xsd "id=" webapp_id "version=" 3.0 "> <display-name>logincheckws< /display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file >index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file> Default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>       Default.jsp</welcome-file> </welcome-file-list> <!--join Spring--<context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationcontext*.xml </param-value> </context-param> <listener> <listEner-class>org.springframework.web.context.contextloaderlistener</listener-class> </listener> &lt ;! --Join CXF Support-<servlet> <description>apache CXF endpoint</description> <dis Play-name>cxf</display-name> <servlet-name>cxf</servlet-name> <servlet-class> Org.apache.cxf.transport.servlet.cxfservlet</servlet-class> <load-on-startup>1</load-on-startup&      Gt </servlet> <servlet-mapping> <servlet-name>cxf</servlet-name> <url-patte Rn>/services/*</url-pattern> </servlet-mapping> </web-app>

Logincheck: Inherits the Abstractphaseinterceptor, implements the Handlemessage method, obtains the password from the client to the SOAP message header, and is saved in the threadlocal variable for use when validating the user.

Package Login.check.ws.impl;import Java.util.list;import Javax.jws.webservice;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;import Org.w3c.dom.nodelist;import Login.check.constant.validuser;import Login.check.ws.interf.ILoginCheck; @WebService (endpointinterface= " Login.check.ws.interf.ILoginCheck ", targetnamespace=" http://login.check.ws ") public class Logincheck extends Abstractphaseinterceptor<soapmessage> implements ilogincheck{private static final threadlocal<string> passwords = new threadlocal<> ();p ublic Logincheck () {super (phase.pre_protocol);} @Overridepublic boolean CheckUser (String username) {return ValidUser.validUser.get (username). Equals (Passwords.get () );} @Overridepublic void Handlemessage (SoapMessage message) throws Fault {List

Client

The first is to generate the client code, not here,

    

It then customizes an interceptor, writes the password information to the SOAP message header, the threadlocal variable used in the password, and the password in the test class into the threadlocal variable, and then the password is available in the interceptor.

The interceptor code is as follows:

Package Login.check.ws.client;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.phase.abstractphaseinterceptor;import Org.apache.cxf.phase.phase;import org.w3c.dom.Document; Import Org.w3c.dom.element;import Login.check.ws.test.logintest;public class Passwordinterceptor extends abstractphaseinterceptor<soapmessage> {public passwordinterceptor () {super (phase.prepare_send);} @Overridepublic void Handlemessage (SoapMessage message) throws Fault {list

The test code is as follows:

Three interceptors were used and the passwords were stored in the threadlocal,

The test results are as follows:

Code Address: Https://files.cnblogs.com/files/liunianfeiyu/logincheckwebservice.rar

Webserive Study Record 5-Interceptor Complete Login Check

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.