springboot集成https

1 生成秘钥

1.1 用管理员cmd 进入jdk bin 目录 C:\Program Files\Java\jdk1.8.0_301\bin

.\keytool.exe -genkeypair -alias test-https -keypass 123456 -keyalg RSA -keysize 1024 -validity 365 -keystore d:\cer2\test.keystore -storepass 123456

1.2 把生成的test.keystore文件复制到resources 目录下

2 服务端配置

server:

port: 8081

tomcat:

max-http-form-post-size: -1

ssl:

key-store: classpath:test.keystore

key-store-type: JKS

key-alias: test-https

key-store-password: 123456

key-password: 123456

3 客户端配置

把生成的test.keystore文件复制到resources 目录下

<dependency>

<groupId>org.apache.httpcomponents</groupId>

<artifactId>httpclient</artifactId>

</dependency>

4 restTemplate 配置

import org.apache.http.conn.ssl.NoopHostnameVerifier;

import org.apache.http.conn.ssl.SSLConnectionSocketFactory;

import org.apache.http.impl.client.HttpClients;

import org.apache.http.ssl.SSLContextBuilder;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

import org.springframework.core.io.ClassPathResource;

import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;

import org.springframework.web.client.RestTemplate;

import javax.net.ssl.SSLContext;

@Configuration

public class RestTemplateConfiguration {

@Bean

public RestTemplate restTemplate() throws Exception {

HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();

//超时配置

factory.setReadTimeout(10000);

factory.setConnectTimeout(3000);

//https

SSLContext sslContext = SSLContextBuilder.create()

.loadTrustMaterial(new ClassPathResource("test.keystore").getFile(), "123456".toCharArray())

.build();

//连接facotry

SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(sslContext, NoopHostnameVerifier.INSTANCE);

factory.setHttpClient(HttpClients.custom().setSSLSocketFactory(socketFactory).build());

return new RestTemplate(factory);

}

}

5 调用示范:

###get

public String getRequestHttps(String url) {

// 1.构建请求头

HttpHeaders headers = new HttpHeaders();

headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);

// 2. 执行请求

ResponseEntity<String> exchange = restTemplateHttps.exchange(

url,

HttpMethod.GET,

new HttpEntity<>(null, headers),

new ParameterizedTypeReference<String>() {

});

Assert.isTrue(exchange.getStatusCode().is2xxSuccessful(), "响应必须是 200 成功");

log.info("getRequest----->{}", exchange);

return exchange.getBody();

}

###post

public static String postJsonRequest(Map<String, Object> body, String url) {

// 1 构建请求头

HttpHeaders headers = new HttpHeaders();

headers.setContentType(MediaType.APPLICATION_JSON);

// 2. 执行请求

ResponseEntity<String> exchange = restTemplateHttp.exchange(

url,

HttpMethod.POST,

new HttpEntity<>(body, headers),

new ParameterizedTypeReference<String>() {

});

Assert.isTrue(exchange.getStatusCode().is2xxSuccessful(), "响应必须是 200 成功");

log.info("postJsonRequest----->{}", exchange);

return exchange.getBody();

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值