user2509940
user2509940

Reputation: 43

How to access Spring REST API in JHipster with standalone

i need to call de jhipster rest service in a java sheduling standalone. but i dont know how, I try with HttpClient libraries and use CredentialsProvider to set de username and password I cant login use this

HttpGet httpget = new HttpGet("http://localhost:8080/#Login");

but when I try to get de rest jason api i get HTTP 401 Unauthorized

I see de Gatlin Test make in scala and its like there are simulating a web-browser.

So I am stacking here, and I will apreciate anybody that can give me some suggest in how to do these.

These is the code

    HttpHost targetHost = new HttpHost("localhost", 8080, "http");
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(AuthScope.ANY,
  new UsernamePasswordCredentials(DEFAULT_USER,DEFAULT_PASS));

AuthCache authCache = new BasicAuthCache();
authCache.put(targetHost, new BasicScheme());

final HttpClientContext context = HttpClientContext.create();
context.setCredentialsProvider(credsProvider);
context.setAuthCache(authCache);

 client = HttpClientBuilder.create().build();
response = client.execute(new HttpGet(URL_SECURED_BY_BASIC_AUTHENTICATION), context);

int statusCode = response.getStatusLine().getStatusCode();
System.out.println("Estatus Codee : " +statusCode);

String output;

In this call a have de estatus 401

response = client.execute(new HttpGet(URL_PROMOTORES), context);

 statusCode = response.getStatusLine().getStatusCode();
System.out.println("Estatus Codee : " +statusCode);

BufferedReader br = new BufferedReader(
  new InputStreamReader((response.getEntity().getContent())));


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

Thanks in advance.

Upvotes: 1

Views: 549

Answers (1)

Gaël Marziou
Gaël Marziou

Reputation: 16294

I don't think you should use basicAuth because it is for HTTP basic authentication (RFC 2617) which is different from what JHipster uses in your case login/password form encoded and session.

Upvotes: 2

Related Questions