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

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 extends AbstractPhaseInterceptor<SoapMessage> {
 
     private SAAJInInterceptor saa = new SAAJInInterceptor();
 
     public HeaderAuthInterceptor() {
         super (Phase.PRE_PROTOCOL);
         getAfter().add(SAAJInInterceptor. class .getName());
     }
 
     public void 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();
         }
         throw new Fault( new Exception( "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" ?>
     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" />
  
     <!-- 集成配置 -->
     < 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" />
     
     <jaxws:endpoint id= "ws" implementor= "#helloService" address= "/helloService" >
         <jaxws:features> 
             <!-- 日志拦截器 -->
             <bean class = "org.apache.cxf.feature.LoggingFeature" /> 
         </jaxws:features>
         <jaxws:inInterceptors>
             <!-- 自定义拦截器 -->
            <bean class = "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 extends AbstractSoapInterceptor{
 
     public ClientAuthInterceptor(){
         super (Phase.WRITE);
     }
     
     public void 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= new QName( "AuthToken" );
         SoapHeader head= new SoapHeader(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
      */
     public static void main(String[] args) {
         JaxWsProxyFactoryBean soapFactoryBean = new JaxWsProxyFactoryBean();
         soapFactoryBean.setAddress( "http://127.0.0.1:8080/cxf/helloService" );
         soapFactoryBean.getInInterceptors().add( new LoggingInInterceptor());
         soapFactoryBean.getOutInterceptors().add( new LoggingOutInterceptor());
         // 添加拦截器
         soapFactoryBean.getOutInterceptors().add( new ClientAuthInterceptor());
         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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值