Axis 1.4 基于wss4j UsernameToken 的安全验证

 

利用以下的wsdl文件生成客户端及服务端代码:

<?xml version="1.0" encoding="UTF-8"?>
<wsdl:definitions targetNamespace="http://localhost:8080/axis/services/stock-wss-01" xmlns:apachesoap="http://xml.apache.org/xml-soap" xmlns:impl="http://localhost:8080/axis/services/stock-wss-01" xmlns:intf="http://localhost:8080/axis/services/stock-wss-01" xmlns:tns1="http://stock.samples" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<!--WSDL created by Apache Axis version: 1.4
Built on Apr 22, 2006 (06:55:48 PDT)-->
 <wsdl:types>
  <schema elementFormDefault="qualified" targetNamespace="http://stock.samples" xmlns="http://www.w3.org/2001/XMLSchema">
   <element name="symbol" type="xsd:string"/>
  </schema>
  <schema elementFormDefault="qualified" targetNamespace="http://localhost:8080/axis/services/stock-wss-01" xmlns="http://www.w3.org/2001/XMLSchema">
   <element name="getQuoteReturn" type="xsd:float"/>
  </schema>

 </wsdl:types>

   <wsdl:message name="getQuoteResponse">

      <wsdl:part element="impl:getQuoteReturn" name="getQuoteReturn"/>

   </wsdl:message>

   <wsdl:message name="getQuoteRequest">

      <wsdl:part element="tns1:symbol" name="symbol"/>

   </wsdl:message>

   <wsdl:portType name="StockQuoteService">

      <wsdl:operation name="getQuote" parameterOrder="symbol">

         <wsdl:input message="impl:getQuoteRequest" name="getQuoteRequest"/>

         <wsdl:output message="impl:getQuoteResponse" name="getQuoteResponse"/>

      </wsdl:operation>

   </wsdl:portType>

   <wsdl:binding name="stock-wss-01SoapBinding" type="impl:StockQuoteService">

      <wsdlsoap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>

      <wsdl:operation name="getQuote">

         <wsdlsoap:operation soapAction=""/>

         <wsdl:input name="getQuoteRequest">

            <wsdlsoap:body use="literal"/>

         </wsdl:input>

         <wsdl:output name="getQuoteResponse">

            <wsdlsoap:body use="literal"/>

         </wsdl:output>

      </wsdl:operation>

   </wsdl:binding>

   <wsdl:service name="StockQuoteServiceService">

      <wsdl:port binding="impl:stock-wss-01SoapBinding" name="stock-wss-01">

         <wsdlsoap:address location="http://localhost:8080/axis/services/stock-wss-01"/>

      </wsdl:port>

   </wsdl:service>

</wsdl:definitions>

 

服务端和客户端需要都存在这个CallbackHandler的实现类 :

 

package samples.stock.client.usernametoken;

import java.io.IOException;

import javax.security.auth.callback.Callback;

import javax.security.auth.callback.CallbackHandler;

import javax.security.auth.callback.UnsupportedCallbackException;

import org.apache.ws.security.WSPasswordCallback;

/**
 * 
 * PWCallback for the Client
 */

public class PWCallback implements CallbackHandler {

    /**
     * 
     * @see javax.security.auth.callback.CallbackHandler#handle(javax.security.auth.callback.Callback[])
     */

    public void handle(Callback[] callbacks) throws IOException,

    UnsupportedCallbackException {

        for (int i = 0; i < callbacks.length; i++) {

            if (callbacks[i] instanceof WSPasswordCallback) {

                WSPasswordCallback pc = (WSPasswordCallback) callbacks[i];

                // set the password given a username

                if ("wss4j".equals(pc.getIdentifier())) {

                    pc.setPassword("security");

                }

            } else {

                throw new UnsupportedCallbackException(callbacks[i], "Unrecognized Callback");

            }

        }

    }

}
 
/**
 * 
 */
package samples.stock.client.usernametoken;

import java.net.URL;
import java.rmi.RemoteException;

import javax.xml.rpc.ServiceException;

import org.apache.axis.EngineConfiguration;
import org.apache.axis.configuration.FileProvider;

import samples.stock.StockQuoteService;
import samples.stock.StockQuoteServiceService;
import samples.stock.StockQuoteServiceServiceLocator;

/**
 * @author linzq
 * 
 * 
 */
public class StockServiceClient {

    public static void main(String[] args) throws ServiceException, RemoteException {
        if (args.length == 0) {

            System.out.println("Usage:\njava StockServiceClient [symbol]");
            return;

        }
        
        // 经测试OK,可以正常使用

        // 使用client_deploy.wsdd来配置UsernameToken消息头
        EngineConfiguration config = new FileProvider("src/samples/stock/client/usernametoken/client_deploy.wsdd");
        StockQuoteServiceService locator = new StockQuoteServiceServiceLocator(config);

        String url = "http://localhost:9999/axis/services/stock-wss-01";
        StockQuoteService service = null;
        try {
            service = locator.getStockWss01(new URL(url));
            float quote = service.getQuote(args[0]);
            System.out.println("stock quote service returned " + args[0] + ": " + quote);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}

 deploy.wsdd文件配置:

<service name="stock-wss-01" provider="java:RPC" style="document" use="literal">
	<requestFlow>
		<handler type="java:org.apache.ws.axis.security.WSDoAllReceiver">
			<parameter name="passwordCallbackClass" value="samples.stock.PWCallback"/>
			<parameter name="action" value="UsernameToken"/>
		</handler>
	</requestFlow>
      <parameter name="wsdlTargetNamespace" value="http://localhost:8080/axis/services/stock-wss-01"/>
      <parameter name="wsdlServiceElement" value="StockQuoteServiceService"/>
      <parameter name="schemaQualified" value="http://localhost:8080/axis/services/stock-wss-01,http://stock.samples"/>
      <parameter name="wsdlServicePort" value="stock-wss-01"/>
      <parameter name="className" value="samples.stock.StockWss01SoapBindingImpl"/>
      <parameter name="wsdlPortType" value="StockQuoteService"/>
      <parameter name="typeMappingVersion" value="1.2"/>
      <operation name="getQuote" qname="getQuote" returnQName="retNS:getQuoteReturn" xmlns:retNS="http://localhost:8080/axis/services/stock-wss-01" returnType="rtns:float" xmlns:rtns="http://www.w3.org/2001/XMLSchema" soapAction="" >
        <parameter qname="pns:symbol" xmlns:pns="http://stock.samples" type="tns:string" xmlns:tns="http://www.w3.org/2001/XMLSchema"/>
      </operation>
      <parameter name="allowedMethods" value="getQuote"/>
  </service>

 client_deploy.wsdd

<deployment xmlns="http://xml.apache.org/axis/wsdd/"
            xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">
	<transport name="http" pivot="java:org.apache.axis.transport.http.HTTPSender"/>
	<globalConfiguration >
		<requestFlow >
			<handler type="java:org.apache.ws.axis.security.WSDoAllSender" >
				<parameter name="action" value="UsernameToken"/>
				<parameter name="user" value="wss4j"/>
				<parameter name="passwordCallbackClass" value="samples.stock.client.usernametoken.PWCallback"/>
				<parameter name="passwordType" value="PasswordDigest"/>
			</handler>
		</requestFlow >
	</globalConfiguration >
</deployment>
 

你可能感兴趣的:(token)