有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java cxf wssecurity客户端不绑定配置文件

我尝试使用EclipseLuna和Wildfly 8.1部署ws-security soap web服务。以下是我的代码示例:

SEI

@WebService
@PolicySets({"WS-SP-EX223_WSS11_Anonymous_X509_Sign_Encrypt"})
public interface IHelloWorld {
    @WebMethod
    @WebResult
    public String sayHello(@WebParam String name);
}

服务豆

@WebService
@EndpointConfig(configFile = "WEB-INF/jaxws-endpoint-config.xml", configName = "Custom WS-Security Endpoint")
public class HelloWorld implements IHelloWorld {

    @Override
    public String sayHello(String name) {
        // TODO Auto-generated method stub
        return "Hello " + name;
    }
}

jaxws端点配置。xml

<jaxws-config xmlns="urn:jboss:jbossws-jaxws-config:4.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xmlns:javaee="http://java.sun.com/xml/ns/javaee" 
    xsi:schemaLocation="urn:jboss:jbossws-jaxws-config:4.0 schema/jbossws-jaxws-config_4_0.xsd">  

    <endpoint-config>    
        <config-name>Custom WS-Security Endpoint</config-name>    

        <property>      
            <property-name>ws-security.signature.properties</property-name>      
            <property-value>META-INF/server.properties</property-value>    
        </property>    

        <property>      
            <property-name>ws-security.encryption.properties</property-name>      
            <property-value>META-INF/server.properties</property-value>    
        </property>    

        <property>      
            <property-name>ws-security.signature.username</property-name>      
            <property-value>server</property-value>    
        </property>    

        <property>      
            <property-name>ws-security.encryption.username</property-name>      
            <property-value>client</property-value>    
        </property>    

        <property>      
            <property-name>ws-security.callback-handler</property-name>      
            <property-value>com.aaa.soap.KeystorePasswordCallback</property-value>    
        </property>  
    </endpoint-config>
</jaxws-config>

服务器。性质

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=password
org.apache.ws.security.crypto.merlin.keystore.alias=server
org.apache.ws.security.crypto.merlin.keystore.file=META-INF/server.jks

我支持相关的jar文件,部署成功。但我担心客户端代码会引发异常

客户。性质

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=password
org.apache.ws.security.crypto.merlin.keystore.alias=client
org.apache.ws.security.crypto.merlin.keystore.file=client.jks

SOAPClient。爪哇

public class SOAPClient {

    private final String serviceURL = "http://localhost:8080/SOAPSecureWeb/HelloWorld";

    private IHelloWorld port;

    public SOAPClient() {
        try {
            QName serviceName = new QName("http://soap.aaa.com/", "HelloWorldService");

            URL wsdlURL = new URL(serviceURL + "?wsdl");

            Service service = Service.create(wsdlURL, serviceName);
            port = (IHelloWorld) service.getPort(IHelloWorld.class);

            ((BindingProvider) port).getRequestContext().put(SecurityConstants.CALLBACK_HANDLER, new KeystorePasswordCallback());
            ((BindingProvider) port).getRequestContext().put(SecurityConstants.SIGNATURE_PROPERTIES, Thread.currentThread().getContextClassLoader().getResource("client.properties"));
            ((BindingProvider) port).getRequestContext().put(SecurityConstants.ENCRYPT_PROPERTIES, Thread.currentThread().getContextClassLoader().getResource("client.properties"));
            ((BindingProvider) port).getRequestContext().put(SecurityConstants.SIGNATURE_USERNAME, "client");
            ((BindingProvider) port).getRequestContext().put(SecurityConstants.ENCRYPT_USERNAME, "server");
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    } // the constructor throws no exception.

    public String callMethd(String name) {
        return port.sayHello("Joseph"); // this method throws exception
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        SOAPClient tc= new SOAPClient();
        String result=tc.callMethd("Joseph");
        System.out.println(result);
    }
}

例外情况如下:

Exception in thread "main" javax.xml.ws.soap.SOAPFaultException: These policy alternatives can not be satisfied: 
{http://docs.oasis-open.org/ws-sx/ws-securitypolicy/200702}ProtectionToken
{http://docs.oasis-open.org/ws-sx/ws-securitypolicy/200702}IncludeTimestamp: Received Timestamp does not match the requirements
{http://docs.oasis-open.org/ws-sx/ws-securitypolicy/200702}SymmetricBinding: Received Timestamp does not match the requirements
{http://docs.oasis-open.org/ws-sx/ws-securitypolicy/200702}EncryptedParts: {http://schemas.xmlsoap.org/soap/envelope/}Body not ENCRYPTED
{http://docs.oasis-open.org/ws-sx/ws-securitypolicy/200702}SignedParts: {http://schemas.xmlsoap.org/soap/envelope/}Body not SIGNED
    at org.apache.cxf.jaxws.JaxWsClientProxy.invoke(JaxWsClientProxy.java:157)
    at com.sun.proxy.$Proxy22.sayHello(Unknown Source)
    at com.aaa.soap.SOAPClient.callMethd(SOAPClient.java:38)
    at com.aaa.soap.SOAPClient.main(SOAPClient.java:44)
Caused by: org.apache.cxf.binding.soap.SoapFault: These policy alternatives can not be satisfied: 
{http://docs.oasis-open.org/ws-sx/ws-securitypolicy/200702}ProtectionToken
{http://docs.oasis-open.org/ws-sx/ws-securitypolicy/200702}IncludeTimestamp: Received Timestamp does not match the requirements
{http://docs.oasis-open.org/ws-sx/ws-securitypolicy/200702}SymmetricBinding: Received Timestamp does not match the requirements
{http://docs.oasis-open.org/ws-sx/ws-securitypolicy/200702}EncryptedParts: {http://schemas.xmlsoap.org/soap/envelope/}Body not ENCRYPTED
{http://docs.oasis-open.org/ws-sx/ws-securitypolicy/200702}SignedParts: {http://schemas.xmlsoap.org/soap/envelope/}Body not SIGNED
    at org.apache.cxf.binding.soap.interceptor.Soap11FaultInInterceptor.unmarshalFault(Soap11FaultInInterceptor.java:84)
    at org.apache.cxf.binding.soap.interceptor.Soap11FaultInInterceptor.handleMessage(Soap11FaultInInterceptor.java:51)
    at org.apache.cxf.binding.soap.interceptor.Soap11FaultInInterceptor.handleMessage(Soap11FaultInInterceptor.java:40)
    at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:272)
    at org.apache.cxf.interceptor.AbstractFaultChainInitiatorObserver.onMessage(AbstractFaultChainInitiatorObserver.java:113)
    at org.apache.cxf.binding.soap.interceptor.CheckFaultInterceptor.handleMessage(CheckFaultInterceptor.java:69)
    at org.apache.cxf.binding.soap.interceptor.CheckFaultInterceptor.handleMessage(CheckFaultInterceptor.java:34)
    at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:272)
    at org.apache.cxf.endpoint.ClientImpl.onMessage(ClientImpl.java:845)
    at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.handleResponseInternal(HTTPConduit.java:1624)
    at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.handleResponse(HTTPConduit.java:1513)
    at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.close(HTTPConduit.java:1318)
    at org.apache.cxf.transport.AbstractConduit.close(AbstractConduit.java:56)
    at org.apache.cxf.transport.http.HTTPConduit.close(HTTPConduit.java:632)
    at org.apache.cxf.interceptor.MessageSenderInterceptor$MessageSenderEndingInterceptor.handleMessage(MessageSenderInterceptor.java:62)
    at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:272)
    at org.apache.cxf.endpoint.ClientImpl.doInvoke(ClientImpl.java:570)
    at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:479)
    at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:382)
    at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:335)
    at org.apache.cxf.frontend.ClientProxy.invokeSync(ClientProxy.java:96)
    at org.apache.cxf.jaxws.JaxWsClientProxy.invoke(JaxWsClientProxy.java:135)
    ... 3 more

客户端代码在绑定客户端时似乎存在一些问题。属性配置文件


共 (1) 个答案

  1. # 1 楼答案

    试试这个:

    <property>      
            <property-name>ws-security.signature.properties</property-name>      
            <property-value>server.properties</property-value>    
        </property>   
    

    并将属性文件直接放在src文件夹下