天天看點

jax-ws 擷取用戶端相關資訊

本部落格轉自 http://blog.csdn.net/z69183787/article/details/46884165, 尊重原創

  1. 基于JDK6 jax-ws開發的webservice擷取用戶端IP位址
    • Endpoint.publish() 輕量級HTTP服務釋出
    • 在web容器tomcat下釋出
  2. 基于XFire開發的webservice擷取用戶端IP位址
  3. 基于Axis開發的webservice擷取用戶端IP位址

[一]、基于JDK6 jax-ws開發的webservice擷取用戶端IP位址

以:http://www.micmiu.com/soa/webservice/jax-ws-demo/ 中的 [三] 2 具體示例為基礎:

1. 情況一:如果以 Endpoint.publish() 的方式釋出:

服務端接口實作類:HelloServiceImpl.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 50 51 package com . micmiu . jaxws . demo . impl ;   import java . net . InetSocketAddress ;   import javax . annotation . Resource ; import javax . jws . WebMethod ; import javax . jws . WebParam ; import javax . jws . WebService ; import javax . xml . ws . WebServiceContext ; import javax . xml . ws . handler . MessageContext ;   import com . micmiu . jaxws . demo . HelloService ; import com . sun . net . httpserver . HttpExchange ; import com . sun . xml . internal . ws . developer . JAXWSProperties ;   @ WebService ( ) public class HelloServiceImpl implements HelloService {   @ Resource private WebServiceContext wsContext ;   @ WebMethod public String sayHello ( @ WebParam ( name = "userName" ) String userName ) { System . out . println ( " ----> 擷取用戶端資訊開始 <---- " ) ; getClientInfo ( ) ; System . out . println ( " ----> 擷取用戶端資訊結束 <---- " ) ; return "hi," + userName + " welcom to www.micmiu.com" ; }   private void getClientInfo ( ) { try { MessageContext mc = wsContext . getMessageContext ( ) ;   HttpExchange exchange = ( HttpExchange ) mc . get ( JAXWSProperties . HTTP_EXCHANGE ) ; InetSocketAddress isa = exchange . getRemoteAddress ( ) ; System . out . println ( "InetSocketAddress : " + isa ) ; System . out . println ( "Hostname : " + isa . getAddress ( ) . getHostAddress ( ) + " address: " + isa . getAddress ( ) . getHostName ( ) ) ; } catch ( Exception e ) { e . printStackTrace ( ) ; }   } }

用戶端調用後,服務端運作日志:

1 2 3 4 5 publish webservice successful -- -- & gt ; 擷取用戶端資訊開始 & lt ; -- -- InetSocketAddress : / 127.0.0.1 : 61333 Hostname : 127.0.0.1 address : demo . micmiu . com -- -- & gt ; 擷取用戶端資訊結束 & lt ; -- --

從上面的日志資訊中可看出:服務端完全可以擷取到用戶端的IP位址。

2. 情況二:如果以web容器的方式釋出(jetty 或 tomcat為例):

服務端接口實作 HelloServiceImpl.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 package com . micmiu . jaxws . demo2 . impl ;   import javax . annotation . Resource ; import javax . jws . WebService ; import javax . servlet . http . HttpServletRequest ; import javax . xml . ws . WebServiceContext ; import javax . xml . ws . handler . MessageContext ;   import com . micmiu . jaxws . demo2 . HelloService ;   @ WebService ( endpointInterface = "com.micmiu.jaxws.demo2.HelloService" ) public class HelloServiceImpl implements HelloService {   @ Resource private WebServiceContext wsContext ;   public String sayHello ( String userName ) { System . out . println ( " ----> 擷取用戶端資訊開始 <---- " ) ; String clientIP = getClientInfo ( ) ; System . out . println ( " ----> 擷取用戶端資訊結束 <---- " ) ; return "Hi," + userName + " welcome to JAX-WS with IP: " + clientIP + " . see more http://www.micmiu.com " ; }   private String getClientInfo ( ) { String clientIP = null ; try { MessageContext mc = wsContext . getMessageContext ( ) ; HttpServletRequest request = ( HttpServletRequest ) ( mc . get ( MessageContext . SERVLET_REQUEST ) ) ; clientIP = request . getRemoteAddr ( ) ; System . out . println ( "client IP : " + clientIP ) ; } catch ( Exception e ) { e . printStackTrace ( ) ; }   return clientIP ;   } }

用戶端代碼不用修改,運作如下:

1 2 3 4 start webservice client . . . send Michael to server Hi , Michael welcome to JAX - WS with IP : 127.0.0.1 . see more http : //www.micmiu.com test client end .

服務端運作日志如下:

1 2 3 4 5 6 7 8 2012 - 8 - 6 19 : 15 : 24 com . sun . xml . ws . transport . http . servlet . WSServletContextListener contextInitialized 資訊 : WSSERVLET12 : JAX - WS 上下文監聽程式正在初始化 2012 - 8 - 6 19 : 15 : 25 com . sun . xml . ws . transport . http . servlet . WSServletDelegate & lt ; init & gt ; 資訊 : WSSERVLET14 : JAX - WS servlet 正在初始化 2012 - 08 - 06 19 : 15 : 25.645 : INFO :: Started SelectChannelConnector @ 0.0.0.0 : 8080 -- -- & gt ; 擷取用戶端資訊開始 & lt ; -- -- client IP : 127.0.0.1 -- -- & gt ; 擷取用戶端資訊結束 & lt ; -- --

從上面的日志資訊中可看出:服務端完全可以擷取到用戶端的IP位址。

[二]、基于XFire開發的webservice擷取用戶端IP位址

以:http://www.micmiu.com/soa/webservice/xfire-ws-base-demo/ 的示例為基礎:

服務端接口實作類:HelloServiceImpl.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 package com . micmiu . xfire . demo . base ;   import javax . servlet . http . HttpServletRequest ;   import org . codehaus . xfire . transport . http . XFireServletController ;   public class HelloWorldServiceImpl implements HelloWorldService {   public String sayHello ( String username ) { System . out . println ( " ----> 擷取用戶端資訊  <---- " ) ; String clientIP = getClientInfo ( ) ; System . out . println ( "用戶端IP :" + clientIP ) ; return "Hi," + username + " welcome,see more http://www.micmiu.com" ; }   public String getClientInfo ( ) { String clientIP = null ; try { HttpServletRequest request = XFireServletController . getRequest ( ) ; System . out . println ( "Addr : " + request . getRemoteAddr ( ) + " host: " + request . getRemoteHost ( ) ) ; clientIP = request . getRemoteAddr ( ) ; } catch ( Exception e ) { e . printStackTrace ( ) ; } return clientIP ; } }

用戶端調用後,服務端的日志資訊如下:

1 2 3 -- -- & gt ; 擷取用戶端資訊    & lt ; -- -- Addr : 127.0.0.1 host : 127.0.0.1 用戶端 IP : 127.0.0.1

從上面的日志資訊中可看出:服務端完全可以擷取到用戶端的IP位址。

[三]、Axis開發的webservice擷取用戶端IP位址

以Axis最簡單的部署方式為例:

服務端代碼:HelloWorld.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 import javax . servlet . http . HttpServletRequest ;   import org . apache . axis . MessageContext ; import org . apache . axis . transport . http . HTTPConstants ;   public class HelloWorld {   public String sayHello ( String username ) { System . out . println ( " ----> Get client info  <---- " ) ; String clientIP = getClientInfo ( ) ; System . out . println ( "client ip" + clientIP ) ; return "Hi," + username + " welcome to axis with IP: " + clientIP + " . see more http://www.micmiu.com " ; }   private String getClientInfo ( ) { MessageContext mc = null ; HttpServletRequest request = null ; String clientIP = null ; try { mc = MessageContext . getCurrentContext ( ) ;   request = ( HttpServletRequest ) mc . getProperty ( HTTPConstants . MC_HTTP_SERVLETREQUEST ) ; clientIP = request . getRemoteAddr ( ) ; } catch ( Exception e ) { e . printStackTrace ( ) ; } return clientIP ; } }

用戶端調用代碼:Client4Hello.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 import org . apache . axis . client . Call ; import org . apache . axis . client . Service ;   public class Client4Hello {   public static void main ( String [ ] args ) { String wsdlUrl = "http://localhost:8088/axis/HelloWorld.jws" ; try { System . out . println ( " ----> 用戶端調用測試  <---- " ) ; Service s = new Service ( ) ; Call call = ( Call ) s . createCall ( ) ; call . setOperationName ( "sayHello" ) ; // 這裡是要調用的方法名 call . setTargetEndpointAddress ( wsdlUrl ) ; // 設定調用的wsdl   String ret = ( String ) call . invoke ( new Object [ ] { "Michael" } ) ; System . out . println ( "發送 Michael ,傳回的資訊 :" + ret ) ; } catch ( Exception e ) { e . printStackTrace ( ) ; }   }   }

運作用戶端結果:

1 2 3 4 -- -- & gt ; 用戶端調用測試    & lt ; -- -- 2012 - 8 - 6 16 : 56 : 13 org . apache . axis . utils . JavaUtils isAttachmentSupported 。。。。 發送 Michael ,傳回的資訊 : Hi , Michael welcome to axis with IP : 127.0.0.1 . see more http : //www.micmiu.com

從tomcat的日志檔案catalina.out 中看到服務端運作資訊:

1 2 -- -- & gt ; Get client info    & lt ; -- -- client ip127 . 0.0.1

從上面的日志資訊中可看出:服務端完全可以擷取到用戶端的IP位址。

到此示範了JAX-WS、XFire、Axis三種webservice的擷取用戶端IP的簡單實作過程。

繼續閱讀