RestTemplate通过HTTPS协议访问接口

这篇具有很好参考价值的文章主要介绍了RestTemplate通过HTTPS协议访问接口。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

RestTemplate通过HTTPS协议访问接口

RestTemplate 默认不支持https协议,需要支持有两种方式,第一种是忽略认证,第二种是导入证书(比第一种安全)

在这里只实现第一种方式,实现代码如下文章来源地址https://www.toymoban.com/news/detail-515777.html

package com.muge.other.controller;

import com.alibaba.fastjson.JSONObject;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContexts;
import org.apache.http.ssl.TrustStrategy;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;

import javax.net.ssl.SSLContext;

@Controller
@RequestMapping("/rest")
public class RestTemplateTest {
    
    @RequestMapping(value = "/test.info",method = RequestMethod.GET)
    @ResponseBody
    public void test(){
        RestTemplate restTemplateHttps= restTemplateHttps();
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Type", "application/json;charset=UTF-8");

        /*MultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
        map.add("userName", "admin");
        map.add("password", "123456");
        HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<>(map,headers);
        String resp =restTemplateHttps.postForObject("https://192.168.2.102:18531/loginInfo/login/v1.0", httpEntity, String.class);
        System.out.println(resp);*/
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("userName","admin");
        jsonObject.put("password","123456");

        JSONObject jsonObject1 = restTemplateHttps.postForObject("https://192.168.2.102:18531/loginInfo/login/v1.0",new HttpEntity<>(jsonObject,headers),JSONObject.class);
        System.out.println(jsonObject1);
    }

    public RestTemplate restTemplateHttps()  {
        RestTemplate restTemplate = null;
        try {
            TrustStrategy acceptingTrustStrategy = (chain, authType) -> true;
            SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(null, acceptingTrustStrategy).build();
            SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext, NoopHostnameVerifier.INSTANCE);

            HttpClientBuilder clientBuilder = HttpClients.custom();

            CloseableHttpClient httpClient = clientBuilder.setSSLSocketFactory(sslsf).build();

            HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
            requestFactory.setHttpClient(httpClient);

            restTemplate = new RestTemplate(requestFactory);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return restTemplate;
    }
}

到了这里,关于RestTemplate通过HTTPS协议访问接口的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • RestTemplate HTTPS请求忽略SSL证书

    使用RestTemplate发送HTTPS请求的时候,出现了这样的一个问题: RestTemplate 默认不支持https协议 解决方案:         第一种是忽略认证         第二种是导入证书,比较复杂(比第一种安全)  这里说一下第一种解决方案,忽略认证 版本:Spring Boot2.x RestTemplateConfig 测试代

    2024年02月10日
    浏览(41)
  • 解锁新技能RestTemplate设置全局、单个请求超时时间及支持https请求

    springboot请求第三方接口时会用到RestTemplate,其底层实现逻辑默认是通过SimpleClientHttpRequestFactory来实现,具体由socket连接来实现;可以替换其默认实现为HttpComponentsClientHttpRequestFactory。 一、自定义RestTemplate实例对象 二、RestTemplate自定义全局超时时间 三、RestTemplate设置单个请求

    2023年04月09日
    浏览(30)
  • RestTemplate发起HTTPS请求Unsupported or unrecognized SSL message 报错解决

    原因:RestTemplate默认是不支持HTTPS请求的,那么如果想使用RestTemplate发送一个HTTPS的请求 ,就需要对证书进行处理,这里记录一下一个可行的方法忽略证书的校验。 使用 RestTemplateBuilder 来构建一个 RestTemplate ,而非使用默认。 requestFactory() 方法用来设置 ClientHttpRequestFactory 。

    2024年02月11日
    浏览(32)
  • restTemplate发送https请求报错I/O error on POST request for “xxxx“: Remote host terminated the handshake解决

    最近在项目开发中遇到了一个问题,用restTemplate调用https接口的时候一直掉不通,报错I/O error on POST request for “xxxx”: Remote host terminated the handshake;nested exception is javax.net.ssl.SSLHandshakeException: Remote host terminated the handshake 远程主机终止了握手 一开始以为是SSL证书的问题。在百度

    2024年02月11日
    浏览(46)
  • springboot使用restTemplate调用webservice接口

    1.首先确定wsdl的http地址,使用postman测试接口是否成功  在浏览器输入webservice地址可以找到相应的请求和响应示例。    如果postman返回了正确的数据,就说明测试成功! 2.接下来代码:

    2024年01月16日
    浏览(52)
  • 微服务中RestTemplate访问其他服务返回值转换问题

    背景: 接收一个springcloud项目,UI模块访问其他服务的接口,返回数据统一都是使用fastjson进行转换,但是新开发了几个新模块之后发现fastjson很多bug(各种内存溢出),但是很多地方已经重度依赖fastjson,只是升级改掉了内存溢出的某些代码,最近突然想起来RestTemplate明明有

    2024年02月13日
    浏览(23)
  • Java工具类:使用RestTemplate请求WebService接口

    对接第三方提供的 WebService 接口,早期的调用方式过于复杂繁琐,所以使用 RestTemplate 进行调用 注:除了 RestTemplate 之外, HttpURLConnection 等也可以用来调用webservice接口 如果需要将xml转为Json,可参考:

    2024年01月22日
    浏览(48)
  • 内网可以通过https来访问,外网不可以通过https来访问,怎么办

    如果您的内网可以通过HTTPS来访问,但外网无法通过HTTPS进行访问,可能有几种原因导致这个问题: 防火墙配置:请确保您的防火墙正确配置,以允许外部访问您的服务器的HTTPS端口(通常为443端口)。检查防火墙规则并确保已配置允许传入和传出的HTTPS流量。 网络地址转换(

    2024年02月07日
    浏览(32)
  • restTemplate调用外部接口,调用返回307,用postman直接调用接口正常返回数据

    restTemplate调用外部接口,调用返回307,用postman直接调用接口正常返回数据 结论:调用的接口路径后加/ postman 关掉自动重定向(filesettins把Automatically follow redirects关掉),调用也同样返回307了 在网上各种搜307,出来的结果都是什么重定向 getHeaders().getLocation().toString() 获得返回

    2024年02月03日
    浏览(38)
  • Spring 教程—REST 客户端详解(WebClient 、RestTemplate、HTTP 接口)

    Spring框架为调用REST端点提供了以下选择: WebClient - 非阻塞、响应式客户端和 fluent API。 RestTemplate - 带有模板方法API的同步客户端。 HTTP 接口 - 注解式接口,并生成动态代理实现。 WebClient  是一个非阻塞的、响应式的客户端,用于执行HTTP请求。它在5.0中引入,提供了  Re

    2024年02月07日
    浏览(33)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包