用openssl创建自认证证书
我是按照这个博客来创建的自认证证书
openssl自认证证书创建
通过openssl指令来生成证书
#生成秘钥
openssl genrsa -out private.key 2048
#生成用于申请请求的证书文件csr,一般会将该文件发送给CA机构进行认证,本例使用自签名证书
openssl req -new -key private.key -out request.csr
#自签名证书
openssl x509 -req -days 365 -in request.csr -signkey private.key -out root.crt
#生成p12文件
openssl pkcs12 -export -out cert.p12 -inkey private.key -in root.crt
#生成p12时记录住密码
#p12文件转换为keystore文件
keytool -importkeystore -srckeystore cert.p12 -destkeystore my.keystore -deststoretype pkcs12
#p12转换为jks
keytool -importkeystore -srckeystore cert.p12 -destkeystore dest.jks -deststoretype pkcs12
#查看证书信息
openssl x509 -noout -text -in root.crt
#从私钥中解析出公钥
openssl rsa -in private.key -pubout -out public.pem
#从JSK文件中提取公钥
keytool -list -rfc -keystore demo.jks -storepass password
将得到的证书保存起来就是公钥
或者通过jdk自带的工具生成证书
${jdkPath}/bin> keytool -genkey -alias client.keystore -keyalg RSA -validity 20000 -keystore xxx.keystore
在springboot的配置文件里添加证书及其他配置信息
此处采用的是yml配置,要求和http兼容
server:
#https监听端口
port: 8089
ssl:
key-store: classpath:xxx.p12
key-store-password: #申请证书时自己设置的密码
key-store-type: PKCS12
key-alias: #证书别名
enabled: true
http:
port: 8081
可能也会用到的properties配置
server.port=8088
server.ssl.key-store=classpath:xxx.p12 #路径在resources底下
server.ssl.key-alias=
server.ssl.enabled=true
server.ssl.key-store-type= PKCS12
server.ssl.key-store-password= #申请证书时自己设置的密码
这里发生了http和https共同占用一个端口产生的冲突
必须加上字段 enabled: true
必须通过实现一个接口改变容器得端口配置此处还与springboot的版本有关
这个是springboot 1.5.x 版本的配置
package com.woniu.learnhttps.config;
import org.apache.catalina.connector.Connector;
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MultiConnectionSupport {
@Value("