Axis2和Rampart模块客户端

时间:2015-03-11 19:42:11

标签: java web-services axis2 ws-security rampart

我需要使用apache axis2和rampart模块的litle帮助,我正在尝试使用cliente但我遇到了错误

  

线程“main”中的异常java.lang.RuntimeException:未定义的“安全策略名称空间不能为空”。资源财产       at org.apache.rampart.RampartException.getMessage(RampartException.java:81)       在org.apache.rampart.RampartException。(RampartException.java:41)       在org.apache.rampart.RampartException。(RampartException.java:57)       在org.apache.rampart.RampartMessageData.setWSSecurityVersions(RampartMessageData.java:387)       在org.apache.rampart.RampartMessageData。(RampartMessageData.java:261)       在org.apache.rampart.MessageBuilder.build(MessageBuilder.java:61)       在org.apache.rampart.handler.RampartSender.invoke(RampartSender.java:65)       在org.apache.axis2.engine.Phase.invokeHandler(Phase.java:340)       在org.apache.axis2.engine.Phase.invoke(Phase.java:313)       在org.apache.axis2.engine.AxisEngine.invoke(AxisEngine.java:262)       在org.apache.axis2.engine.AxisEngine.send(AxisEngine.java:427)       在org.apache.axis2.description.OutInAxisOperationClient.send(OutInAxisOperation.java:406)       at org.apache.axis2.description.OutInAxisOperationClient.executeImpl(OutInAxisOperation.java:229)       在org.apache.axis2.client.OperationClient.execute(OperationClient.java:165)       在axis2.services.SimpleServiceStub.sumar(SimpleServiceStub.java:189)       在axis2client.Application1。(Application1.java:96)       在axis2client.Application1.main(Application1.java:192)   进程退出,退出代码为1。

这是我的网络服务客户端的源代码:

public Application1() throws Exception {
    SimpleServiceStub stub;
    try {
        ConfigurationContext ctx = ConfigurationContextFactory.createConfigurationContextFromFileSystem("C:\\AESPANA\\Axis2Test\\Axis2Client\\src\\axis2conf\\SimpleServiceConf",null);
        stub = new SimpleServiceStub(ctx,"http://172.17.24.147:8995/Axis2Service/services/SimpleService?wsdl");

        ServiceClient client=stub._getServiceClient();
        client.engageModule("rampart");

        RampartConfig rampartConfig=getRampartConfig();
        out_sec_policy.addAssertion(rampartConfig);

        client.getOptions().setProperty(RampartMessageData.KEY_RAMPART_OUT_POLICY, out_sec_policy);

        stub._setServiceClient(client);

        System.out.println("Llamando Cliente");
        SimpleServiceStub.Sumar sum1=new SimpleServiceStub.Sumar();
        sum1.setA(2);
        sum1.setB(3);
        System.out.println("Ejecutandolo Cliente");
        SimpleServiceStub.SumarResponse response1=stub.sumar(sum1);
        System.out.println(response1);

    } catch (AxisFault e) {
        e.printStackTrace();
    } catch (RemoteException e) {
        e.printStackTrace();
    }
}

private Policy loadPolicy(String name) throws XMLStreamException {
        InputStream resource;
    try {
        resource = new FileInputStream(name);
        StAXOMBuilder builder = new StAXOMBuilder(resource);
        return PolicyEngine.getPolicy(builder.getDocumentElement());
    } catch (FileNotFoundException e) {
        // TODO
    }      
    return null;
}

private RampartConfig getRampartConfig(){

                RampartConfig rampartConfig = new RampartConfig();
                rampartConfig.setPwCbClass("axis2client.SimpleServicePWCBHandle");



                CryptoConfig sigCrypto = new CryptoConfig();

                sigCrypto.setProvider("org.apache.ws.security.components.crypto.Merlin");

                Properties props = new Properties();
                props.setProperty("org.apache.ws.security.crypto.merlin.keystore.type", "JKS");
                props.setProperty("org.apache.ws.security.crypto.merlin.file","C:\\AESPANA\\Axis2Test\\Axis2Client\\src\\axis2conf\\SimpleServiceConf\\keys\\client.jks");
                props.setProperty("org.apache.ws.security.crypto.merlin.keystore.password", "apache");

                sigCrypto.setProp(props);


                rampartConfig.setSigCryptoConfig(sigCrypto);
                rampartConfig.setEncrCryptoConfig(sigCrypto);

                Policy policy = new Policy();

                return rampartConfig;

        }

public static void main(String[] args) throws Exception {
    new Application1();
}}

这是客户端的policy.xml文件。

<wsp:Policy wsu:Id="PoliticaOperacion"
            xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy"
            xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"
            xmlns:sp="http://docs.oasis-open.org/ws-sx/ws-securitypolicy/200802">
    <wsp:ExactlyOne>
        <wsp:All>
            <sp:AsymmetricBinding>
                <wsp:Policy>
                    <sp:InitiatorToken>
                        <wsp:Policy>
                            <sp:X509Token sp:IncludeToken="http://docs.oasis-open.org/ws-sx/ws-securitypolicy/200702/IncludeToken/AlwaysToRecipient">
                                <wsp:Policy>
                                    <sp:WssX509V3Token10/>
                                </wsp:Policy>
                            </sp:X509Token>
                        </wsp:Policy>
                    </sp:InitiatorToken>
                    <sp:RecipientToken>
                        <wsp:Policy>
                            <sp:X509Token sp:IncludeToken="http://docs.oasis-open.org/ws-sx/ws-securitypolicy/200702/IncludeToken/Never">
                                <wsp:Policy>
                                    <sp:WssX509V3Token10/>
                                </wsp:Policy>
                            </sp:X509Token>
                        </wsp:Policy>
                    </sp:RecipientToken>
                    <sp:AlgorithmSuite>
                        <wsp:Policy>
                            <sp:TripleDesRsa15/>
                        </wsp:Policy>
                    </sp:AlgorithmSuite>
                    <sp:Layout>
                        <wsp:Policy>
                            <sp:Strict/>
                        </wsp:Policy>
                    </sp:Layout>
                    <sp:IncludeTimestamp/>
                    <sp:OnlySignEntireHeadersAndBody/>
                </wsp:Policy>
            </sp:AsymmetricBinding>
            <sp:Wss10>
                <wsp:Policy>
                    <sp:MustSupportRefKeyIdentifier/>
                </wsp:Policy>
            </sp:Wss10>
        </wsp:All>
    </wsp:ExactlyOne>
</wsp:Policy>

services.xml就是这个。

<serviceGroup>
    <service name="SimpleService"
             targetNamespace="http://ws.apache.org/axis2/samples/book/"
             xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy"
             xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"
             xmlns:sp="http://docs.oasis-open.org/ws-sx/ws-securitypolicy/200802">
        <module ref="rampart"/>
        <description>SimpleService</description>
        <schema schemaNamespace="http://ws.apache.org/axis2/samples/book/xsd/"/>
        <parameter name="ServiceClass" locked="false">axis2service.SimpleService</parameter>
        <operation name="sumar">
            <messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver"/>
        </operation>
        <parameter name="InflowSecurity">
            <action>
                <passwordCallbackClass>axis2service.SimpleServiceHandler</passwordCallbackClass>
                <signaturePropFile>service.properties</signaturePropFile>
                <signatureKeyIdentifier>DirectReference</signatureKeyIdentifier>
            </action>
        </parameter>
        <wsp:PolicyAttachment xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy">
            <wsp:AppliesTo>
                <policy-subject identifier="binding:soap11/operation:sumar/in"/>
                <policy-subject identifier="binding:soap12/operation:sumar/in"/>
            </wsp:AppliesTo>
            <wsp:Policy wsu:Id="PoliticaOperacion">
                <wsp:ExactlyOne>
                    <wsp:All>
                        <sp:AsymmetricBinding>
                            <wsp:Policy>
                                <sp:InitiatorToken>
                                    <wsp:Policy>
                                        <sp:X509Token sp:IncludeToken="http://docs.oasis-open.org/ws-sx/ws-securitypolicy/200702/IncludeToken/AlwaysToRecipient">
                                            <wsp:Policy>
                                                <sp:WssX509V3Token10/>
                                            </wsp:Policy>
                                        </sp:X509Token>
                                    </wsp:Policy>
                                </sp:InitiatorToken>
                                <sp:RecipientToken>
                                    <wsp:Policy>
                                        <sp:X509Token sp:IncludeToken="http://docs.oasis-open.org/ws-sx/ws-securitypolicy/200702/IncludeToken/Never">
                                            <wsp:Policy>
                                                <sp:WssX509V3Token10/>
                                            </wsp:Policy>
                                        </sp:X509Token>
                                    </wsp:Policy>
                                </sp:RecipientToken>
                                <sp:AlgorithmSuite>
                                    <wsp:Policy>
                                        <sp:TripleDesRsa15/>
                                    </wsp:Policy>
                                </sp:AlgorithmSuite>
                                <sp:Layout>
                                    <wsp:Policy>
                                        <sp:Strict/>
                                    </wsp:Policy>
                                </sp:Layout>
                                <sp:IncludeTimestamp/>
                                <sp:OnlySignEntireHeadersAndBody/>
                            </wsp:Policy>
                        </sp:AsymmetricBinding>
                        <sp:Wss10>
                            <wsp:Policy>
                                <sp:MustSupportRefKeyIdentifier/>
                            </wsp:Policy>
                        </sp:Wss10>
                    </wsp:All>
                </wsp:ExactlyOne>
            </wsp:Policy>
        </wsp:PolicyAttachment>
    </service>
</serviceGroup>

这是WSDL文件

  <?xml version="1.0" encoding="UTF-8" ?> 
- <wsdl:definitions xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:wsaw="http://www.w3.org/2006/05/addressing/wsdl" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:ns="http://ws.apache.org/axis2/samples/book/xsd/" xmlns:ns1="http://org.apache.axis2/xsd" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:tns="http://ws.apache.org/axis2/samples/book/" targetNamespace="http://ws.apache.org/axis2/samples/book/">
- <wsp:Policy xmlns:wsp="http://www.w3.org/ns/ws-policy" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" wsu:Id="PoliticaOperacion">
- <wsp:ExactlyOne>
- <wsp:All>
- <sp:AsymmetricBinding xmlns:sp="http://docs.oasis-open.org/ws-sx/ws-securitypolicy/200802">
- <wsp:Policy xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy">
- <sp:InitiatorToken>
- <wsp:Policy>
- <sp:X509Token>
- <wsp:Policy>
  <sp:WssX509V3Token10 /> 
  </wsp:Policy>
  </sp:X509Token>
  </wsp:Policy>
  </sp:InitiatorToken>
- <sp:RecipientToken>
- <wsp:Policy>
- <sp:X509Token>
- <wsp:Policy>
  <sp:WssX509V3Token10 /> 
  </wsp:Policy>
  </sp:X509Token>
  </wsp:Policy>
  </sp:RecipientToken>
- <sp:AlgorithmSuite>
- <wsp:Policy>
  <sp:TripleDesRsa15 /> 
  </wsp:Policy>
  </sp:AlgorithmSuite>
- <sp:Layout>
- <wsp:Policy>
  <sp:Strict /> 
  </wsp:Policy>
  </sp:Layout>
  <sp:IncludeTimestamp /> 
  <sp:OnlySignEntireHeadersAndBody /> 
  </wsp:Policy>
  </sp:AsymmetricBinding>
- <sp:Wss10 xmlns:sp="http://docs.oasis-open.org/ws-sx/ws-securitypolicy/200802">
- <wsp:Policy xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy">
  <sp:MustSupportRefKeyIdentifier /> 
  </wsp:Policy>
  </sp:Wss10>
  </wsp:All>
  </wsp:ExactlyOne>
  </wsp:Policy>
  <wsdl:documentation>SimpleService</wsdl:documentation> 
- <wsdl:types>
- <xs:schema targetNamespace="http://ws.apache.org/axis2/samples/book/xsd/" attributeFormDefault="qualified" elementFormDefault="qualified">
- <xs:element name="sumar">
- <xs:complexType>
- <xs:sequence>
  <xs:element name="a" minOccurs="0" type="xs:int" /> 
  <xs:element name="b" minOccurs="0" type="xs:int" /> 
  </xs:sequence>
  </xs:complexType>
  </xs:element>
- <xs:element name="sumarResponse">
- <xs:complexType>
- <xs:sequence>
  <xs:element name="return" minOccurs="0" type="xs:int" /> 
  </xs:sequence>
  </xs:complexType>
  </xs:element>
  </xs:schema>
  </wsdl:types>
- <wsdl:message name="sumarRequest">
  <wsdl:part name="parameters" element="ns:sumar" /> 
  </wsdl:message>
- <wsdl:message name="sumarResponse">
  <wsdl:part name="parameters" element="ns:sumarResponse" /> 
  </wsdl:message>
- <wsdl:portType name="SimpleServicePortType">
- <wsdl:operation name="sumar">
  <wsdl:input message="tns:sumarRequest" wsaw:Action="urn:sumar" /> 
  <wsdl:output message="tns:sumarResponse" wsaw:Action="urn:sumarResponse" /> 
  </wsdl:operation>
  </wsdl:portType>
- <wsdl:binding name="SimpleServiceSoap11Binding" type="tns:SimpleServicePortType">
  <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document" /> 
- <wsdl:operation name="sumar">
  <soap:operation soapAction="urn:sumar" style="document" /> 
- <wsdl:input>
  <wsp:PolicyReference xmlns:wsp="http://www.w3.org/ns/ws-policy" URI="#PoliticaOperacion" /> 
  <soap:body use="literal" /> 
  </wsdl:input>
- <wsdl:output>
  <soap:body use="literal" /> 
  </wsdl:output>
  </wsdl:operation>
  </wsdl:binding>
- <wsdl:binding name="SimpleServiceSoap12Binding" type="tns:SimpleServicePortType">
  <soap12:binding transport="http://schemas.xmlsoap.org/soap/http" style="document" /> 
- <wsdl:operation name="sumar">
  <soap12:operation soapAction="urn:sumar" style="document" /> 
- <wsdl:input>
  <wsp:PolicyReference xmlns:wsp="http://www.w3.org/ns/ws-policy" URI="#PoliticaOperacion" /> 
  <soap12:body use="literal" /> 
  </wsdl:input>
- <wsdl:output>
  <soap12:body use="literal" /> 
  </wsdl:output>
  </wsdl:operation>
  </wsdl:binding>
- <wsdl:binding name="SimpleServiceHttpBinding" type="tns:SimpleServicePortType">
  <http:binding verb="POST" /> 
- <wsdl:operation name="sumar">
  <http:operation location="sumar" /> 
- <wsdl:input>
  <mime:content type="application/xml" part="parameters" /> 
  </wsdl:input>
- <wsdl:output>
  <mime:content type="application/xml" part="parameters" /> 
  </wsdl:output>
  </wsdl:operation>
  </wsdl:binding>
- <wsdl:service name="SimpleService">
- <wsdl:port name="SimpleServiceHttpSoap11Endpoint" binding="tns:SimpleServiceSoap11Binding">
  <soap:address location="http://http://172.17.24.150:8995/Axis2Service/services/SimpleService.SimpleServiceHttpSoap11Endpoint/" /> 
  </wsdl:port>
- <wsdl:port name="SimpleServiceHttpSoap12Endpoint" binding="tns:SimpleServiceSoap12Binding">
  <soap12:address location="http://http://172.17.24.150:8995/Axis2Service/services/SimpleService.SimpleServiceHttpSoap12Endpoint/" /> 
  </wsdl:port>
- <wsdl:port name="SimpleServiceHttpEndpoint" binding="tns:SimpleServiceHttpBinding">
  <http:address location="http://http://172.17.24.150:8995/Axis2Service/services/SimpleService.SimpleServiceHttpEndpoint/" /> 
  </wsdl:port>
  </wsdl:service>
  </wsdl:definitions>

我的服务类就是这个

public class SimpleService {

    public int sumar(int a, int b){
        return a+b;
    }
}

任何人都可以帮助我,我不知道如何解决它。任何想法朋友?

0 个答案:

没有答案