Web Service(二):cxf 實現

來源:互聯網
上載者:User

標籤:name   設定密碼   ring   nfa   spring   extension   java   outside   相關   

1. cxf簡介

  Web Services 的一種實現方式。

  Apache CXF = Celtix + XFire,後更名為 Apache CXF ,簡稱為 CXF。

  CXF 繼承了 Celtix 和 XFire 兩大開源項目的精華,提供了對 JAX-WS 全面的支援,並且提供了多種 Binding 、DataBinding、Transport 以及各種 Format 的支援,並且可以根據實際項目的需要,採用代碼優先(Code First)或者 WSDL 優先(WSDL First)來輕鬆地實現 Web Services 的發布和使用。

 

2. JAX-WS

  JAX-WS(Java API for XML Web Services)規範是一組XML web services的JAVA API。

  在 JAX-WS中,一個遠程調用可以轉換為一個基於XML的協議例如SOAP,在使用JAX-WS過程中,*開發人員不需要編寫任何產生和處理SOAP訊息的代碼*。JAX-WS的運行時實現會將這些API的調用轉換成為對應的SOAP訊息。

  * 在伺服器端,使用者只需要通過Java語言定義遠程調用所需要實現的介面SEI(service endpoint interface),並提供相關的實現,通過調用JAX-WS的服務發布介面就可以將其發布為WebService介面。(發布服務  詳見5.1)

  * 在用戶端,使用者可以通過JAX-WS的API建立一個代理(用本機物件來替代遠端服務)來實現對於遠程伺服器端的調用。(建立代理  詳見5.2)

  JAX-WS 也提供了一組針對底層訊息進行操作的API調用,你可以通過Dispatch 直接使用SOAP訊息或XML訊息發送請求或者使用Provider處理SOAP或XML訊息。

 

3. cxf特點

  部署靈活、支援多種程式設計語言、多種傳輸方式。

 

4. 代碼產生

  Java to WSDL;

  WSDL to Java;

  XSD to WSDL;

  WSDL to XML;

  WSDL to SOAP;

  WSDL to Service;

  

5. 實踐

  5.1 伺服器端

  Spring設定檔 cxf.xml

<?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:p="http://www.springframework.org/schema/p"    xmlns:context="http://www.springframework.org/schema/context"    xmlns:task="http://www.springframework.org/schema/task"     xmlns:jaxws="http://cxf.apache.org/jaxws"     xmlns:cxf="http://cxf.apache.org/core"    xsi:schemaLocation="http://www.springframework.org/schema/beans        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd        http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd          http://cxf.apache.org/jaxws    http://cxf.apache.org/schemas/jaxws.xsd">    <!-- cxf webservice -->    <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="loggingInInterceptor" class="org.apache.cxf.interceptor.LoggingInInterceptor" />    <bean id="loggingOutInterceptor" class="org.apache.cxf.interceptor.LoggingOutInterceptor"/>    <cxf:bus>        <cxf:inInterceptors>            <ref bean="loggingInInterceptor"/>        </cxf:inInterceptors>        <cxf:outInterceptors>            <ref bean="loggingOutInterceptor"/>        </cxf:outInterceptors>        <cxf:outFaultInterceptors>            <ref bean="loggingOutInterceptor"/>        </cxf:outFaultInterceptors>        <cxf:inFaultInterceptors>            <ref bean="loggingInInterceptor"/>        </cxf:inFaultInterceptors>    </cxf:bus>     <bean id="serverPasswordCallback" class="util.web.ServerPasswordCallback" />       <bean id="authentication_server" class="org.apache.cxf.ws.security.wss4j.WSS4JInInterceptor">                  <constructor-arg>                      <map>                          <entry key="action" value="UsernameToken" />                          <entry key="passwordType" value="PasswordText" />                          <entry key="user" value="FHDServer" />                          <entry key="passwordCallbackRef">                              <ref bean="serverPasswordCallback" />                          </entry>                      </map>                  </constructor-arg>         </bean>   
<!-- 注意下面的address,這裡的address的名稱就是訪問的WebService的name --> <!-- 外協完工登記 --> <bean id="mesTaskServiceImpl" class="com.outsideasy.ws.mes.wxdata.MesTaskServiceImpl"> </bean> <jaxws:server id="mesTaskServiceInter" serviceClass="com.outsideasy.ws.mes.wxdata.MesTaskServiceInter" address="/wxdata/mesTask"> <jaxws:serviceBean> <ref bean="mesTaskServiceImpl"/> </jaxws:serviceBean> <jaxws:inInterceptors> <ref bean="authentication_server"></ref> </jaxws:inInterceptors> </jaxws:server> <cxf:bus> <cxf:features> <cxf:logging/> </cxf:features> </cxf:bus></beans>

 

 

  ServerPasswordCallback.java

  

package util.web;import java.io.IOException;import java.util.HashMap;import java.util.List;import java.util.Map;import javax.security.auth.callback.Callback;import javax.security.auth.callback.CallbackHandler;import javax.security.auth.callback.UnsupportedCallbackException;import javax.servlet.http.HttpServletRequest;import org.apache.cxf.message.Exchange;import org.apache.cxf.message.Message;import org.apache.cxf.staxutils.W3CDOMStreamWriter;import org.apache.log4j.Logger;import org.apache.ws.security.WSPasswordCallback;import org.apache.ws.security.handler.RequestData;import org.springframework.beans.factory.annotation.Autowired;import org.w3c.dom.Document;import org.w3c.dom.Node;import org.w3c.dom.NodeList;import common.sysmodule.model.WsIdentity;import common.sysmodule.service.WsIdentityService;public class ServerPasswordCallback implements CallbackHandler {    @Autowired    private WsIdentityService wsIdentityService;    protected static Logger logger = Logger.getLogger("service");    public void handle(Callback[] callbacks) throws IOException,            UnsupportedCallbackException {                WSPasswordCallback pc = (WSPasswordCallback) callbacks[0];         String identify = pc.getIdentifier();                //身分識別驗證        List<WsIdentity> list=wsIdentityService.getEnabledIdentity(identify);        if (list!=null && list.size()>0 && list.get(0).getIdentify().equals(identify)) {             pc.setPassword(list.get(0).getPsw());//驗證使用者名稱後,在設定密碼就可以自動驗證        } else {            throw new SecurityException("驗證失敗");        }            }}

 

 

 

 

參考連結:

  1. http://baike.baidu.com/link?url=WOTPK-J7UxJuXZ6hMr8OPYDMItaEHwva8wzOEUSM3wKXF5KgBBXRM8VfgA-kUE_RrAoNMYFipZY0VK_kCtpw8_

 

Web Service(二):cxf 實現

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.