Search code examples
javajax-rsresteasysquid

Jax-RS unable to authenticate to proxy


My company has a proxy

proxy=myProxy
port=myProxyPort
username=me
password=myPassword

I try to access the outside world by using simple java.net functions and it worked!

System.setProperty("http.proxyHost", myProxy);
System.setProperty("http.proxyPort", myProxyPort);

Authenticator.setDefault(new Authenticator() {
      protected PasswordAuthentication getPasswordAuthentication() {
        return new
           PasswordAuthentication(me,myPasssword.toCharArray());
    }});

URL u = new URL("http://www.google.com");
HttpURLConnection con = (HttpURLConnection) u.openConnection();
DataInputStream di = new DataInputStream(con.getInputStream());
byte[] b = new byte[1024*512];
while(-1 != di.read(b,0,1024*512)) {
   System.out.print(new String(b));

Not I try to do it by using Jax-RS Resteasy Implementation like that:

Client client = new ResteasyClientBuilder().defaultProxy(myProxy, myProxyPort).build(); 
System.out.println(client.target("https://www.google.com").request().get().readEntity(String.class));

I got the following error

Cache Access Denied
ERR_CACHE_ACCESS_DENIED
(squid/3.1.6)
Sorry, you are not currently allowed to request https://www.google.com/* from this cache until you have authenticated yourself

Can somebody tell me how to authenticate to the proxy with username-password using Jax-RS


Solution

  • Wow, It really make me crazy this problem. I don't know how to solve it in an implementation independent way. Anyway, by now it worked like that:

    ResteasyClient client = new ResteasyClientBuilder().defaultProxy(myProxy, myProxyPort).build(); 
    Credentials credentials = new UsernamePasswordCredentials(me, mypassword);
    ApacheHttpClient4Engine engine = (ApacheHttpClient4Engine)client.httpEngine();
    HttpContext context =  new BasicHttpContext();
    engine.setHttpContext(context);
    
    CredentialsProvider provider = new BasicCredentialsProvider(); 
    context.setAttribute(ClientContext.CREDS_PROVIDER, provider);
    AuthScope authScope = new AuthScope(
                myProxy,
                myProxyPort,
                null,
                null);
    System.out.println(client.target("https://www.google.com").request().get().readEntity(String.class));