如何使用自签名证书进行https请求

本文涉及的产品
云解析 DNS,旗舰版 1个月
全局流量管理 GTM,标准版 1个月
公共DNS(含HTTPDNS解析),每月1000万次HTTP解析
简介: 如何使用自签名证书进行https请求

1.生成自签名证书

  1. 生成私钥
openssl genrsa -out private.key
  1. 生成自签名证书
openssl req -new -key private.key -out ca.csr
openssl x509 -req -days 365 -in ca.csr -signkey private.key -out ca.pem

365为天数

  1. 第一条命令只需填Common Name(域名), 如下:
❯ openssl req -new -key private.key -out ca.csr
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) []:
State or Province Name (full name) []:
Locality Name (eg, city) []:
Organization Name (eg, company) []:
Organizational Unit Name (eg, section) []:
Common Name (eg, fully qualified host name) []:*.kq-qzj.cn
Email Address []:
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:

2.配置nginx

将生成的private.keyca.pem放入/etc/nginx/cert

配置nginx

server {
    listen 30443 ssl;
    server_name *.kq-qzj.cn;
    ssl_certificate  /etc/nginx/cert/ca.pem;
    ssl_certificate_key /etc/nginx/cert/private.key;
    ssl_session_timeout 5m;
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;
    location / {
      root /etc/nginx/conf.d;
    }
}

3. Java客户端跳过SSL和配置DNS解析

自定义DNS解析

import org.apache.http.conn.DnsResolver;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
 * @author Zijian Liao
 * @since 2.9.0
 */
@Component
public class HisResolver implements DnsResolver, InitializingBean {
    private Map<String, InetAddress[]> dnsMap = new ConcurrentHashMap<>();
    @Override
    public InetAddress[] resolve(String host) throws UnknownHostException {
        final InetAddress[] resolvedAddresses = dnsMap.get(host);
        if(resolvedAddresses != null){
            return resolvedAddresses;
        }
        return InetAddress.getAllByName(host);
    }
    // 初始化
    @Override
    public void afterPropertiesSet() throws Exception {
        this.refreshAddress();
    }
    // 定时刷新
    @Scheduled(fixedRateString = "PT5M")
    public void refreshTask() throws UnknownHostException {
        this.refreshAddress();
    }
    private void refreshAddress() throws UnknownHostException {
        // 模拟查询数据库
        Map<String, InetAddress[]> dnsMap = new ConcurrentHashMap<>();
        InetAddress[] inetAddress = InetAddress.getAllByName("127.0.0.1");
        dnsMap.put("a.kqqzj.cn", inetAddress);
        dnsMap.put("b.kqqzj.cn", inetAddress);
        dnsMap.put("c.kqqzj.cn", inetAddress);
        dnsMap.put("d.kqqzj.cn", inetAddress);
        this.dnsMap = dnsMap;
    }
}

跳过SSL

import lombok.extern.slf4j.Slf4j;
import org.apache.http.client.HttpClient;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.ssl.SSLContexts;
import org.apache.http.ssl.TrustStrategy;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
import javax.net.ssl.SSLContext;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
/**
 * @author Zijian Liao
 * @since 2.9.0
 */
@Slf4j
@SpringBootTest
public class RestTemplateTest {
    @Resource
    private HisResolver hisResolver;
    private RestTemplate restTemplate;
    @BeforeEach
    public void init() throws NoSuchAlgorithmException, KeyStoreException, KeyManagementException {
        // SSL
        TrustStrategy acceptingTrustStrategy = (x509Certificates, authType) -> true;
        SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(null, acceptingTrustStrategy).build();
        SSLConnectionSocketFactory connectionSocketFactory = new SSLConnectionSocketFactory(sslContext,
                new NoopHostnameVerifier());
        HttpClient httpClient = HttpClientBuilder.create()
                .setDnsResolver(hisResolver)
                .setSSLSocketFactory(connectionSocketFactory)
                .build();
        ClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(httpClient);
        this.restTemplate = new RestTemplate(requestFactory);
    }
    @Test
    public void testSsl(){
        System.out.println(restTemplate.getForObject("https://a.kq-qzj.cn:30443/a.html", String.class));
        System.out.println(restTemplate.getForObject("https://b.kq-qzj.cn:30443/a.html", String.class));
        System.out.println(restTemplate.getForObject("https://c.kq-qzj.cn:30443/a.html", String.class));
        System.out.println(restTemplate.getForObject("https://d.kq-qzj.cn:30443/a.html", String.class));
        System.out.println(restTemplate.getForObject("http://127.0.0.1:10000/a.html", String.class));
    }
}
目录
相关文章
|
1月前
|
Linux Docker Windows
Docker配置https证书案例
本文介绍了如何为Docker的Harbor服务配置HTTPS证书,包括安装Docker和Harbor、修改配置文件以使用证书、生成自签名证书、配置证书以及验证配置的步骤。
35 2
Docker配置https证书案例
|
2月前
|
安全 Apache Windows
WAMP——配置HTTPS证书
WAMP——配置HTTPS证书
55 1
WAMP——配置HTTPS证书
|
2月前
|
安全 网络安全 Windows
【Azure App Service】遇见az命令访问HTTPS App Service 时遇见SSL证书问题,暂时跳过证书检查的办法
【Azure App Service】遇见az命令访问HTTPS App Service 时遇见SSL证书问题,暂时跳过证书检查的办法
【Azure App Service】遇见az命令访问HTTPS App Service 时遇见SSL证书问题,暂时跳过证书检查的办法
|
2月前
|
Web App开发
Chrome浏览器导出HTTPS证书
Chrome浏览器导出HTTPS证书
43 0
Chrome浏览器导出HTTPS证书
|
2月前
|
网络协议 安全 网络安全
免费申请 HTTPS 证书的八大方法
免费申请 HTTPS 证书的八大方法
|
3月前
|
安全 Java 网络安全
RestTemplate进行https请求时适配信任证书
RestTemplate进行https请求时适配信任证书
56 3
|
2月前
|
JavaScript 前端开发 Java
【Azure 环境】各种语言版本或命令,发送HTTP/HTTPS的请求合集
【Azure 环境】各种语言版本或命令,发送HTTP/HTTPS的请求合集
|
4月前
|
前端开发 小程序 应用服务中间件
在服务器上正确配置域名https证书(ssl)及为什么不推荐使用宝塔申请免费ssl证书
在服务器上正确配置域名https证书(ssl)及为什么不推荐使用宝塔申请免费ssl证书
225 4
|
3月前
|
数据安全/隐私保护
https【详解】与http的区别,对称加密,非对称加密,证书,解析流程图
https【详解】与http的区别,对称加密,非对称加密,证书,解析流程图
65 0
|
JavaScript 前端开发
下一篇
无影云桌面