axis2+rampart实现WS-Security (一)

首先搭建基于axis2的ws服务端与客户端,发布成功并且客户端能够正常调用。
下面具体介绍axis2+rampart的配置。
1)、下载rampart-1.4.mar文件,下载地址:http://ws.apache.org/rampart/download/1.4/download.cgi
解压后把\modules\rampart-1.4.mar放入服务端与客户端的WebRoot\WEB-INF\module文件夹下。将\rampart-1.4\lib下的jar包放入服务端与客户端的lib中。
2)、用KEYTOOL生成一对JKS文件,service.jks和client.jks(具体方法请百度、Google)
service.jks存放了SERVICE的私钥和CLIENT的公钥。
client.jks存放了CLIENT的私钥和SERVICE的公钥。
3)、新建service.properties、client.properties文件
service.properties
org.apache.ws.security.crypto.provider=org.apache.ws.security.components.crypto.Merlin
org.apache.ws.security.crypto.merlin.keystore.type=jks
org.apache.ws.security.crypto.merlin.keystore.password=apache
org.apache.ws.security.crypto.merlin.file=service.jks

client.properties
org.apache.ws.security.crypto.provider=org.apache.ws.security.components.crypto.Merlin
org.apache.ws.security.crypto.merlin.keystore.type=jks
org.apache.ws.security.crypto.merlin.keystore.password=apache
org.apache.ws.security.crypto.merlin.file=client.jks

4)、将service.jks和service.properties放入服务端的src目录下,client.jks和client.properties放入客户端的src目录下
5)、在服务端的service.xml里面加入相应的安全属性;
     <module ref="rampart" />
     <parameter name="InflowSecurity">
       <action>
           <items>Timestamp Signature</items>
           <signaturePropFile>service.properties</signaturePropFile>
       </action>
    </parameter>
    <parameter name="OutflowSecurity">
       <action>
           <items>Timestamp Signature</items>
           <user>service</user>
           <passwordCallbackClass>zzvcom.ws.PWCBHandler</passwordCallbackClass>
           <signaturePropFile>service.properties</signaturePropFile>
           <signatureKeyIdentifier>DirectReference</signatureKeyIdentifier>
       </action>
    </parameter>

6)、在客户端的WebRoot\WEB-INF\conf\axis2.xml里面加入相应的安全属性,在声明客户端对象的时候要指明这个配置上下文,使得程序能够找到这个axis2.xml文件。
 <module ref="rampart" />

	 <parameter name="OutflowSecurity">
       <action>
         <items>Timestamp Signature</items>
         <user>client</user>
         <signaturePropFile>client.properties</signaturePropFile>
         <passwordCallbackClass>zzvcom.ws.PWCBHandler</passwordCallbackClass>          <signatureKeyIdentifier>DirectReference</signatureKeyIdentifier>
       </action>
     </parameter>
     <parameter name="InflowSecurity">
       <action>
         <items>Timestamp Signature</items>
         <signaturePropFile>client.properties</signaturePropFile>
       </action>
     </parameter>

7)、在客户端和服务器端写一个安全相关的callback类,一般客户端和服务器端的这两个类是一样的,就是把客户端和服务端的功能用一个类来实现了。
package zzvcom.ws;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.security.auth.callback.Callback;
import javax.security.auth.callback.CallbackHandler;
import javax.security.auth.callback.UnsupportedCallbackException;

import org.apache.ws.security.WSPasswordCallback;

public class PWCBHandler implements CallbackHandler {
    public void handle(Callback[] callbacks) throws IOException,UnsupportedCallbackException{
          for (int i = 0; i < callbacks.length; i++){
              WSPasswordCallback pwcb = (WSPasswordCallback)callbacks[i];
              String id = pwcb.getIdentifer();
              if("client".equals(id)){
                  pwcb.setPassword("apache");
              }else if("service".equals(id)){
                  pwcb.setPassword("apache");
              }else{
                throw new UnsupportedCallbackException(callbacks[i],
                "对不起,您不是授权用户,不能访问该WEB服务!");
              }
           }
    }
}

8)、客户端代码
package zzvcom.ws;

import java.io.File;

import org.apache.axis2.context.ConfigurationContext;
import org.apache.axis2.context.ConfigurationContextFactory;
import org.apache.axis2.databinding.ADBBean;

import zzvcom.ws.SampleServerStub.Echo;

public class SampleClient {
	 public String testserver(String path) throws Exception {
		    String toEPR = "http://localhost:8080/axis2_server/services/SampleServer?wsdl";
		    ConfigurationContext configContext = null;
		    SampleServerStub serviceClient = null;
	        configContext = ConfigurationContextFactory
	                .createConfigurationContextFromFileSystem(path+"/WebRoot/WEB-INF",path+"/WebRoot/WEB-INF/conf/axis2.xml");
	        serviceClient = new SampleServerStub(configContext, toEPR);
	        SampleServerStub.Echo echo = (Echo) getTestObject(Echo.class);
	        echo.setS("zzvcom");
		    return serviceClient.echo(echo).get_return();
	  }
	  
		public ADBBean getTestObject(Class type) throws Exception {
			return (ADBBean) type.newInstance();
		}
		
		public static void main(String[] args) {
			SampleClient sampleClient=new SampleClient();
			try {
				File file=new File("");
		    	String path=file.getAbsolutePath();
				String resaultValue=sampleClient.testserver(path);
				System.out.println("从服务端返回内容为:"+resaultValue);
			} catch (Exception e) {
				e.printStackTrace();
			}

		}
}

9)、在客户端调用的代码中,注意
configContext = ConfigurationContextFactory
	                .createConfigurationContextFromFileSystem(path+"/WebRoot/WEB-INF",path+"/WebRoot/WEB-INF/conf/axis2.xml");

没有找到方法直接取得当前绝对路径,所以区分为2种情况
a)、main方法调用时传入的当前的绝对路径作为参数。
File file=new File("");
String path=file.getAbsolutePath();
configContext = ConfigurationContextFactory
	                .createConfigurationContextFromFileSystem(path+"/WebRoot/WEB-INF",path+"/WebRoot/WEB-INF/conf/axis2.xml");

b)、客户端部署在web容器中,从页面或者action调用,传入request.getRealPath("/")作为参数,并且没有WebRoot目录。
configContext = ConfigurationContextFactory
	                .createConfigurationContextFromFileSystem(path+"/WEB-INF",path+"/WEB-INF/conf/axis2.xml");


你可能感兴趣的:(java,apache,Web,xml,Security)