Camunda Rest API with Authentication on

1,290 views
Skip to first unread message

suresh.t...@gmail.com

unread,
Feb 27, 2014, 12:25:09 AM2/27/14
to camunda-...@googlegroups.com
Hi,
I am able to call Rest API with out any Problem when user Authentication is off. I got Authentication required error when i removed comments related to authentication on in Rest-engine web.xml and below Client code i used.

DefaultHttpClient dhc = new DefaultHttpClient();

dhc.getCredentialsProvider().setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM), new UsernamePasswordCredentials("demo", "demo"));

AuthCache authCache = new BasicAuthCache();
// Generate BASIC scheme object and add it to the local auth cache
BasicScheme basicAuth = new BasicScheme();
authCache.put(new HttpHost("localhost", 8080, "http"), basicAuth);

// Add AuthCache to the execution context
BasicHttpContext localcontext = new BasicHttpContext();
localcontext.setAttribute(ClientContext.AUTH_CACHE, authCache);
HttpGet hp =new HttpGet("http://localhost:8080/engine-rest/engine/engine/process-definition");
try{
HttpResponse processResponse = dhc.execute(hp,localcontext);
BufferedReader br = new BufferedReader(
new InputStreamReader((processResponse.getEntity().getContent())));

String output;
System.out.println("Output from Server .... \n");
while ((output = br.readLine()) != null) {
System.out.println(output);
}

dhc.getConnectionManager().shutdown();
}
catch(Exception e){

e.printStackTrace();
}

Please provide me solution for this.

regards,
suresh

Christian Lipphardt

unread,
Feb 27, 2014, 5:19:57 AM2/27/14
to camunda-...@googlegroups.com, suresh.t...@gmail.com
Hi Suresh,

below code works with authentication. It is based on 

    <dependency>
      <groupId>org.apache.httpcomponents</groupId>
      <artifactId>httpclient</artifactId>
      <version>4.3.3</version>
    </dependency>

    CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
    credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("demo", "demo"));

    AuthCache authCache = new BasicAuthCache();
    // Generate BASIC scheme object and add it to the local auth cache
    authCache.put(new HttpHost("localhost", 8080, "http"), new BasicScheme());

    HttpClientContext context = HttpClientContext.create();
    context.setCredentialsProvider(credentialsProvider);
    context.setAuthCache(authCache);

    HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
    HttpClient client = httpClientBuilder.build();


    try{
      HttpResponse processResponse = client.execute(hp, context);
      BufferedReader br = new BufferedReader(
          new InputStreamReader((processResponse.getEntity().getContent())));

      String output;
      System.out.println("Output from Server .... \n");
      while ((output = br.readLine()) != null) {
        System.out.println(output);
      }
    }
    catch(Exception e){
      e.printStackTrace();
    }

Cheers,
Christian

suresh.t...@gmail.com

unread,
Feb 27, 2014, 5:54:32 AM2/27/14
to camunda-...@googlegroups.com, suresh.t...@gmail.com
Hi Chris,
Thank you for providing Information. I tested with below code and now i am able to Authenticate. If password is wrong than i am getting error message with big html data. Instead of this is there anyway to return json data with proper error when user name or password wrong.

regards,
suresh

Reply all
Reply to author
Forward
0 new messages