首先搭建基于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");
分享到:
相关推荐
这个文档详细描述了本人实现axis2+rampart ,有截图和详细配置
纯java调用ws-security+CXF实现的webservice安全接口
axis2-eclipse-codegen-plugin-1.6.2+axis2-eclipse-service-plugin-1.6.2
axis2-eclipse-codegen-plugin-1.6.2.zip和axis2-eclipse-service-plugin-1.6.2.zip,解压即可使用
axis2-idea-plugin-1.7.9
共四个文件,都是最先版的,希望可以帮助大家。axis2-eclipse-service-archiver-wizard和axis2-eclipse-codegen-wizard和axis2-1.6.1-bin和axis2-1.6.1-war
内含: ISNetworksProvider.jar tsik.jar ws-security.jar 和wss4j的所有包
axis2-1.6.2-war、axis2-1.6.1-war、axis2-1.6.2-bin
绝对可以用,里面配有myeclipse10安装AXIS2插件的步骤,希望大家工作愉快,谢谢,给好评哦
axis2-eclipse-codegen-plugin-1.6.0和axis2-eclipse-service-plugin-1.6.0有关jar包下载,解压后直接将这两个jar包放到eclips下的plugins目录下,重启eclipse即可
axis2-eclipse-service-archiver-wizard.zip
axis2-1.5.4-bin.zip + axis2-1.5.4-war.zip
axis2-eclipse-codegen-plugin-1.6.1.zip axis2-eclipse-codegen-plugin-1.6.1.zip
axis2-std-1.0-bin.zip与axis2.war axis2-std-1.0-bin.zip与axis2.war
目前axis2最高版本是2.0以上的版本,但是eclipse和myeclipse都不支持,无奈只能使用低版本的插件1.6.3;经实验,可以安装成功; 安装方法:右键解压到当前文件夹,Copy解压的文件到eclipse安装目录dropins下,重启...
用于axis2开发的一个必不可少的包,开发时需要把包安置在eclipse下
axis2-eclipse-codegen-plugin-1.6.0.zip
axis2-eclipse-service-plugin-1.7.4.zip eclipse插件
Axis2是下一代 Apache ...支持开发 Axis2 的动力是探寻模块化更强、灵活性更高和更有效的体系结构,这种体系结构可以很容易地插入到其他相关 Web 服务标准和协议(如 WS-Security、WS-ReliableMessaging 等)的实现中。
axis2-eclipse-service-plugin-1.6.2