首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> Java 相關知識

Apache Geronimo 實現與部署 J2EE Web 服務:從 WSDL 到安全攔截器

     編輯:Java 相關知識

業務場景:資金轉帳 Web 服務化

現有銀行模擬系統(EJB 實現),需暴露為 SOAP/HTTP Web 服務,支援審計員監管。

Web 服務構件清單

  1. WSDL 文檔(描述接口/端口)
  2. 服務端點接口(實現 java.rmi.Remote,拋 RemoteException)
  3. JAX-RPC 映射(XML/Java 類型映射)
  4. 服務實現(EJB 或 Servlet)
  5. webservices.xml 部署描述符

開發流程

1. 從端點接口生成 WSDL (wscompile)

接口繼承 java.rmi.Remote,方法拋 RemoteException,參數/返回值為 JAX-RPC 相容類型(JavaBean + 原始類型)。

<configuration xmlns="https://java.sun.com/xml/ns/jax-rpc/ri/config">
    <service name="BusinessLogic"
        targetNamespace="https://ibm.com/samples/workdev/v1/wsdl"
        typeNamespace="https://ibm.com/samples/workdev/v1/types"
        packageName="com.ibm.workdev.v1.interfaces">
        <interface name="com.ibm.workdev.v1.interfaces.BusinessLogicEndpoint"/>
    </service>
</configuration>
wscompile -classpath ... -gen:server -f:documentliteral config.xml

2. 手工修正 WSDL

  • 參數名優化:long_2 → amount。
  • 無返回值方法需添加空 complexType 並關聯到響應消息部件。

3. 生成 JAX-RPC 映射

<configuration xmlns="...">
    <wsdl location="file:///BusinessLogic.wsdl" packageName="com.ibm.workdev.v1.interfaces"/>
</configuration>
wscompile -s src-gen -keep -mapping mapping.xml -classpath ... -gen:client -f:documentliteral config.xml

後處理:修正 wsdl-message-part-name(從 parameters 改為實際元素名),刪除 void 方法的 wsdl-return-value-mapping。

4. 實現端點接口與安全攔截器

Servlet 實現類 BusinessLogicServiceServlet 調用 EJB。安全攔截器 WSSecurityServerHandler 實現 javax.xml.rpc.handler.Handler:

  • 從 SOAP Header 提取 UsernameToken。
  • 放入 ThreadLocal,Servlet 取出建立 LoginContext,以認證主體身份 Subject.doAs 調用 EJB。

5. 配置 webservices.xml

<webservices xmlns="https://java.sun.com/xml/ns/j2ee" version="1.1">
    <webservice-description>
        <webservice-description-name>BusinessLogic</webservice-description-name>
        <wsdl-file>WEB-INF/BusinessLogic.wsdl</wsdl-file>
        <jaxrpc-mapping-file>WEB-INF/jaxrpc-mapping.xml</jaxrpc-mapping-file>
        <port-component>
            <port-component-name>BusinessLogicEndpointPort</port-component-name>
            <wsdl-port xmlns:ns="...">ns:BusinessLogicEndpointPort</wsdl-port>
            <service-endpoint-interface>com.ibm.workdev.v1.interfaces.BusinessLogicEndpoint</service-endpoint-interface>
            <service-impl-bean><servlet-link>BusinessLogicServiceServlet</servlet-link></service-impl-bean>
            <handler>
                <handler-name>ServerSecurityHandler</handler-name>
                <handler-class>com.ibm.workdev.v1.web.wssec.WSSecurityServerHandler</handler-class>
                <soap-header xmlns:wssec="...">wssec:Security</soap-header>
            </handler>
        </port-component>
    </webservice-description>
</webservices>

客戶端存根部署

  1. 從運行中服務獲取 WSDL:https://localhost:8080/workdev/service?WSDL。
  2. 同樣用 wscompile 生成客戶端工件(-gen:client)。
  3. web.xml 中聲明 <service-ref>,含 handler 鏈(客戶端加密/簽名)。
  4. JNDI 查詢:new InitialContext().lookup("java:comp/env/service/BusinessLogicService")。

為何不直接用 Eclipse WTP?

  • WTP 0.7 僅支援模組部署,無法部署含 EAR 描述符(資料源、JMS、安全配置)的完整應用。
  • 僅支援 Axis Runtime,生成非 J2EE 標準服務,產生大量雜碼。
  • 不支援在向導中配置 SOAP Handler,需手工修改描述符。

結論

Geronimo 依賴 J2EE 1.4 標準提供健壯 Web 服務支援。雖工具鏈尚待成熟,但手工掌握 wscompile、描述符配置、Handler 鏈開發,即可構建生產級服務。未來隨 WTP 完善 JSR 109 支援,開發效率將大幅提升。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved