XML和Web服务安全示例

来源:互联网 发布:nginx 参数判断 编辑:程序博客网 时间:2024/06/09 23:51

这个示例实际上是使用JWSDP1.6提供的simple例子.把它应用在Eclipse中,并发布到Tomcat5.59,

由于JAX-RPC和XWSS正在变化中,而且这些变化已经有了结果(有测试版),并会在今年提供最终版本,所以不打算改造现有应用作为例子了,随手用JWSDP1.6自带的例子,在Eclipse中发布它到Tomcat5.59中去。

实际上,如果不对JWSDP1.6自带的例子进行修改和某些项的纠正,它的例子就不能运行成功.在这里不再指出这些问题了.只保证simple例子在Eclipse中成功发布到tomcat中.

上图是SecureBinary例子的目录结构和引用的库.例子中的部分文件夹来自<JWSDP1.6.home>/xws-security/samples/simple中的对应目录.引用的库包括了jax-rpc,saaj,javamail,jaf,xwss,commonlog等等.

要修改的文件包括:sjsas-config.xml,client-config.xml,config.xml,属性文件build.properties.

sjsas-config.xml

这个文件定义了Ant使用的类库,以及定义了两个Ant任务:

<property name="java.endorsed.dir" value="${sjsas.home}/lib/endorsed" />
<path id="app.classpath">
 <fileset dir="${jwsdp.home}/jaxrpc/lib">
  <include name="*.jar" />
 </fileset>
 <fileset dir="${sjsas.home}/lib">
  <include name="*.jar" />
  <include name="sun-appserv-ant.jar" />
 </fileset>

 <fileset dir="${javahome}/lib">
  <include name="tools.jar" />
 </fileset>

 <fileset dir="${jwsdp.home}/jwsdp-shared/lib">
  <include name="*.jar" />
 </fileset>
 <fileset dir="${jwsdp.home}/xws-security/lib">
  <include name="*.jar" />
 </fileset>
 <fileset dir="${jwsdp.home}/saaj/lib">
  <include name="*.jar" />
 </fileset>
 <fileset dir="${jwsdp.home}/sjsxp/lib">
  <include name="*.jar" />
 </fileset>
</path>

<taskdef name="wscompile"
 classname="com.sun.xml.rpc.tools.ant.Wscompile"
 classpathref="app.classpath" />

<taskdef name="wsdeploy" classname="com.sun.xml.rpc.tools.ant.Wsdeploy"
 classpathref="app.classpath" />

<taskdef name="appserv-deploy"
 classname="org.apache.tools.ant.taskdefs.optional.sun.appserv.DeployTask"
 classpathref="app.classpath" />

<taskdef name="appserv-undeploy"
 classname="org.apache.tools.ant.taskdefs.optional.sun.appserv.UndeployTask"
 classpathref="app.classpath" />

不必担心这个文件的不完整性,因为它只作为外部资源由xml实体引用到build.xml文件中.

属性配置文件build.properties

这个文件中定义了属性sjsas.home,javahome,jwsdp.home,指出了JWSDP和应用服务器的位置.根据你自己的安装,修改这些路径,此外还指明了客户端和服务端所使用安全配置文件以及服务的URL:

# This file is referenced by the build.xml file.

# java and jwsdp
javahome=D:/jdk1.5.0

#container homes (uncomment the appropriate container home property)
#Note: only one of the 3 below should be uncommented at any point of time
#tomcat.home=h:/jakarta-tomcat-5.5.9
sjsas.home=D:/Sun/AppServer
#sjsws.home=<set it to SJSWS install dir>

# Replace username and password values with the admin user/password
# for the appropriate container instance being used for this sample
username=admin
password=cherubangel

# the host and port for the endpoint
endpoint.host=localhost
endpoint.port=8080

#VS.DIR : virtual server name for SJSWS
VS.DIR=<set it to the virtual server name, required only if you are using SJSWS webserver>

#place where standalone JWSDP is installed
jwsdp.home=I:/jwsdp-1.6

# Set to true or false to control jaxrpc tool output
jaxrpc.tool.verbose=false

# look in config directory for alternate security configurations
# Client Security Config. file
#client.security.config=config/dump-client.xml
#client.security.config=config/user-pass-authenticate-client.xml
client.security.config=config/encrypted-user-pass-client.xml
#client.security.config=config/encrypt-usernameToken-client.xml
#client.security.config=config/sign-client.xml
#client.security.config=config/encrypt-client.xml
#client.security.config=config/encrypt-using-symmkey-client.xml
#client.security.config=config/sign-encrypt-client.xml
#client.security.config=config/encrypt-sign-client.xml
#client.security.config=config/sign-ticket-also-client.xml
#client.security.config=config/timestamp-sign-client.xml
#client.security.config=config/flexiblec.xml
#client.security.config=config/method-level-client.xml


# Server Security Config. file
#server.security.config=config/dump-server.xml
#server.security.config=config/user-pass-authenticate-server.xml
server.security.config=config/encrypted-user-pass-server.xml
#server.security.config=config/encrypt-usernameToken-server.xml
#server.security.config=config/sign-server.xml
#server.security.config=config/encrypt-server.xml
#server.security.config=config/sign-encrypt-server.xml
#server.security.config=config/encrypt-sign-server.xml
#server.security.config=config/sign-ticket-also-server.xml
#server.security.config=config/timestamp-sign-server.xml
#server.security.config=config/flexibles.xml
#server.security.config=config/method-level-server.xml

# proxy server settings in case of remote endpoints
http.proxyHost=<set it to proxy host>
http.proxyPort=<proxy port>

# do not edit below
# Name of this particular example
example=simple

# Service URL
service.url=/securesimple/Ping

下面的两个文件的修改是由于这两个文件被引入到Eclipse中的缘故,如果不修改,你也可以根据执行Ant任务时报告的错误予以纠正.

client-config.xml

这是wscompile生成客户端制品所需要的配置文件,修改后文件如下:

<configuration xmlns="http://java.sun.com/xml/ns/jax-rpc/ri/config">
<wsdl location="./WEB-INF/etc/PingService.wsdl"
      packageName="simple" />
</configuration>

config.xml

这是wscompile生成服务端制品所需要的配置文件,修改后文件如下:

<configuration xmlns="http://java.sun.com/xml/ns/jax-rpc/ri/config">
<wsdl location="./WEB-INF/etc/PingService.wsdl"
      packageName="simple" />
</configuration>

以上两个配置文件都指定了同一个WSDL文件,对于客户端的我们可以指定为WS URL来代替本地文件.

经过以上修改后,就可以运行Ant构建了,由于这个例子使用Eclipse发布安全的Web服务到Tomcat中,所以就要手工发布了.

首先,运行Ant任务gen-server生成服务端制品.然后把dist目录中的securesimple.war拷贝到Tomcat发布目录,在形成规范的目录结构后,在WEB-INF中创建lib并在其内放入需要的库,如下图:

上图显示的库可以在JWSDP1.6中相关分类中找到,把它们都放入lib中。

接下来,运行你的Tomcat,然后执行run-sample任务,你就可以看到结果了.不过在运行任务前最好把这个任务依赖的deploy-forced除掉,这个任务是为了自动发布Web服务的,但在我们这个例子中不需要这个功能,所以去掉.

做了这么多工作,好象没有涉及到任何安全方面的东西,这就是Sun提供的这个方便.我们只需要在wscompile中使用选项-security <安全配置文件>,我们把重点放在这个文件中即可,对于谁来具体处理安全问题,那就是类

SecurityEnvironmentHandler所做的工作,安全环境处理器类处理了所有安全的问题,我们不打算进一步探究这个类以及它的背景,是因为Sun的一个全新的WS环境就要推出了,目前有测试版在用,估计年末会推出正式版.

原创粉丝点击