RestTemplate同时支持发送https和http请求,最简洁

1 篇文章 0 订阅
1 篇文章 0 订阅

        springboot集成了优雅的RestTemplate,可以替代以前阿帕奇或者okhttp的一些http通信组件,通过注入的方式在系统内优雅的引用。

        随着各大网站门户安全意识的提高,https的使用也越来越多,开发时免不了要配置RestTemplate等工具支持https请求的发出,最常见的做法就是发送时,构建sslcontext,忽略证书认证,信任所有。在配置RestTemplate时,考虑兼容性,必须一步到位,同时支持两种方式。

最简洁的方式具体如下:

1.maven引入核心的依赖,其他lombok和spring常用的依赖在此不再赘述

 <dependency>
                <groupId>org.apache.httpcomponents</groupId>
                <artifactId>httpclient</artifactId>
                <version>4.5.3</version>
</dependency>

2.继承SimpleClientHttpRequestFactory 实现自己需要的https忽略证书逻辑

import org.apache.http.conn.ssl.SSLContexts;
import org.apache.http.conn.ssl.TrustStrategy;
import org.springframework.http.client.SimpleClientHttpRequestFactory;

import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import java.net.HttpURLConnection;
import java.security.KeyStore;

/**
 * 继承SimpleClientHttpRequestFactory 根据连接支持实现http和https请求发送
 */
public class HttpsClientRequestFactory extends SimpleClientHttpRequestFactory {
    @Override
    protected void prepareConnection(HttpURLConnection connection, String httpMethod) {
        try {
            if (!(connection instanceof HttpsURLConnection)) {
                //http协议
                super.prepareConnection(connection, httpMethod);
            }
            if ((connection instanceof HttpsURLConnection)) {
                HttpsURLConnection httpsURLConnection= (HttpsURLConnection) connection;
                //https协议
                KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
                //信任任何链接(也就是忽略认证)
                TrustStrategy anyTrustStrategy = (chain, authType) -> true;
                SSLContext ctx = SSLContexts.custom().loadTrustMaterial(trustStore, anyTrustStrategy).build();
                httpsURLConnection.setSSLSocketFactory(ctx.getSocketFactory());
                super.prepareConnection(httpsURLConnection, httpMethod);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

3.配置RestTemplate的启动配置类RestTemplateConfig,直接使用新的facatory

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;


@Configuration
@Slf4j
public class RestTemplateConfig {

    @Bean
    public RestTemplate restTemplate(){
        HttpsClientRequestFactory factory = new HttpsClientRequestFactory();
        //单位为ms (部分接口数据量大,读取改为60秒)
        factory.setReadTimeout(60000);
        //单位为ms
        factory.setConnectTimeout(10000);
        return new RestTemplate (factory);
    }
}

  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值