Java CXF WebService框架 集成Spring3 自定义拦截器进行头验证

来源:互联网 发布:cutecom串口发送数据 编辑:程序博客网 时间:2024/05/22 06:06
CXF框架的拦截器特性,我们通过CXF拦截器特性实现Header验证。CXF版本: cxf-2.7.15 服务器端案例代码:HeaderAuthInterceptor.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package com.what21.cxf.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.Node;
import org.w3c.dom.NodeList;
 
public class HeaderAuthInterceptor extendsAbstractPhaseInterceptor<SoapMessage> {
 
    privateSAAJInInterceptor saa = newSAAJInInterceptor();
 
    publicHeaderAuthInterceptor() {
        super(Phase.PRE_PROTOCOL);
        getAfter().add(SAAJInInterceptor.class.getName());
    }
 
    publicvoid handleMessage(SoapMessage message)throws Fault {
        SOAPMessage soapMsg = message.getContent(SOAPMessage.class);
        if(soapMsg == null){
            saa.handleMessage(message);
            soapMsg = message.getContent(SOAPMessage.class);
        }
        SOAPHeader header =null;
        try{
            header = soapMsg.getSOAPHeader();
            NodeList userNodes = header.getElementsByTagName("username");
            NodeList passNodes = header.getElementsByTagName("password");
            if(userNodes!=null&& passNodes!=null){
                Node userNode = userNodes.item(0);
                Node passNode = passNodes.item(0);
                String username = userNode.getTextContent();
                String password = passNode.getTextContent();
                System.out.println("username : "+ username + ",password : "+ password);
                return;
            }
        }catch (SOAPException e) {
            e.printStackTrace();
        }
        thrownew Fault(newException("auth error."));
    }
     
}
cxf-ws.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<?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">
     
    <!-- webservice bean -->
    <bean id="helloService"class="com.what21.cxf.spring.HelloService"/>
  
    <!-- 集成配置 -->
    <importresource="classpath:META-INF/cxf/cxf.xml"/>
    <importresource="classpath:META-INF/cxf/cxf-extension-soap.xml"/>
    <importresource="classpath:META-INF/cxf/cxf-servlet.xml"/>
     
    <jaxws:endpoint id="ws"implementor="#helloService"address="/helloService">
        <jaxws:features> 
            <!-- 日志拦截器 -->
            <beanclass="org.apache.cxf.feature.LoggingFeature"/> 
        </jaxws:features>
        <jaxws:inInterceptors>
            <!-- 自定义拦截器 -->
            <beanclass="com.what21.cxf.interceptor.HeaderAuthInterceptor"/> 
        </jaxws:inInterceptors>
    </jaxws:endpoint>
     
</beans>
 客户端代码案例:ClientAuthInterceptor.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package com.what21.cxf.interceptor;
 
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.w3c.dom.Document;
import org.w3c.dom.Element;
 
 
public class ClientAuthInterceptor extendsAbstractSoapInterceptor{
 
    publicClientAuthInterceptor(){
        super(Phase.WRITE);
    }
     
    publicvoid handleMessage(SoapMessage soapMessage)throws Fault {
        Document doc=DOMUtils.createDocument();
        // 根节点
        Element rootEle=doc.createElementNS("","AuthToken");
        // 用户ID
        Element userEle = doc.createElement("username");
        userEle.setTextContent("username");
        rootEle.appendChild(userEle);
        // 密码
        Element passEle = doc.createElement("password");
        passEle.setTextContent("password");
        rootEle.appendChild(passEle);
        // 添加到头
        List<Header> headers = soapMessage.getHeaders();
        QName qname=newQName("AuthToken");
        SoapHeader head=newSoapHeader(qname, rootEle);
        headers.add(head);
    }
 
}
CxfClient.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package com.what21.cxf.interceptor;
 
import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
 
import com.what21.cxf.spring.IHello;
 
public class CxfClient {
 
    /**
     * @param args
     */
    publicstatic void main(String[] args) {
        JaxWsProxyFactoryBean soapFactoryBean =new JaxWsProxyFactoryBean();
        soapFactoryBean.setAddress("http://127.0.0.1:8080/cxf/helloService");
        soapFactoryBean.getInInterceptors().add(newLoggingInInterceptor());
        soapFactoryBean.getOutInterceptors().add(newLoggingOutInterceptor());
        // 添加拦截器
        soapFactoryBean.getOutInterceptors().add(newClientAuthInterceptor());
        soapFactoryBean.setServiceClass(IHello.class);
         
        Object o = soapFactoryBean.create();
        IHello helloService = (IHello)o;
        String result =null;
        try{
            result = helloService.message("i am client!");
        }catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(result);
    }
 
}
SOAP消息格式:
1
2
3
4
5
6
7
8
9
10
11
12
13
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    <soap:Header>
        <AuthToken>
            <username>username</username>
            <password>password</password>
        </AuthToken>
    </soap:Header>
    <soap:Body>
        <ns2:message xmlns:ns2="http://spring.cxf.what21.com/">
            <arg0>i am client!</arg0>
        </ns2:message>
    </soap:Body>
</soap:Envelope>
0 0
原创粉丝点击