Cxf Implements user verification (compared to the previous method of completely spring)

Source: Internet
Author: User

1 ...................................... server-side validation class

Package interceptor; import javax. XML. soap. soapexception; import javax. XML. soap. soapheader; import javax. XML. soap. soapmessage; import Org. apache. cxf. binding. soap. soapmessage; import Org. apache. cxf. binding. soap. SAAJ. saajininterceptor; import Org. apache. cxf. interceptor. fault; import Org. apache. cxf. phase. abstractphaseinterceptor; import Org. apache. cxf. phase. phase; import Org. w3C. dom. nodelist; public class authorinterceptor extends actphaseinterceptor <soapmessage> {private saajininterceptor SAA = new saajininterceptor (); Public authorinterceptor () {super (phase. pre_protocol); getafter (). add (saajininterceptor. class. getname ();} public void handlemessage (soapmessage message) throws fault {system. out. println ("come in servicesauthorinterceptor"); soapmessage mess = message. getcontent (soapmessage. class); If (mess = NULL) {SAA. handlemessage (Message); mess = message. getcontent (soapmessage. class);} soapheader head = NULL; try {head = mess. getsoapheader ();} catch (exception e) {e. printstacktrace ();} If (Head = NULL) {return;} nodelist nodes = head. getelementsbytagname ("TNS: spid"); nodelist nodepass = head. getelementsbytagname ("TNS: sppassword"); If ("Zhong ". equals (nodes. item (0 ). gettextcontent () & ("guo ". equals (nodepass. item (0 ). gettextcontent () {system. out. println ("authentication successful");} else {soapexception soapexc = new soapexception ("authentication error"); throw new fault (soapexc);} system. out. println ("6 ");}}

 

2 ................................... configure the XML file of the Public Filter

<! -- Public filter --> <cxf: Bus> <cxf: ininterceptors> <ref bean = "authorinterceptor"/> </cxf: ininterceptors> </cxf: Bus>

In this way, the client automatically performs user verification when calling the interface.

 

3 .................................... write the client Verification Code

package InterCeptor;import org.apache.cxf.binding.soap.SoapHeader;import org.apache.cxf.binding.soap.SoapMessage;import org.apache.cxf.binding.soap.interceptor.AbstractSoapInterceptor;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 javax.xml.XMLConstants;import javax.xml.namespace.QName;import java.text.SimpleDateFormat;import java.util.Date;import java.util.List;public class HeaderIntercepter extends AbstractSoapInterceptor {private String qname;public HeaderIntercepter() {super(Phase.WRITE);}public void handleMessage(SoapMessage soapMessage) throws Fault {System.out.println("come in ClientHeaderIntercepter");String spName = "zhong";String spPassword = "guo";QName name = new QName("RequestSOAPHeader");Document doc = DOMUtils.createDocument();Element spId = doc.createElement("tns:spId");spId.setTextContent(spName);Element spPass = doc.createElement("tns:spPassword");spPass.setTextContent(spPassword);Element root = doc.createElementNS(qname, "tns:RequestSOAPHeader");root.appendChild(spId);root.appendChild(spPass);SoapHeader head = new SoapHeader(name, root);List<Header> headers = soapMessage.getHeaders();headers.add(head);}private Object getHeader() {QName qName = new QName("", "", "");Document document = DOMUtils.createDocument();Element element = document.createElementNS(qname, "RequestSOAPHeader");Element token = document.createElement("token");token.setTextContent("kkkkk");// element.appendChild(token);SoapHeader header = new SoapHeader(qName, token);return (header);}public String getQname() {return qname;}public void setQname(String qname) {this.qname = qname;}}

4 .......................... automatically generate a server interface proxy through wsdl2java

5 ...... the XML ing file of spring is configured on the client.

 

<?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"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/schemas/jaxws.xsd"><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="inMessageInterceptor" class="InterCeptor.HeaderIntercepter"><property name="qname"value="http://localhost:8080/SpringCXF/services/GetUserInfo" /></bean><jaxws:client id="com.client.GetUserInfo" serviceClass="objectinterface.GetUserInfo"address="http://localhost:8080/SpringCXF/services/GetUserInfo"><jaxws:outInterceptors><ref bean="inMessageInterceptor" /></jaxws:outInterceptors></jaxws:client></beans>

 

6 ............................ the interface is called directly and then OK.

package com.client;import objectinterface.GetUserInfo;import objectinterface.User;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class GetUserInfoTest {public static void main(String[] args) throws Exception {User user = new User();ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext-client.xml");GetUserInfo userInfo = ctx.getBean("com.client.GetUserInfo", GetUserInfo.class);String xmlDoc = userInfo.xmlInfo();user = userInfo.getUserInfo("sa");System.out.println(user.getUsername());System.out.println("success");System.out.println(xmlDoc);}}

 

7.

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.