【问题标题】:How to access Spring REST API in JHipster with standalone如何在 JHipster 中独立访问 Spring REST API
【发布时间】:2015-08-18 20:21:32
【问题描述】:

我需要在独立的 java 调度中调用 de jhipster rest 服务。 但我不知道如何,我尝试使用 HttpClient 库并使用 CredentialsProvider 设置用户名和密码 我无法登录使用这个

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

但是当我尝试获取 de rest jason api 时,我得到 HTTP 401 Unauthorized

我在 scala 中看到了 de Gatlin Test make,它类似于模拟网络浏览器。

所以我在这里堆积,我会感谢任何可以给我一些建议的人。

这是代码

    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;

在此调用中,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();

提前致谢。

【问题讨论】:

    标签: rest apache-httpclient-4.x jhipster


    【解决方案1】:

    我认为您不应该使用 basicAuth,因为它用于 HTTP 基本身份验证 (RFC 2617),这与 JHipster 在您的登录/密码表单编码和会话中使用的不同。

    【讨论】:

      猜你喜欢
      • 2015-05-19
      • 2015-10-09
      • 2016-09-11
      • 1970-01-01
      • 1970-01-01
      • 2015-10-08
      • 2019-06-06
      • 1970-01-01
      • 2011-06-28
      相关资源
      最近更新 更多