5

Come posso accedere a un servizio Web tramite un'autenticazione HTTP di base? Sto usando i netbeans integrati nelle funzionalità client di webservice. Ma quando provo ad accedere al servizio web, ottengo un'eccezione con un messaggio di errore fallito dell'autorizzazione 401.Netbeans Basic Http Auth Jax-WS

Come posso passare il nome utente e la password corretti?

Grazie!

risposta

4

È possibile utilizzare la classe BindingProvider o WSBindingProvider per accedere a un servizio Web tramite un'autenticazione HTTP di base. Il codice è il seguente.

XxxService service = new XxxService(); 
Xxx port = service.getXxxPort(); 

Map<String, Object> reqContext = ((BindingProvider)port).getRequestContext(); 
reqContext.put(BindingProvider.USERNAME_PROPERTY, "username"); 
reqContext.put(BindingProvider.PASSWORD_PROPERTY, "password"); 
+2

Questo funzionerà se il WSDL non è protetto da Autenticazione Http di base – snowflake

+0

perché non funziona per me? Ho esattamente quel 'AImpl aImplPort = new AImplService(). GetAImplPort(); BindingProvider prov = (BindingProvider) aImplPort; prov.getRequestContext(). Put (BindingProvider.USERNAME_PROPERTY, "fred"); prov.getRequestContext(). Put (BindingProvider.PASSWORD_PROPERTY, "fred"); Stringa b = aImplPort.b(); System.out.println (b); 'non invia l'header HTTP di autorizzazione previsto –

3

È anche possibile fornire il proprio Authenticator. In questo modo funzionerà anche se il WDSL stesso è protetto dall'autenticazione HTTP di base.

@WebServiceRef(wsdlLocation = "https://laka/sito?wsdl") 
static XxxService service; 

public static void main(String[] args) { 

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

    service = new XxxService(); 
    Xxx port = service.getXxxPort(); 

    // invoke webservice and print response 
    XxxResponse resp = port.foo(); 
    System.out.println(resp.toString()); 

} 
+0

Non sapeva che java contiene questa funzionalità. Molto utile. Grazie! – gruenewa

+0

perché funziona per me e il 'reqContext.put (BindingProvider.USERNAME_PROPERTY," username "); L'approccio non è correlato alla mia versione java? –

Problemi correlati