引言
前一節介紹了使用userslst文件來實現webservice的用戶名和密碼的校驗
本節介紹使用webservice的handler來實現webservice的安全校驗
這裡不用用戶名和密碼來實現安全校驗換一種方式采用IP地址校驗的方式
這裡通過一個配置文件來控制是否打開IP校驗開關以及允許哪些IP地址的客戶端可以訪問
這裡的開發還是基於上一節HelloService這個基本的webservice基礎上來開發
項目環境
system:win
myeclipse: tomcat: JDK:開發環境編譯環境
axis:
項目結構圖如下
示例代碼配置文件
webxmlweb項目的配置文件和基本的webservice配置沒任何區別
web
xml
<?xml version= encoding=UTF?>
<webapp version=
xmlns=
xmlns:xsi=instance
xsi:schemaLocation=
app__xsd>
<servlet>
<servletname>AxisServlet</servletname>
<servletclass>
orgapacheaxistransporthttpAxisServlet
</servletclass>
</servlet>
<servletmapping>
<servletname>AxisServlet</servletname>
<urlpattern>/services/*</urlpattern>
</servletmapping>
</webapp>
serverconfigwsddaxis的配置文件這裡的配置就是一個webservice+handler的基本配置
server
config
wsdd
<?xml version= encoding=UTF?> <deployment xmlns= xmlns:java=> <globalConfiguration> <parameter name=sendMultiRefs value=pan/archive////true /> <parameter name=disablePrettyXML value=pan/archive////true /> <parameter name=adminPassword value=pan/archive////admin /> <parameter name=attachmentsDirectory value=pan/archive////file://localhost/D:/tomcat/webapps/WebService/WEBINF/attachments /> <parameter name=dotNetSoapEncFix value=pan/archive////true /> <parameter name=enableNamespacePrefixOptimization value=pan/archive////false /> <parameter name=sendXMLDeclaration value=pan/archive////true /> <parameter name=sendXsiTypes value=pan/archive////true /> <parameter name=attachmentsimplementation value=pan/archive////orgapacheaxisattachmentsAttachmentsImpl /> <requestFlow> <handler type=java:orgapacheaxishandlersJWSHandler> <parameter name=scope value=pan/archive////session /> </handler> <handler type=java:orgapacheaxishandlersJWSHandler> <parameter name=scope value=pan/archive////request /> <parameter name=extension value=pan/archive////jwr /> </handler> </requestFlow> </globalConfiguration> <handler name=LocalResponder type=java:orgapacheaxistransportlocalLocalResponder /> <handler name=URLMapper type=java:orgapacheaxishandlershttpURLMapper /> <handler name=Authenticate type=java:orgapacheaxishandlersSimpleAuthenticationHandler /> <service name=AdminService provider=java:MSG> <parameter name=allowedMethods value=pan/archive////AdminService /> <parameter name=enableRemoteAdmin value=pan/archive////false /> <parameter name=className value=pan/archive////orgapacheaxisutilsAdmin /> <namespace>;/namespace> </service> <service name=Version provider=java:RPC> <parameter name=allowedMethods value=pan/archive////getVersion /> <parameter name=className value=pan/archive////orgapacheaxisVersion /> </service> <transport name=http> <requestFlow> <handler type=URLMapper /> <handler type=java:orgapacheaxishandlershttpHTTPAuthHandler /> </requestFlow> <parameter name=qs:list value=pan/archive////orgapacheaxistransporthttpQSListHandler /> <parameter name=qs:wsdl value=pan/archive////orgapacheaxistransporthttpQSWSDLHandler /> <parameter name=qslist value=pan/archive////orgapacheaxistransporthttpQSListHandler /> <parameter name=thod value=pan/archive////orgapacheaxistransporthttpQSMethodHandler /> <parameter name=qs:method value=pan/archive////orgapacheaxistransporthttpQSMethodHandler /> <parameter name=qswsdl value=pan/archive////orgapacheaxistransporthttpQSWSDLHandler /> </transport> <transport name=local> <responseFlow> <handler type=LocalResponder /> </responseFlow> </transport> <! 配置一個handler用來進行IP校驗> <handler name=IPHandler type=java:serverhandlerIpAuthentionHandler> <parameter name=status value=pan/archive////success /> </handler> <! 配置自己的服務 > <service name=HelloService provider=java:RPC> <parameter name=allowedMethods value=pan/archive////* /> <parameter name=className value=pan/archive////serverserviceHelloServiceImpl /> <! 引入IP校驗的handler > <requestFlow> <handler type=IPHandler /> </requestFlow> </service> </deployment>
服務端文件
服務類
HelloServiceImpl
java
HelloServiceImpl
java
package server
service;
public class HelloServiceImpl {
public String hello(String s) {
System
out
println(
我是服務端……
)
System
out
println(
方法的入參為
+s)
return
hello
+ s;
}
}
handler處理類和配置文件
IpAuthentionHandler
java
IpAuthentionHandler
java
package server
handler;
import java
io
IOException;
import java
io
InputStream;
import java
util
Properties;
import java
util
regex
Pattern;
import javax
servlet
http
HttpServletRequest;
import org
apache
axis
AxisFault;
import org
apache
axis
MessageContext;
import org
apache
axis
handlers
BasicHandler;
import org
apache
axis
transport
http
HTTPConstants;
import org
apache
axis
utils
Messages;
//利用handler進行客戶端IP校驗
public class IpAuthentionHandler extends BasicHandler {
private static final long serialVersionUID =
L;
private static Properties p = new Properties()
static {
InputStream in = null;
try {
//注意這裡的文件的存放位置和文件路徑的書寫方式
path 不以
/
開頭時默認是從此類所在的包下取資源
//這裡如果我們把ip
properties放在src根目錄下
然後采用src/ip
properties的形式是沒法取到此文件的
//此時需要使用
in=IpAuthentionHandler
class
getClassLoader()
getResourceAsStream(
ip
properties
)
in = IpAuthentionHandler
class
getResourceAsStream(
ip
properties
)
p
load(in)
} catch (IOException e) {
System
out
println(
ip
properties配置文件加載失敗!
)
e
printStackTrace()
} finally {
if (null != in)
try {
in
close()
} catch (IOException e) {
System
out
println(
關閉流操作發生異常!
)
e
printStackTrace()
}
}
}
public void invoke(MessageContext messageContext) throws AxisFault {
String status=(String)this
getOption(
status
)
System
out
println(
IpAuthentionHandler
s status is :
+status)
String uri = messageContext
getSOAPActionURI()
String targetService = messageContext
getTargetService()
System
out
println(
webservice開始IP認證
service》
+ uri +
/
+ targetService)
String name = HTTPConstants
MC_HTTP_SERVLETREQUEST;
HttpServletRequest request = (HttpServletRequest) messageContext
getProperty(name)
String remoteAddr = request
getRemoteAddr()
System
out
println(
客戶端IP:
+ remoteAddr)
String switcher = p
getProperty(
ip_switcher
)
System
out
println(
IP校驗開關
+ switcher)
if (
on
equalsIgnoreCase(switcher)) {
System
out
println(
服務端IP校驗開關處於【打開】狀態
需要校驗IP
)
String regx = p
getProperty(
ip_allow
)
System
out
println(
允許調用服務的IP地址有
+ regx)
if (null != regx && regx
length() >
) {
String regxArray[] = regx
split(
)
boolean ip_check = false;
for (int i =
; i < regxArray
length; i++) {
Pattern p = pile(regxArray[i])
boolean flag = p
matcher(remoteAddr)
find()
if (flag) {
ip_check = true;
break;
}
}
if (ip_check) {
System
out
println(
IP校驗通過!
)
} else {
throw new AxisFault(
Messages
getMessage(
wrong ip:
+remoteAddr)
null
null)
}
} else {
System
out
println(
請指定校驗的客戶端IP!
)
throw new AxisFault()
}
} else if (
off
equalsIgnoreCase(switcher)) {
System
out
println(
服務端IP校驗開關處於【關閉】狀態
不需要校驗IP
)
}
}
}
ip
properties
ip
properties
##################################IP校驗配置##################################
#IP校驗開關 只能填寫on或者off
不區分大小寫
ip_switcher=on
#允許調用對應的webservice服務的客戶端IP地址 多個IP地址之間用逗號隔開
#當ip校驗開關打開的時候
必須配置IP地址
ip_allow=
##################################IP校驗配置##################################
客戶端文件
Test
java
Test
java
package client;
import
URL;
import javax
xml
rpc
ParameterMode;
import org
apache
axis
client
Call;
import org
apache
axis
encoding
XMLType;
public class Test
{
public static void main(String args[]) throws Exception {
webservice_user()
}
public static void webservice_user() throws Exception {
//
創建service對象
通過axis自帶的類創建
org
apache
axis
client
Service service = new org
apache
axis
client
Service()
//
創建url對象
String wsdlUrl = //localhost:
/WebService
_Security/services/HelloService?wsdl
;// 請求服務的URL
URL url = new URL(wsdlUrl)
// 通過URL類的構造方法傳入wsdlUrl地址創建URL對象
//
創建服務方法的調用者對象call
設置call對象的屬性
Call call = (Call) service
createCall()
call
setTargetEndpointAddress(url)
// 給call對象設置請求的URL屬性
String serviceName =
hello
;// webservice的方法名
call
setOperationName(serviceName)
// 給call對象設置調用方法名屬性
call
addParameter(
s
XMLType
XSD_STRING
ParameterMode
IN)
// 給call對象設置方法的參數名
參數類型
參數模式
call
setReturnType(XMLType
SOAP_STRING)
// 設置調用方法的返回值類型
// call
setTimeout(new Integer(
))
//設置超時限制
//
通過invoke方法調用webservice
String str = new String(
pantp
)
String dept = (String) call
invoke(new Object[] { str })
// 調用服務方法
//
打印返回結果
System
out
println(
我是客戶端……
)
System
out
println(dept)
}
}
驗證結果
發布工程
啟動tomcat服務器
看webservice在浏覽器中是否可以正常顯示
在浏覽器中輸入wsdl地址
//localhost:
/WebService
_Security/services/HelloService?wsdl
運行webservice客戶端看是否可以正常的訪問
目前IP配置文件中開關是打開的並且是允許訪問此webservice服務的
運行後客戶端和服務端日志分別如下
模擬不能正常訪問的IP地址的調用情況
把ipproperties文件中的
ip_allow=
改為
ip_allow=
然後重新發布項目啟動tomcat:
此時客戶端和服務端的日志如下(此時需要時間稍微長一點客戶端才會出現以下異常)
From:http://tw.wingwit.com/Article/program/Java/hx/201311/26935.html