Jersey Client API - authentication

后端 未结 7 2109
不知归路
不知归路 2020-11-30 23:14

I\'m using the Jersey client API to submit SOAP requests to a JAX-WS webservice. By default Jersey is somehow using my Windows Nt credentials for authentication when challen

7条回答
  •  佛祖请我去吃肉
    2020-11-30 23:55

    Adding this answer as I keep finding answers for older versions of Jersey that are no longer relevant in 2.x.

    For Jersey 2 there are several ways. Take a look at:

    JavaDoc for org.glassfish.jersey.client.authentication.HttpAuthenticationFeature

    Here is the one that is working for me (simplest basic auth IMHO).

        ClientConfig config = new ClientConfig();
    
        HttpAuthenticationFeature feature = HttpAuthenticationFeature.basic("username", "password");
    
        Client client = ClientBuilder.newClient(config);
        client.register(feature);
    
        WebTarget webTarget = client.target("http://api.asite.com/api").path("v1/reports/list");
        Invocation.Builder invocationBuilder =  webTarget.request(MediaType.TEXT_PLAIN_TYPE);
    
        Response response = invocationBuilder.get();
    
        System.out.println( response.getStatus() );
        System.out.println( response.readEntity(String.class) );
    

提交回复
热议问题