近年来,随着面向服务的平台的大规模开放,异构程序之间的通信的需求不断增多,随之而来的就是webservice的蓬勃发展。
Java中用来构建webservice的主流技术有Axis2,JAX-WS,CXF(主要对JAX-WS进行了一系列的封装)。
今天主要给大家介绍一些关于webservice的验证或者说是权限管理,本文主要针对JAX-WS进行说明,JAX-WS是基本适用于所有的的webservice调用,而且它主要依赖于JDK,额外需要的jar很少,我个人认为JAX-WS适用和灵活可以和很多框架结合适用,比如Spring,SSH,SpringMVC框架。只要能够掌握JAX-WS在平时的工作中使用就绰绰有余了。
回归正题还是来说webservice的验证,为什么要做webservice的验证,基本上都是为了数据安全考虑,当然也有特殊原因。
webservice的验证主要有一下几种验证方式:
1.在获取wsdl文件的时候验证
2.在soap head中验证
一、在获取wsdl文件的时候验证
1、基本原理:其实就是拦截特定的请求,例如我要获取wsdl的时候,对方提供wsdl路径是http://www.baidu.com/XXX/HelloServiceImplPort?wsdl,只要我门获取到这个地址在测试工具XMLSpy中输入这个地址时会弹出这个对话框,要求你输入用户名和密码。
2、实现方式
2.1服务器实现方式
这里我使用的是SSH框架,其实主要就是webservice和Spring的集成。我把关键的地方贴出来
a.在tomcat中配置用户名和密码
1.找到tomcat安装目录---->conf------>tomcat-users.xml,创建用户名和密码都为tomcat
2.客户端程序
</pre><pre name="code" class="html">这是一个接口
package com.webservice;
import java.util.List;
import javax.jws.WebService;
import com.xx.bean.Campaign;
import com.xx.bean.Campaigntarget;
@WebService
public interface CRM_CampaignService {
public boolean addCampaign(List<Campaign> list);
public boolean addCampaigntarget(List<Campaigntarget> list);
}
这是接口实现方式,把方法中的代码都去掉了,这里涉及到和Spring整合
package com.webservice;
import java.util.List;
import javax.jws.HandlerChain;
import javax.jws.WebService;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import com.xx.bean.Campaign;
import com.xx.bean.Campaigntarget;
@WebService(endpointInterface="com.webservice.CRM_CampaignService")
public class CRM_CampaignServiceImpl implements CRM_CampaignService{
@Autowired
private CampaignService campaignService;
@Autowired
private CampaigntargetService campaigntargetService;
@Autowired
private SessionFactory sessionFactory;
@Override
public boolean addCampaign(List<Campaign> list) {
}
@Override
public boolean addCampaigntarget(List<Campaigntarget> list) {
}
}
这是web.xml配置
<description>
JAX-WS endpoint - CRM_CampaignServiceImplService
</description>
<display-name>CRM_CampaignServiceImplService</display-name>
<servlet-name>CRM_CampaignServiceImplService</servlet-name>
<servlet-class>
com.sun.xml.ws.transport.http.servlet.WSSpringServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CRM_CampaignServiceImplService</servlet-name>
<url-pattern>/CRM_CampaignServiceImplPort</url-pattern>
</servlet-mapping>
<security-role>
<description>Normal operator user</description>
<role-name>tomcat</role-name>
</security-role>
<security-constraint>
<span style="white-space:pre"> </span> <web-resource-collection>
<span style="white-space:pre"> </span> <web-resource-name>Operator Roles Security</web-resource-name>
<span style="white-space:pre"> </span> <span style="white-space:pre"> </span><url-pattern>/CRM_CampaignServiceImplPort</url-pattern>
<span style="white-space:pre"> </span> </web-resource-collection>
<span style="white-space:pre"> </span><auth-constraint>
<span style="white-space:pre"> </span> <role-name>tomcat</role-name>
<span style="white-space:pre"> </span></auth-constraint>
<user-data-constraint>
<span style="white-space:pre"> </span> <transport-guarantee>NONE</transport-guarantee>
<pre name="code" class="html"><span style="white-space:pre"> </span></user-data-constraint>
</security-constraint> <span style="font-family: Arial, Helvetica, sans-serif;"> </span>
<login-config>
<auth-method>BASIC</auth-method>
</login-config>
这是Spring配置文件,注意红线加粗的地方,这是要额外引入的
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
<strong style="background-color: rgb(255, 102, 0);">xmlns:ws="http://jax-ws.dev.java.net/spring/core"
xmlns:wss="http://jax-ws.dev.java.net/spring/servlet"</strong>
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
<strong><span style="color:#ff0000;"> http://jax-ws.dev.java.net/spring/core
http://jax-ws.dev.java.net/spring/core.xsd
http://jax-ws.dev.java.net/spring/servlet
http://jax-ws.dev.java.net/spring/servlet.xsd" </span></strong>
default-autowire="byName" default-lazy-init="true">
<bean id="cRM_CampaignService" class="com.webservice.CRM_CampaignServiceImpl"/>
<wss:binding url="/CRM_CampaignServiceImplPort">
<wss:service>
<ws:service bean="#cRM_CampaignService" />
</wss:service>
</wss:binding>
到这里服务端基本完成了,输入访问地址就会出来这个对话框
输入用户名和密码都是tomcat
然后就可以看到wsdl文件
在这里获取wsdl是需要的验证基本上就结束了,有什么错误还请大家斧正。