简体   繁体   中英

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.

我认为您不应该使用basicAuth,因为它用于HTTP基本认证(RFC 2617),这与JHipster在您的案例登录/密码格式编码和会话中使用的方式不同。

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