Let’s Encrypt免费证书的申请

Let’s Encrypt免费证书的申请

Let’s Encrypt 的证书免费。支持通配符域名,唯一不好的地方就是证书有效期只有90天。需要主动更新。

申请证书

证书的申请我目前知道2种方式

自己手动申请

自己clone脚本 GitHub - certbot/certbot: Certbot is EFF's tool to obtain certs from Let's Encrypt and (optionally) auto-enable HTTPS on your server. It can also act as a client for any other CA that uses the ACME protocol. 执行
然后可以选择根据DNS或者http来验证域名的所有权,比较的麻烦

使用acme申请

这种方式非常的快捷,全程自动。而且还会自动帮你写入Linuxcrontab定时任务。会自动的更新证书。
唯一不好的是,需要把域名的DNS服务器设置为阿里云的

acme申请ssl证书

下载脚本并且执行

curl https://get.acme.sh | sh

source ~/.bashrc

获取阿里云后台的AccessKey和SecretKey

image

声明shell变量

使用阿里云的AccessKey和SecretKey

export Ali_Key="**********"
export Ali_Secret="************"

生成SSL证书

acme.sh --issue --dns dns_ali -d springboot.io -d *.springboot.io

springboot.io 换成自己的域名。

等待 120s,脚本会拿着你的key去调用阿里云的接口。自动设置dns信息来完成域名的验证。而且还会把你的key记录在shell脚本里(有一定的安全隐患),用于证书的更新

如果看到下图,表示证书申请成功

生成的证书目录在:~/.acme.sh/[域名]
image

Nginx配置证书

server {
	listen 443;
	server_name springboot.io www.springboot.io;
	ssl on;
	ssl_certificate /root/.acme.sh/springboot.io/fullchain.cer;
	ssl_certificate_key /root/.acme.sh/springboot.io/springboot.io.key;
    ....
}

SpringBoot配置证书

因为Servlet容器需要的证书格式不一样,需要通过 openssl 来转换证书

openssl安装

yum -y install openssl

生成 p12 文件

openssl pkcs12 -export -in fullchain.cer -inkey springboot.io.key -out springboot.io.p12

会要求输入一次密码。需要记着,执行成功会在当前目录生成一个springboot.io.p12的文件。

根据 p12 文件生成 keystore 文件

keytool -importkeystore -v  -srckeystore springboot.io.p12 -srcstoretype pkcs12 -srcstorepass 123456 -destkeystore springboot.io.keystore -deststoretype jks -deststorepass 123456
  • destkeystore 生成的keystore的文件名
  • -srcstorepass p12 文件的密码
  • -deststorepass keystore的密码
生成OK,但是有条警告信息,把里面的建议命名复制出来执行一遍就好

keytool -importkeystore -srckeystore springboot.io.keystore -destkeystore springboot.io.keystore -deststoretype pkcs12

这条警告不用管,只是提示信息,提示把原来的证书文件备份为了 .old 文件

复制证书到springboot项目

image

配置 application.yml

server:
  port: 443
  servlet:
    context-path: /
  ssl:
    enabled: true
    # keystore 文件
    key-store: classpath:ssl/springboot.io.keystore
    key-store-type: PKCS12
    # keystore的密码
    key-store-password: 123456

大功告成 :grin:

证书会在过期的前几天就自动更新(删除原文件,生成新文件),你需要再次的生成keystore文件更新到项目中

1 个赞