2012-08-22 70 views
10

Estoy intentando crear un cliente de WS de terceros. Mi aplicación se ejecuta en JBoss AS 6 (con su pila Apache CXF 2.3.1). Genere el código del cliente por wsconsume (wsdl2java). Cuando intenté conectar con WS una excepción conseguido:Apache CXF - Ninguna de las alternativas de política se puede satisfacer

No assertion builder for type http://schemas.microsoft.com/ws/06/2004/policy/http}BasicAuthentication registered. 
Exception in thread "main" org.apache.cxf.ws.policy.PolicyException: None of the policy alternatives can be satisfied. 

de autenticación parte de WSDL se parece a:

<wsp:Policy wsu:Id="abc_ssl_policy"> 
    <wsp:ExactlyOne> 
     <wsp:All> 
      <http:BasicAuthentication 
       xmlns:http="http://schemas.microsoft.com/ws/06/2004/policy/http" /> 
      <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:Basic256 /> 
         </wsp:Policy> 
        </sp:AlgorithmSuite> 
        <sp:Layout> 
         <wsp:Policy> 
          <sp:Strict /> 
         </wsp:Policy> 
        </sp:Layout> 
       </wsp:Policy> 
      </sp:TransportBinding> 
     </wsp:All> 
    </wsp:ExactlyOne> 
</wsp:Policy> 

El código de cliente:

@WebServiceClient(name = "Abc", 
       wsdlLocation = "https://hiddendomain.com/abc/abc.svc?wsdl", 
       targetNamespace = "http://tempuri.org/")     
public class Abc extends Service { 

public final static URL WSDL_LOCATION; 

public final static QName SERVICE = new QName("http://tempuri.org/", "Abc"); 
public final static QName AbcSsl = new QName("http://tempuri.org/", "abc_ssl"); 
static { 

    Authenticator.setDefault(new Authenticator() { 
     @Override 
     protected PasswordAuthentication getPasswordAuthentication() { 
      return new PasswordAuthentication("user", "pas".toCharArray()); 
     } 

    }); 

    URL url = null; 
    try { 
     url = new URL("https://hiddendomain.com/abc/abc.svc?wsdl"); 

    } catch (MalformedURLException e) { 
     java.util.logging.Logger.getLogger(DistrInfo.class.getName()) 
      .log(java.util.logging.Level.INFO, 
       "Can not initialize the default wsdl from {0}", "..."); 
    } 
    WSDL_LOCATION = url; 
} 

excepción se produce cuando planees Intento conseguir Conduit :

Client client = ClientProxy.getClient(port); 
    HTTPConduit con = (HTTPConduit) client.getConduit(); <- exception 

I s Tenga en cuenta que esto se debe a una política de EM no estándar y necesito un Interceptor adecuado para manejar esta política, pero ¿alguien puede mostrarme una forma de hacerlo?

yo incluso no, donde debo poner mis credenciales HTTPS auth (no puedo conseguir conducto)

Respuesta

11

El problema ha ido cuando he usado este código:

import org.apache.cxf.endpoint.Client; 
import org.apache.cxf.frontend.ClientProxy; 
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean; 
import org.apache.cxf.transport.http.HTTPConduit; 

... 

JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean(); 

//factory.getInInterceptors().add(new LoggingInInterceptor()); 
//factory.getOutInterceptors().add(new LoggingOutInterceptor()); 

factory.setServiceClass(IAbc.class); 
factory.setAddress("https://hiddendomain.com/abc/abc.svc/soap"); <- must be /soap there, otherwise 404 

IAbc info = (IAbc) factory.create(); 

Client client = ClientProxy.getClient(info); 
HTTPConduit http = (HTTPConduit) client.getConduit(); 

http.getAuthorization().setUserName("user"); 
http.getAuthorization().setPassword("pass"); 

String abc = info.abc(); 
+1

Gracias, que resolvieron mi problema también. Al principio estaba usando un servicio HTTP, pero después de cambiar a HTTPS obtuve la temida excepción PolicyException. Inicialmente hice 'MyService ss = new MyService (nueva URL (wsdlUrl), nueva QName (" http://tempuri.org/ "," MyService ")); IMyService port = ss.getBasicHttpBindingIMyService(); '. Ahora hago 'JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean(); factory.setServiceClass (IMyService.class); factory.setAddress (wsdlUrl.replace ("? wsdl", "")); IMyService port = (IMyService) factory.create(); '. – neu242

+0

¡Esto fue realmente útil! ¡¡Gracias!! –

+0

Tengo el mismo problema ... mientras el código estaba funcionando en Tomcat, pero cuando implemente el código en jboss recibo este error. puede por favor compartir el código completo del programa del cliente. – aravind

1

Usted es impresionante. esto resolvió mi problema de wsse: problema de seguridad de políticas en mi wsdl. Ahora puedo llamar al servicio seguro usando CXF.

tengo que añadir mi código siguiente en

JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean(); 
    factory.setServiceClass(TicketServicePortType.class); 
    factory.setAddress("http://localhost:8090/services"); 
    TicketServicePortType port = (TicketServicePortType) factory.create(); 

    Client client = ClientProxy.getClient(port); 
    HTTPConduit http = (HTTPConduit) client.getConduit(); 

    http.getAuthorization().setUserName("user"); 
    http.getAuthorization().setPassword("password"); 


    Endpoint cxfEndpoint = client.getEndpoint(); 

    Map<String,Object> outProps = new HashMap<String,Object>(); 

    outProps.put(WSHandlerConstants.ACTION, WSHandlerConstants.USERNAME_TOKEN); 
    outProps.put(WSHandlerConstants.USER, "user"); 
    outProps.put(WSHandlerConstants.PASSWORD_TYPE, WSConstants.PW_TEXT); 
    outProps.put(WSHandlerConstants.PW_CALLBACK_CLASS, 
    ClientPasswordCallback.class.getName()); 

    WSS4JOutInterceptor wssOut = new WSS4JOutInterceptor(outProps); 
    cxfEndpoint.getOutInterceptors().add(wssOut); 
+0

1) es mejor usar outProps.put (WSHandlerConstants.PW_CALLBACK_REF, new ClientPasswordCallback()); 2) la inicialización de nombre de usuario/contraseña en http.getAuthorization() parece ser inútil ... – ursa

1

Para mí la eliminación cxf-Bundle del proyecto ayudó de inmediato:

<dependency> 
    <groupId>org.apache.cxf</groupId> 
    <artifactId>cxf-bundle</artifactId> 
    <version>2.7.17</version> 
</dependency> 
Cuestiones relacionadas