HTTPS——Let's Encrypt申请免费的ssl证书

1,下载Let's Encrypt Windows认证客户端

http://files.cnblogs.com/files/teamblog/letsencrypt-win-simple.V1.9.1.zip

2,解压缩,打开letsencrypt.exe

3,设置提醒邮箱

http://images2015.cnblogs.com/blog/998529/201702/998529-20170207174641463-1130694755.png

4,Y同意条款

http://images2015.cnblogs.com/blog/998529/201702/998529-20170207174724729-1975993882.png

5,M方式认证

 http://images2015.cnblogs.com/blog/998529/201702/998529-20170207174757885-1494902777.png

6,在你想要上https的域名的后台文件里,加入对"/.well-known/acme-challenge/*"形式访问的处理


因为Let's Encrypt要验证你的域名是否属于你,会在你刚刚解压缩文件夹下生成一个验证文件,这里*是生成是随机字符

就比如:

http://images2015.cnblogs.com/blog/998529/201702/998529-20170207175223354-141299022.png

这是我服务器上解压路径(我给放到桌面了),验证时候(第七,第八步)会生成一个要验证域名的文件夹,里面存放着验证文件,如下图:

http://images2015.cnblogs.com/blog/998529/201702/998529-20170207175332932-85538970.png

SpringMVC形式验证的代码如下,其他方式自行实现:

@RequestMapping("/.well-known/acme-challenge/*")  
    public ResponseEntity<String> check(HttpServletRequest request, HttpServletResponse response){  
        HttpHeaders responseHeaders = new HttpHeaders();  
        responseHeaders.set("Content-Type", "application/json;charset=UTF-8");  
        String result="";  
        try {  
            String URI=request.getRequestURI().replace("/","\\");  
            //文件路径自行替换一下就行,就是上图中生成验证文件的路径,因为URI中已经包含了/.well-known/acme-challenge/,所以这里不需要  
            File file=new File("C:\\Users\\Administrator\\Desktop\\letsencrypt-win-simple.V1.9.1\\www.gutongxue.com\\"+URI);  
            InputStream is = new FileInputStream(file);  
            // 设置response参数,可以打开下载页面  
            response.reset();  
            response.setContentType("application/vnd.ms-excel;charset=utf-8");  
            response.setHeader("Content-Disposition", "attachment;filename="+ new String(("验证文件").getBytes(), "iso-8859-1"));  
            ServletOutputStream out = response.getOutputStream();  
            BufferedInputStream bis = null;  
            BufferedOutputStream bos = null;  
            try {  
                bis = new BufferedInputStream(is);  
                bos = new BufferedOutputStream(out);  
                byte[] buff = new byte[2048];  
                int bytesRead;  
                // Simple read/write loop.  
                while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {  
                    bos.write(buff, 0, bytesRead);  
                }  
            } catch (final IOException e) {  
                throw e;  
            } finally {  
                if (bis != null)  
                    bis.close();  
                if (bos != null)  
                    bos.close();  
            }  
        }catch (Exception e){  
  
        }  
        return new ResponseEntity<String>(result, responseHeaders, HttpStatus.OK);  
    }  

我已经写好的一个springMVC项目web 放到服务器上tomcatwebapps文件夹中启动tomcat
 

7,输入你想转为https的域名

8,验证文件的域名(如果用我的代码,就还输入你的域名就行,验证原理是访问你输入的值+/.well-known/acme-challenge/+生成的随机码)

http://images2015.cnblogs.com/blog/998529/201702/998529-20170207175757416-1720327317.png

 9,证书到这里就生成成功了

http://images2015.cnblogs.com/blog/998529/201702/998529-20170207180109651-823399148.png

10,由于Let's Encrypt是免费的SSL证书,90天就过期了,需要再次认证,贴心的Let's Encrypt客户端程序会自动帮你生成验证脚本,不要关闭窗口,继续往下走就行

11,确认帮你创建定时任务(不确定的话90天后SSL证书过期,就不是https)

http://images2015.cnblogs.com/blog/998529/201702/998529-20170207180258729-1855456807.png

12,输入该计算机(服务器)的管理员帐号密码

http://images2015.cnblogs.com/blog/998529/201702/998529-20170207180327822-1107163615.png

13,证书部分完成,可以关闭该窗口了

http://images2015.cnblogs.com/blog/998529/201702/998529-20170207180406510-601658188.png

14,找到生成的证书文件,默认路径在 C:\Users\Administrator\AppData\Roaming\letsencrypt-win-simple\httpsacme-v01.api.letsencrypt.org\ ,主要使用下图三个文件:

http://images2015.cnblogs.com/blog/998529/201702/998529-20170207180613932-977177363.png

15、将14步得到的文件(www.gutongxue.com-key.pemwww.gutongxue.com-crt.pem)通过https://www.myssl.cn/tools/merge-jks-cert.html   这个网站上的合成工具生成.jks文件并下载。

16、将如图代码取消注释

并在相应位置添加keystoreFile="jks文件地址" keystorePass="证书的密码",如图:

 

17,修改tomcatconf目录下web.xml文件:

 http://images2015.cnblogs.com/blog/998529/201702/998529-20170207182539822-867365375.png

如图位置,加入以下代码:

[html] view plain copy

  1. <security-constraint>   
  2.        <web-resource-collection >   
  3.               <web-resource-name >SSL</web-resource-name>   
  4.               <url-pattern>/*</url-pattern>   
  5.        </web-resource-collection>  
  6.                                
  7.        <user-data-constraint>   
  8.               <transport-guarantee>CONFIDENTIAL</transport-guarantee>   
  9.        </user-data-constraint>   
  10. </security-constraint>  

18,重启Tomcat,完成

 

猜你喜欢

转载自blog.csdn.net/qq_39429962/article/details/82860470