Search code examples
javaresthttpclientshiro

Apache Shiro authcBasic authentication using Java and apache HttpClient


My REST application uses Shiro basic authentication to secure the REST endpoints and its working great when tested from the Browser.

Now I want to be able to login into the application from a java client using Apache HttpClient

Any ideas?

Thank You


Solution

  • This worked for me

            DefaultHttpClient httpclient = new DefaultHttpClient();
        try {
    
            httpclient.getCredentialsProvider().setCredentials(
                new AuthScope("localhost", 9009),
                new UsernamePasswordCredentials("username", "password*"));
    
            HttpGet httpget = new HttpGet("http://localhost:9009/path/list");
    
            ResponseHandler<String> responseHandler = new BasicResponseHandler();
            System.out.println("executing request" + httpget.getRequestLine());
    
            String responseBody = httpclient.execute(httpget, responseHandler);
            System.out.println("----------------------------------------");
            System.out.println(responseBody);
            System.out.println("----------------------------------------");
            System.out.println("Job Done!");
        } catch (Exception ex) {
            Logger.getLogger(Command.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            httpclient.getConnectionManager().shutdown();
        }
    }