axis2集成安全和jibx

1.先写服务类,内容如下:

public class StockQuoteService {
    private HashMap map = new HashMap();

    public Man getPrice(String symbol) {
        Double price = (Double) map.get(symbol);
        Man man = new Man(42.2);
        man.setName("kevin");
        man.setSex("man");
 
        return man;
    }

    public void update(String symbol, double price) {
        map.put(symbol, new Double(price));
    }
}

 

2.用ant生成wsdl,xsd,binding.xml,内容如下:

<project name="quickstart" basedir="." default="generate-wsdl">
 <property name="jibx-home" value="C:\Users\Kevin\Downloads\jibx_1_2_3\jibx" />

 <path id="jibx-classpath">
  <fileset dir="${jibx-home}/lib" includes="**/*.jar" />
  <pathelement location="bin" />
 </path>

 <!-- Generate bindings, schemas, and WSDL -->
 <target name="generate-wsdl">
  <delete quiet="true" dir="${basedir}/gen" />
  <mkdir dir="${basedir}/gen" />
  <echo message="Running Jibx2Wsdl tool" />
  <java classpathref="jibx-classpath" classname="org.jibx.ws.wsdl.tools.Jibx2Wsdl" fork="true" failοnerrοr="true">
   <arg value="-p" />
   <arg value="target/classes" />
   <arg value="-s" />
   <arg value="src/main/java" />
   <arg value="-t" />
   <arg value="${basedir}/gen" />
   <arg value="--strip-prefixes=ns" />
   <arg value="com.kevin.StockQuoteService" />
  </java>
 </target>
</project>

 

3.用ant生成service代码,ant内容如下:

<project name="quickstart" basedir="." default="generate.wsdl">
 <property environment="env" />
 <property name="project.home" value="src/main/webapp" />
 <property name="resource.path" value="src/main/resources" />
 <property name="build.dir" value="target" />

 <path id="axis2.classpath">
  <fileset dir="${project.home}/WEB-INF/lib">
   <include name="*.jar" />
  </fileset>
 </path>

 <path id="client.lib">
  <fileset dir="lib">
   <include name="*.jar" />
  </fileset>
 </path>

 <target name="generate.wsdl">
  <java classname="org.apache.ws.java2wsdl.Java2WSDL" fork="true" classpathref="axis2.classpath">
   <arg line="-cp target/classes" />
   <arg line="-cn com.kevin.StockQuoteService" />
   <arg line="-of ${resource.path}/META-INF/StockQuoteService.wsdl" />
  </java>
 </target>

 <target name="generate.service">
  <java classname="org.apache.axis2.wsdl.WSDL2Java" fork="true" classpathref="client.lib">
   <arg line="-s" />
   <arg line="-ss" />
   <arg line="-uw" />
   <arg line="-sd" />
   <arg line="-ssi" />
   <arg line="-ap" />
   <arg line="-S" />
   <arg line="-l java" />
   <arg line="-p com.kevin.service" />
   <arg line="-d jibx" />
   <arg line="-noBuildXML" />
   <arg line="-o src/main/java" />
   <arg line="-sp" />
   <arg line="-Ebindingfile ${resource.path}/META-INF/binding.xml" />
   <arg line="-uri ${resource.path}/META-INF/StockQuoteService.wsdl" />
  </java>
  <delete dir="src/main/java/resources" />
 </target>

 <target name="generate.aar">
  <jar destfile="${project.home}/WEB-INF/services/StockQuoteService.aar">
   <fileset excludes="**/Test.class, *.properties" dir="${build.dir}/classes">
   </fileset>
  </jar>
 </target>

 <target name="generate.client">
  <java classname="org.apache.axis2.wsdl.WSDL2Java" fork="true" classpathref="client.lib">
   <arg line="-S" />
   <arg line="-s" />
   <arg line="-uw" />
   <arg line="-noBuildXML" />
   <arg line="-p com.kevin.client" />
   <arg line="-d jibx" />
   <arg line="-o src/main/java" />
   <arg line="-sp" />
   <arg line="-Ebindingfile ${resource.path}/META-INF/binding.xml" />
   <arg line="-uri ${resource.path}/META-INF/StockQuoteService.wsdl" />
  </java>
 </target>
</project>

4.在生成的service代码中,在类StockQuoteServiceSkeleton中加入业务代码,参照StockQuoteService.

 

5.增加service.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<service name="StockQuoteService" scope="application">
 <description>
  Stock Quote Sample Service
    </description>
 <messageReceivers>
  <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out"
   class="com.kevin.service.StockQuoteServiceMessageReceiverInOut" />
 </messageReceivers>
 <parameter name="ServiceClass">
  com.kevin.service.StockQuoteServiceSkeleton
    </parameter>

 <module ref="rampart" />
 <module ref="addressing" />

 <wsp:Policy wsu:Id="UTOverTransport"
  xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"
  xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy">
  <wsp:ExactlyOne>
   <wsp:All>
    <sp:TransportBinding
     xmlns:sp="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy">
     <wsp:Policy>
      <sp:TransportToken>
       <wsp:Policy>
        <sp:HttpsToken RequireClientCertificate="false" />
       </wsp:Policy>
      </sp:TransportToken>
      <sp:AlgorithmSuite>
       <wsp:Policy>
        <sp:Basic128 />
       </wsp:Policy>
      </sp:AlgorithmSuite>
      <sp:Layout>
       <wsp:Policy>
        <sp:Lax />
       </wsp:Policy>
      </sp:Layout>
      <sp:IncludeTimestamp />
     </wsp:Policy>
    </sp:TransportBinding>
    <sp:SignedSupportingTokens
     xmlns:sp="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy">
     <wsp:Policy>
      <sp:UsernameToken
       sp:IncludeToken="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy/IncludeToken/AlwaysToRecipient" />
     </wsp:Policy>
    </sp:SignedSupportingTokens>

    <ramp:RampartConfig xmlns:ramp="http://ws.apache.org/rampart/policy">
     <ramp:passwordCallbackClass>com.kevin.PWCBHandler</ramp:passwordCallbackClass>
    </ramp:RampartConfig>

   </wsp:All>
  </wsp:ExactlyOne>
 </wsp:Policy>

</service>

6.生成aar文件,可以不包含*.class文件,拷至WEB-INF/services文件夹中。

 

7.生成client文件,并拷至client端。

 

8.客户端调用程序:

public static void main(String[] args) {
  try {
   ConfigurationContext ctx = ConfigurationContextFactory
     .createConfigurationContextFromFileSystem(
       "src/main/webapp/WEB-INF", null);

   StockQuoteServiceStub stub = new StockQuoteServiceStub(ctx,
     "https://localhost:8443/axis2/services/StockQuoteService");
   ServiceClient sc = stub._getServiceClient();
   Options options = sc.getOptions();
   options.setProperty(RampartMessageData.KEY_RAMPART_POLICY,
     loadPolicy("policy.xml"));
   sc.setOptions(options);

   sc.engageModule("addressing");
   sc.engageModule("rampart");
   
   getPrice(stub);
//   update(stub);
//   getPrice(stub);

  } catch (Exception e) {
   e.printStackTrace();
  }
 }
 
 public static void getPrice(StockQuoteServiceStub stub) {
  try {
   Man man = stub.getPrice("CDE");
   
   System.out.println(man.getName());
   System.out.println(man.getSex());
   System.out.println(man.getPrice());
  } catch (Exception e) {
   e.printStackTrace();
   System.err.println("\n\n\n");
  }
 }

 public static void update(StockQuoteServiceStub stub) {
  try {
   stub.update("CDE", new Double(42.35));
   System.err.println("price updated");
  } catch (Exception e) {
   e.printStackTrace();
   System.err.println("\n\n\n");
  }
 }

 private static Policy loadPolicy(String xmlPath) throws Exception {
  StAXOMBuilder builder = new StAXOMBuilder(xmlPath);
  return PolicyEngine.getPolicy(builder.getDocumentElement());
 }

 

9.其中policy.xml内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<wsp:Policy wsu:Id="UTOverTransport" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy">
 <wsp:ExactlyOne>
   <wsp:All>
  <sp:TransportBinding xmlns:sp="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy">
    <wsp:Policy>
   <sp:TransportToken>
     <wsp:Policy>
    <sp:HttpsToken RequireClientCertificate="false"/>
     </wsp:Policy>
   </sp:TransportToken>
   <sp:AlgorithmSuite>
     <wsp:Policy>
    <sp:Basic128/>
     </wsp:Policy>
   </sp:AlgorithmSuite>
   <sp:Layout>
     <wsp:Policy>
    <sp:Lax/>
     </wsp:Policy>
   </sp:Layout>
   <sp:IncludeTimestamp/>
    </wsp:Policy>
  </sp:TransportBinding>
  <sp:SignedSupportingTokens xmlns:sp="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy">
   <wsp:Policy>
    <sp:UsernameToken sp:IncludeToken="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy/IncludeToken/AlwaysToRecipient" />
    </wsp:Policy>
  </sp:SignedSupportingTokens>
  
  <ramp:RampartConfig xmlns:ramp="http://ws.apache.org/rampart/policy">
   <ramp:user>alice</ramp:user>
   <ramp:passwordCallbackClass>com.kevin.client.PWCBHandler</ramp:passwordCallbackClass>
   <ramp:sslConfig>
     <ramp:property name="javax.net.ssl.trustStore">client.jks</ramp:property>
           <ramp:property name="javax.net.ssl.trustStorePassword">apache</ramp:property>
   </ramp:sslConfig>
  </ramp:RampartConfig>
  
   </wsp:All>
 </wsp:ExactlyOne>
</wsp:Policy>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值