简体   繁体   中英

RESTEasy Client Proxy Preemptive Basic Authentication

I am using RESTEasy Proxy Framework to call my Rest-Services. I would like to use preemptive authentication with the proxy framework.

Thats my current Code:

public void callSomeService() throws Exception {

    RegisterBuiltin.register(ResteasyProviderFactory.getInstance());

    DefaultHttpClient client = new DefaultHttpClient();
    UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(
            USERNAME, PASSWORD);
    AuthScope authscope = new AuthScope(AuthScope.ANY_HOST,
            AuthScope.ANY_PORT, AuthScope.ANY_REALM);
    client.getCredentialsProvider().setCredentials(authscope, credentials);
    ApacheHttpClient4Executor executer = new ApacheHttpClient4Executor(client);
    dummyResource = ProxyFactory.create(DummyResource.class,
            "http://localhost:8888/myapp/rest/", executer);

    // Do some calls here       
}

When I monitor the traffic of my application, the Rest-Service gets called twice:

  1. First the client receives an 401 Error (UNAUTHORIZED)
  2. In the second request there is the Authorization Header added and everything works fine.

What I actually want to do is that the Authorization Header is already added in the first request! How can I do that?

I am using RESTEasy 2.3.5! I also read the documentation ( http://docs.jboss.org/resteasy/docs/2.3.5.Final/userguide/html_single/index.html#transport_layer ) where is an example given for preemptive authentication, which actually doesnt work, because of this code:

BasicScheme basicAuth = new BasicScheme();
authCache.put("com.bluemonkeydiamond.sippycups", basicAuth);

You're right, the example in the documentation does not compile. Try replacing the string "com.bluemonkeydiamond.sippycups" with an instance of HttpHost. The HttpHost class has several constructors so be sure to look at the JavaDocs. The simplest constructor takes a string. For example,

BasicScheme basicAuth = new BasicScheme();
authCache.put(new HttpHost("com.bluemonkeydiamond.sippycups"), basicAuth);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM