jax-ws 获取客户端相关信息
本博客转自 http://blog.csdn.net/z69183787/article/details/46884165, 尊重原创
- 基于JDK6 jax-ws开发的webservice获取客户端IP地址
- Endpoint.publish() 轻量级HTTP服务发布
- 在web容器tomcat下发布
- 基于XFire开发的webservice获取客户端IP地址
- 基于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
;
/**
* blog http://www.micmiu.com
*
* @author Michael
*/
@
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
;
/**
* blog http://www.micmiu.com
*
* @author Michael
*/
@
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
;
/**
* @blog http://www.micmiu.com
* @author Michael
*/
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
;
/**
*
* @author <a href="http://www.micmiu.com">Michael Sun</a>
*/
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
;
/**
*
* @author <a href="http://www.micmiu.com">Michael Sun</a>
*/
public
class
Client4Hello
{
/**
* @param args
*/
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的简单实现过程。