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

本文涉及的产品
公共DNS(含HTTPDNS解析),每月1000万次HTTP解析
云解析 DNS,旗舰版 1个月
全局流量管理 GTM,标准版 1个月
简介: 如何使用自签名证书进行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));
    }
}
目录
相关文章
|
5天前
|
安全 API 网络安全
使用OkHttp进行HTTPS请求的Kotlin实现
使用OkHttp进行HTTPS请求的Kotlin实现
|
17天前
|
安全 网络安全 数据安全/隐私保护
政务单位IP地址https证书
政务单位IP地址HTTPS证书是一种专为只有IP地址而无域名的政务网站设计的数字证书,用于加密通信、确保数据安全并提升用户信任度。申请流程包括选择证书颁发机构、提交申请并验证、部署证书等步骤。证书有效期通常为一年或多年,需定期更新以确保安全性。
|
1月前
|
存储 网络安全 对象存储
缺乏中间证书导致通过HTTPS协议访问OSS异常
【10月更文挑战第4天】缺乏中间证书导致通过HTTPS协议访问OSS异常
87 4
|
1月前
|
安全 算法 量子技术
【HTTPS】中间人攻击和证书的验证
【HTTPS】中间人攻击和证书的验证
51 1
|
1月前
|
存储 缓存 安全
https访问提示不安全,证书密钥验证上如何解决
【10月更文挑战第4天】访问提示不安全,证书密钥验证上如何解决
263 2
|
2月前
|
Linux Docker Windows
Docker配置https证书案例
本文介绍了如何为Docker的Harbor服务配置HTTPS证书,包括安装Docker和Harbor、修改配置文件以使用证书、生成自签名证书、配置证书以及验证配置的步骤。
167 2
Docker配置https证书案例
|
1月前
|
安全 网络安全 数据安全/隐私保护
HTTPS 请求中的证书验证详解(Python版)
HTTPS 请求中的证书验证详解(Python版)
104 0
|
3月前
|
安全 网络安全 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证书问题,暂时跳过证书检查的办法
|
3月前
|
JavaScript 前端开发 Java
【Azure 环境】各种语言版本或命令,发送HTTP/HTTPS的请求合集
【Azure 环境】各种语言版本或命令,发送HTTP/HTTPS的请求合集
|
2月前
|
监控 安全 搜索推荐
设置 HTTPS 协议以确保数据传输的安全性
设置 HTTPS 协议以确保数据传输的安全性