Bläddra i källkod

git-svn-id: https://192.168.57.71/svn/jsgkj@935 931142cf-59ea-a443-aa0e-51397b428577

ld_guxl 8 år sedan
förälder
incheckning
cf65896e8f

+ 79 - 0
gkjsjy/trunk/src/com/cxfws/config/soapheader/AddSoapHeader.java

@@ -0,0 +1,79 @@
+package com.cxfws.config.soapheader;
+
+import org.apache.commons.lang.StringUtils;
+import org.apache.cxf.binding.soap.SoapMessage;
+import org.apache.cxf.binding.soap.interceptor.AbstractSoapInterceptor;
+import org.apache.cxf.interceptor.Fault;
+import org.apache.cxf.phase.Phase;
+import java.util.List;
+import javax.xml.namespace.QName;
+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.Phase;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+
+public class AddSoapHeader extends AbstractSoapInterceptor {
+	
+	@Autowired
+	private WsUserConfig wsUserConfig;
+	
+	public AddSoapHeader(){   
+        super(Phase.WRITE);   
+    }
+	
+	//<soap:Header><tns:RequestSOAPHeader xmlns:tns="http://192.168.39.115:8088/userService"><tns:username>admin</tns:username><tns:password>098f6bcd4621d373cade4e832627b4f6</tns:password></tns:RequestSOAPHeader></soap:Header>
+
+	//<soapenv:Header><userService soapenv:actor="http://www.w3.org/2003/05/soap-envelope/role/next">admin&amp;e10adc3949ba59abbe56e057f20f883e</userService></soapenv:Header>
+	@Override
+	public void handleMessage(SoapMessage message) throws Fault {
+		// TODO Auto-generated method stub
+		System.out.println("--------------handleMessage---------------------");
+		String spPassword="e10adc3949ba59abbe56e057f20f883e";   
+        String spName="admin";
+        
+        if(null!=wsUserConfig&&StringUtils.isNotBlank(wsUserConfig.getWsUser())&&StringUtils.isNotBlank(wsUserConfig.getWsPasswd())){
+        	spPassword=wsUserConfig.getWsPasswd();
+        	spName=wsUserConfig.getWsUser();
+        }
+        
+        /*
+        QName qname=new QName("RequestSOAPHeader");   
+        Document doc=DOMUtils.createDocument();   
+        //自定义节点
+        Element spId=doc.createElement("tns:username");   
+        spId.setTextContent(spName);   
+        //自定义节点
+        Element spPass=doc.createElement("tns:password");   
+        spPass.setTextContent(spPassword);   
+           
+        Element root=doc.createElementNS("http://192.168.39.115:8088/userService", "tns:RequestSOAPHeader");   
+        root.appendChild(spId);   
+        root.appendChild(spPass);   
+          
+        SoapHeader head=new SoapHeader(qname,root);   
+        List<Header> headers=message.getHeaders();   
+        headers.add(head); 
+        */ 
+        
+        QName qname=new QName("RequestSOAPHeader");   
+        Document doc=DOMUtils.createDocument();
+        Element root=doc.createElement("userService");
+        //root.setAttribute("soap:actor", "http://www.w3.org/2003/05/soap-envelope/role/next");
+        root.setAttribute("soap:actor", SOAPConstants.URI_SOAP_1_2_ROLE_NEXT);
+        root.setTextContent(new StringBuffer().append(spName).append("&").append(spPassword).toString());
+
+        SoapHeader head=new SoapHeader(qname,root);   
+
+        List<Header> headers=message.getHeaders();   
+        headers.add(head); 
+      //  head.
+        System.out.println(">>>>>添加header<<<<<<<");
+	}
+
+}

+ 78 - 0
gkjsjy/trunk/src/com/cxfws/config/soapheader/AuthIntercetpr.java

@@ -0,0 +1,78 @@
+package com.cxfws.config.soapheader;
+
+import java.util.List;
+
+import javax.persistence.EntityManager;
+import javax.persistence.PersistenceContext;
+import javax.xml.soap.SOAPException;
+
+import org.apache.commons.lang.StringUtils;
+import org.apache.cxf.binding.soap.SoapHeader;
+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.json.JSONArray;
+import org.json.JSONException;
+import org.json.JSONObject;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.w3c.dom.Element;
+
+import com.cxfws.config.service.IAuthService;
+import com.xtframe.sec.user.entity.UserEntity;
+
+//http://jyao.iteye.com/blog/1343722
+//<soapenv:Header><userService soapenv:actor="http://www.w3.org/2003/05/soap-envelope/role/next">admin&amp;8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92</userService></soapenv:Header>
+
+public class AuthIntercetpr  extends AbstractPhaseInterceptor<SoapMessage>{
+	private final static String VALIDATE_NODE_NAME="userService";
+	
+	@Autowired
+	private IAuthService authService;
+	
+	public AuthIntercetpr() {  
+        // 指定该拦截器在哪个阶段被激发  
+        super(Phase.PRE_INVOKE);  
+    }  
+	@Override
+	public void handleMessage(SoapMessage message) throws Fault {
+		//System.out.println("==================SoapMessage =" + message);
+		// 获取SOAP消息的全部头  
+        List<Header> headers = message.getHeaders();  
+        if (null == headers || headers.size() < 1) {  
+            throw new Fault(new SOAPException("SOAP消息头格式不对哦!"));  
+        }
+
+        boolean validate=false;
+        for (Header header : headers) {  
+            SoapHeader soapHeader = (SoapHeader) header; 
+            //System.out.println("==================header =" + header);
+            
+            Element element = (Element) soapHeader.getObject();  
+            //System.out.println("ELEMENT =" + element.getNodeName()+":::"+element.getTextContent());  
+            if(VALIDATE_NODE_NAME.equalsIgnoreCase(element.getNodeName())){
+            	System.out.println(element.getTextContent());
+            	String[] textContent=StringUtils.split(element.getTextContent(),"&");
+            	if(null != textContent && textContent.length==2){
+            		//validate="admin".equals(textContent[0])&&"e10adc3949ba59abbe56e057f20f883e".equals(textContent[1]);
+            	
+            		List<Object> userEntity=authService.validatepass(textContent[0], textContent[1]);
+            		validate=(null!=userEntity&&!userEntity.isEmpty());
+
+            	}
+            	break;
+            }
+        }
+        
+        if(!validate){
+        	throw new Fault(new SOAPException("用户验证失败!")); 
+        }
+	}
+
+	
+	public static void main(String[] args) throws JSONException {
+		String[] textContent=StringUtils.split("admin&e10adc3949ba59abbe56e057f20f883e","&");
+		System.out.println(new JSONArray(textContent).toString());
+	}
+}

+ 207 - 0
gkjsjy/trunk/src/com/cxfws/config/soapheader/SOAPConstants.java

@@ -0,0 +1,207 @@
+/*
+ * Copyright (c) 1997, 2008, Oracle and/or its affiliates. All rights reserved.
+ * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. 
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ */
+/* 
+ * $Id: SOAPConstants.java,v 1.12 2005/04/05 22:28:13 mk125090 Exp $
+ * $Revision: 1.12 $
+ * $Date: 2005/04/05 22:28:13 $
+ */
+
+
+package com.cxfws.config.soapheader;
+
+import javax.xml.namespace.QName;
+
+/**
+ * The definition of constants pertaining to the SOAP protocol. 
+ */
+public interface SOAPConstants {
+    /**
+     * Used to create <code>MessageFactory</code> instances that create
+     * <code>SOAPMessages</code> whose concrete type is based on the
+     * <code>Content-Type</code> MIME header passed to the
+     * <code>createMessage</code> method. If no <code>Content-Type</code>
+     * header is passed then the <code>createMessage</code> may throw an
+     * <code>IllegalArgumentException</code> or, in the case of the no
+     * argument version of <code>createMessage</code>, an
+     * <code>UnsupportedOperationException</code>.
+     *
+     * @since  SAAJ 1.3
+     */
+    public static final String DYNAMIC_SOAP_PROTOCOL = "Dynamic Protocol";
+
+    /**
+     * Used to create <code>MessageFactory</code> instances that create
+     * <code>SOAPMessages</code> whose behavior supports the SOAP 1.1  specification.
+     *
+     * @since  SAAJ 1.3
+     */
+    public static final String SOAP_1_1_PROTOCOL = "SOAP 1.1 Protocol";
+
+    /**
+     * Used to create <code>MessageFactory</code> instances that create
+     * <code>SOAPMessages</code> whose behavior supports the SOAP 1.2
+     * specification
+     *
+     * @since  SAAJ 1.3
+     */
+    public static final String SOAP_1_2_PROTOCOL = "SOAP 1.2 Protocol";
+
+    /**
+     * The default protocol: SOAP 1.1 for backwards compatibility.
+     *
+     * @since SAAJ 1.3
+     */
+    public static final String DEFAULT_SOAP_PROTOCOL = SOAP_1_1_PROTOCOL;
+
+    /**
+     * The namespace identifier for the SOAP 1.1 envelope.
+     * @since SAAJ 1.3
+     */
+    public static final String
+		URI_NS_SOAP_1_1_ENVELOPE = "http://schemas.xmlsoap.org/soap/envelope/";
+    /**
+     * The namespace identifier for the SOAP 1.2 envelope.
+     * @since SAAJ 1.3
+     */
+    public static final String
+		URI_NS_SOAP_1_2_ENVELOPE = "http://www.w3.org/2003/05/soap-envelope";
+
+    /**
+     * The namespace identifier for the SOAP 1.1 envelope, All SOAPElements in this 
+     * namespace are defined by the SOAP 1.1 specification.
+     */
+    public static final String
+        URI_NS_SOAP_ENVELOPE = URI_NS_SOAP_1_1_ENVELOPE;
+
+    /**
+     * The namespace identifier for the SOAP 1.1 encoding.
+     * An attribute named <code>encodingStyle</code> in the
+     * <code>URI_NS_SOAP_ENVELOPE</code> namespace and set to the value
+     * <code>URI_NS_SOAP_ENCODING</code> can be added to an element to indicate
+     * that it is encoded using the rules in section 5 of the SOAP 1.1
+     * specification.
+     */
+    public static final String
+        URI_NS_SOAP_ENCODING = "http://schemas.xmlsoap.org/soap/encoding/";
+
+    /**
+     * The namespace identifier for the SOAP 1.2 encoding.
+     * @since SAAJ 1.3
+     */
+    public static final String
+        URI_NS_SOAP_1_2_ENCODING = "http://www.w3.org/2003/05/soap-encoding";
+
+    /**
+     * The media type  of the <code>Content-Type</code> MIME header in SOAP 1.1.
+     * @since SAAJ 1.3
+     */
+    public static final String
+        SOAP_1_1_CONTENT_TYPE = "text/xml";
+
+    /**
+     * The media type  of the <code>Content-Type</code> MIME header in SOAP 1.2.
+     * @since SAAJ 1.3
+     */
+    public static final String
+        SOAP_1_2_CONTENT_TYPE = "application/soap+xml";
+
+    /**
+     * The URI identifying the next application processing a SOAP request as the intended
+     * actor for a SOAP 1.1 header entry (see section 4.2.2 of the SOAP 1.1 specification).
+     * <p>
+     * This value can be passed to
+     * {@link SOAPHeader#examineMustUnderstandHeaderElements(String)},
+     * {@link SOAPHeader#examineHeaderElements(String)} and
+     * {@link SOAPHeader#extractHeaderElements(String)}
+     */
+    public static final String
+        URI_SOAP_ACTOR_NEXT = "http://schemas.xmlsoap.org/soap/actor/next";
+
+    /**
+     * The URI identifying the next application processing a SOAP request as the intended
+     * role for a SOAP 1.2 header entry (see section 2.2 of part 1 of the SOAP 1.2
+     * specification).
+     * @since SAAJ 1.3
+     */
+    public static final String
+        URI_SOAP_1_2_ROLE_NEXT = URI_NS_SOAP_1_2_ENVELOPE + "/role/next";
+
+    /**
+     * The URI specifying the role None in SOAP 1.2.
+     * @since SAAJ 1.3
+     */
+    public static final String
+        URI_SOAP_1_2_ROLE_NONE = URI_NS_SOAP_1_2_ENVELOPE + "/role/none";
+
+    /**
+     * The URI identifying the ultimate receiver of the SOAP 1.2 message.
+     * @since SAAJ 1.3
+     */
+    public static final String
+        URI_SOAP_1_2_ROLE_ULTIMATE_RECEIVER =
+            URI_NS_SOAP_1_2_ENVELOPE + "/role/ultimateReceiver";
+
+    /**
+     * The default namespace prefix for http://www.w3.org/2003/05/soap-envelope
+     * @since SAAJ 1.3
+     */
+    public static final String SOAP_ENV_PREFIX = "env";
+
+    /**
+     * SOAP 1.2 VersionMismatch Fault
+     * @since SAAJ 1.3
+     */
+    public static final QName SOAP_VERSIONMISMATCH_FAULT =
+         new QName(URI_NS_SOAP_1_2_ENVELOPE, "VersionMismatch", SOAP_ENV_PREFIX);
+                                                                                                                  
+    /**
+     * SOAP 1.2 MustUnderstand Fault
+     * @since SAAJ 1.3
+     */
+    public static final QName SOAP_MUSTUNDERSTAND_FAULT =
+         new QName(URI_NS_SOAP_1_2_ENVELOPE, "MustUnderstand", SOAP_ENV_PREFIX);
+                                                                                                                  
+    /**
+     * SOAP 1.2 DataEncodingUnknown Fault
+     * @since SAAJ 1.3
+     */
+    public static final QName SOAP_DATAENCODINGUNKNOWN_FAULT =
+         new QName(URI_NS_SOAP_1_2_ENVELOPE, "DataEncodingUnknown", SOAP_ENV_PREFIX);
+                                                                                                                  
+    /**
+     * SOAP 1.2 Sender Fault
+     * @since SAAJ 1.3
+     */
+    public static final QName SOAP_SENDER_FAULT =
+         new QName(URI_NS_SOAP_1_2_ENVELOPE, "Sender", SOAP_ENV_PREFIX);
+                                                                                                                  
+    /**
+     * SOAP 1.2 Receiver Fault
+     * @since SAAJ 1.3
+     */
+    public static final QName SOAP_RECEIVER_FAULT =
+         new QName(URI_NS_SOAP_1_2_ENVELOPE, "Receiver", SOAP_ENV_PREFIX);
+                                                                                                                  
+}

+ 39 - 0
gkjsjy/trunk/src/com/cxfws/config/soapheader/WsUserConfig.java

@@ -0,0 +1,39 @@
+package com.cxfws.config.soapheader;
+
+import javax.annotation.PostConstruct;
+
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.context.annotation.Lazy;
+import org.springframework.stereotype.Component;
+
+@Component
+@Lazy(false)
+public class WsUserConfig {
+	
+	private String wsUser;
+	
+	private String wsPasswd;
+	
+	@PostConstruct
+	public void init(){
+		System.out.println("-----------WsUserConfig----------"+wsUser+":::"+wsPasswd);
+	}
+
+	public String getWsUser() {
+		return wsUser;
+	}
+	@Value("${wsname:null}")
+	public void setWsUser(String wsUser) {
+		this.wsUser = wsUser;
+	}
+
+	public String getWsPasswd() {
+		return wsPasswd;
+	}
+	@Value("${wspasswd:null}")
+	public void setWsPasswd(String wsPasswd) {
+		this.wsPasswd = wsPasswd;
+	}
+	
+	
+}