业务中,会访问到需要安全认证的服务。如

restemplate发送安全认证的http请求

发送请求时,需要把resttemplate设置头信息,具体实现如下

        String url = "http://localhost:8080/testController";
        HttpHeaders header = new HttpHeaders();
        //输入自己的用户名和密码
        String userAndPass = "username:passworld";
        //Basic后有空格
        //Base64需要maven引入commons-codec
        header.add("Authorization", "Basic "+Base64.encodeBase64String(userAndPass.getBytes()));
        HttpEntity<String> entity = new HttpEntity<>(header);
        ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, entity, String.class);
        String sttr = response.getBody();

 

 

相关文章:

  • 2021-06-30
  • 2021-09-08
  • 2021-07-11
  • 2021-09-22
  • 2022-02-12
  • 2021-12-07
猜你喜欢
  • 2021-06-17
  • 2021-05-19
  • 2022-12-23
  • 2021-08-09
  • 2022-01-14
  • 2021-09-18
  • 2021-09-10
相关资源
相似解决方案