JAVA使用RestTemplate访问URL带用户名密码验证的Http请求

问题

项目中需要对数据进行同步,在代码中通过配置url,然后使用RestTemplate发起http请求,其中url携带了用户名与密码,如下:

http://cluster:cluster@192.168.61.30:18011/kettle/executeTrans/?rep=resource&trans=drug

原代码如下:

 if (hospitalSyncEnabled) {
     String url = hospitalSyncUrl + hospitalBizResponse.getId();
     String response = restTemplate.getForObject(url, String.class);
     log.info("Call Kettle Response : {}", response);
 }

结果:请求报错:“401 Unauthorized…”

分析

根据报错结果可以推测请求失败是因为该http请求在目的主机鉴权失败。因此问题出在:把账号:密码直接拼接在url中,然后通过restTemplate.getForObject进行请求导致的。

接下来尝试将账号:密码信息设置在http的headers中:

  if (hospitalSyncEnabled) {
      String url = hospitalSyncUrl + hospitalBizResponse.getId();
      HttpHeaders headers = new HttpHeaders();
      String encodeAuthString = Base64.getEncoder().encodeToString(authString.getBytes());
      headers.set("Authorization", "Basic " + encodeAuthString);

      HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<>(null,headers);
      String response = restTemplate.exchange(url, HttpMethod.GET, httpEntity, String.class).getBody();
      log.info("Call Kettle Response : {}", response);
  }

结果

在对代码修改后,再次调用接口,访问成功。

原因探索

通过debug,可以发现,restTemplate.getForObject进行请求时,携带的账号:密码信息直接拼接在url中,但是由于安全性问题,现在很多系统已经不支持该方式,因此导致鉴权失败;

然而,当我们将账号:密码信息设置在header后,并通过exchange方法进行http请求,最终在创建连接时,其携带了账号:密码信息,因此鉴权成功。
在这里插入图片描述

  • 9
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Java 中,可以使用 `RestTemplate` 类来访问 URL 并获取 Cookie。以下是一个示例代码: ```java import org.springframework.http.*; import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; import org.springframework.web.client.RestTemplate; public class Main { public static void main(String[] args) { String username = "your_username"; String password = "your_password"; String url = "http://example.com/login"; RestTemplate restTemplate = new RestTemplate(); HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); MultiValueMap<String, String> map = new LinkedMultiValueMap<>(); map.add("username", username); map.add("password", password); HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(map, headers); ResponseEntity<String> response = restTemplate.postForEntity(url, request, String.class); HttpHeaders responseHeaders = response.getHeaders(); List<String> cookies = responseHeaders.get("Set-Cookie"); if (cookies != null) { for (String cookie : cookies) { System.out.println("Cookie: " + cookie); } } } } ``` 在该示例中,我们首先创建一个 `RestTemplate` 对象,并设置请求头为 `application/x-www-form-urlencoded`。然后我们创建一个 `MultiValueMap` 对象,将用户名密码写入其中,创建一个 `HttpEntity` 对象,并使用 `postForEntity` 方法发送请求。最后,我们读取服务器的响应头,并获取返回的 Cookie。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Honey Ro

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值