引言
前面講了webservice的安全機制和本節繼續webservice的安全之旅
本節采用servlet的Filter的來實現對webservice的安全訪問
在調用webservice之前過濾器會攔截匹配的請求只有滿足安全要求的客戶端才能訪問webservice服務
項目環境
system:win myeclipse: tomcat:
JDK:開發環境編譯環境
axis:
示例代碼()配置文件
webxml
webxml
<?xml version= encoding=UTF?>
<webapp version=
xmlns=
xmlns:xsi=instance
xsi:schemaLocation=
app__xsd>
<! 配置webservice的處理類 >
<servlet>
<servlet
name>AxisServlet</servlet
name>
<servlet
class>
org
apache
axis
transport
http
AxisServlet
</servlet
class>
</servlet>
<servlet
mapping>
<servlet
name>AxisServlet</servlet
name>
<url
pattern>/services/*</url
pattern>
</servlet
mapping>
<!
配置IP地址的過濾器
>
<filter>
<filter
name>WebServiceFilter</filter
name>
<filter
class>server
filter
WebServiceFilter</filter
class>
</filter>
<filter
mapping>
<filter
name>WebServiceFilter</filter
name>
<url
pattern>/services/*</url
pattern>
</filter
mapping>
</web
app>
server
config
wsdd
server
config
wsdd
<?xml version=
encoding=
UTF
?>
<deployment xmlns=
xmlns:java=
>
<globalConfiguration>
<parameter name=
sendMultiRefs
value=
true
/>
<parameter name=
disablePrettyXML
value=
true
/>
<parameter name=
adminPassword
value=
admin
/>
<parameter name=
attachments
Directory
value=
D:\tomcat
\webapps\WebService\WEB
INF\attachments
/>
<parameter name=
dotNetSoapEncFix
value=
true
/>
<parameter name=
enableNamespacePrefixOptimization
value=
false
/>
<parameter name=
sendXMLDeclaration
value=
true
/>
<parameter name=
sendXsiTypes
value=
true
/>
<parameter name=
attachments
implementation
value=
org
apache
axis
attachments
AttachmentsImpl
/>
<requestFlow>
<handler type=
java:org
apache
axis
handlers
JWSHandler
>
<parameter name=
scope
value=
session
/>
</handler>
<handler type=
java:org
apache
axis
handlers
JWSHandler
>
<parameter name=
scope
value=
request
/>
<parameter name=
extension
value=
jwr
/>
</handler>
</requestFlow>
</globalConfiguration>
<handler name=
LocalResponder
type=
java:org
apache
axis
transport
local
LocalResponder
/>
<handler name=
URLMapper
type=
java:org
apache
axis
handlers
http
URLMapper
/>
<handler name=
Authenticate
type=
java:org
apache
axis
handlers
SimpleAuthenticationHandler
/>
<service name=
AdminService
provider=
java:MSG
>
<parameter name=
allowedMethods
value=
AdminService
/>
<parameter name=
enableRemoteAdmin
value=
false
/>
<parameter name=
className
value=
org
apache
axis
utils
Admin
/>
<namespace>;/namespace>
</service>
<service name=
Version
provider=
java:RPC
>
<parameter name=
allowedMethods
value=
getVersion
/>
<parameter name=
className
value=
org
apache
axis
Version
/>
</service>
<transport name=
http
>
<requestFlow>
<handler type=
URLMapper
/>
<handler
type=
java:org
apache
axis
handlers
http
HTTPAuthHandler
/>
</requestFlow>
<parameter name=
qs:list
value=
org
apache
axis
transport
http
QSListHandler
/>
<parameter name=
qs:wsdl
value=
org
apache
axis
transport
http
QSWSDLHandler
/>
<parameter name=
qs
list
value=
org
apache
axis
transport
http
QSListHandler
/>
<parameter name=
thod
value=
org
apache
axis
transport
http
QSMethodHandler
/>
<parameter name=
qs:method
value=
org
apache
axis
transport
http
QSMethodHandler
/>
<parameter name=
qs
wsdl
value=
org
apache
axis
transport
http
QSWSDLHandler
/>
</transport>
<transport name=
local
>
<responseFlow>
<handler type=
LocalResponder
/>
</responseFlow>
</transport>
<!
配置自己的服務
>
<service name=
HelloService
provider=
java:RPC
>
<parameter name=
allowedMethods
value=
*
/>
<parameter name=
className
value=
server
service
HelloServiceImpl
/>
</service>
</deployment>
(
)服務端代碼
HelloServiceImpl
java
webservice服務端
HelloServiceImpl
java
package server
service;
public class HelloServiceImpl {
public String hello(String s) {
return
hello
+ s;
}
}
WebServiceFilter
java
Filter過濾器
WebServiceFilter
java
package server
filter;
import java
io
IOException;
import javax
servlet
Filter;
import javax
servlet
FilterChain;
import javax
servlet
FilterConfig;
import javax
servlet
ServletException;
import javax
servlet
ServletRequest;
import javax
servlet
ServletResponse;
import javax
servlet
http
HttpServletRequest;
public class WebServiceFilter implements Filter {
//不允許訪問webservice服務的IP地址
static final String[] deniedIPList=new String[]{
};
public boolean isIPDenied(String ipAddr){
if(deniedIPList
length==
)
return false;
for(int i=
;i<deniedIPList
length;i++){
if(deniedIPList[i]
equals(ipAddr)){
return true;
}
}
return false;
}
public void destroy() {
}
public void doFilter(ServletRequest req
ServletResponse res
FilterChain chain) throws IOException
ServletException {
HttpServletRequest request=(HttpServletRequest) req;
String clientIP=request
getRemoteHost()
System
out
println(
客戶端IP:
+clientIP)
System
out
println(
開始過濾…
)
if(isIPDenied(clientIP)){
throw new ServletException(
你沒有權限調用此webservice!
)
}else{
chain
doFilter(req
res)
}
}
public void init(FilterConfig arg
) throws ServletException {
}
}
(
)客戶端代碼
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(
))
//設置超時限制
//
//此處的用戶名和密碼對應WEB
INF目錄下users
lst文件中的用戶名和密碼
//
call
getMessageContext()
setUsername(
pantp
)
//
call
getMessageContext()
setPassword(
)
//
//
通過invoke方法調用webservice
String str=new String(
pantp
)
System
out
println(
開始調用webservice服務……
)
String dept = (String) call
invoke(new Object[] { str })
// 調用服務方法
System
out
println(
結束調用webservice服務……
)
//
打印返回結果
System
out
println(
返回結果如下
+dept)
}
}
安全測試(
)正常測試(本機IP地址不在受限IP之內)
浏覽器中輸入wsdl地址測試
運行Test客戶端測試
客戶端日志
服務端日志
(
)受限測試(本機IP地址在受限IP之內)
修改WebServiceFilter類中deniedIPList數組所在的一行代碼
加入IP地址
然後重新發布項目
修改後數組IP地址如下
受限IP地址列表
static final String[] deniedIPList=new String[]{
};
浏覽器中輸入wsdl地址測試
運行Test客戶端測試
客戶端日志
服務端日志
總結
至此
webservice的安全相關的文章就已經介紹完了
以上都是webservice安全方面比較簡單的實現措施
更多的歡迎各位的探討
From:http://tw.wingwit.com/Article/program/Java/hx/201311/25712.html