Chinaunix首页 | 论坛 | 博客
  • 博客访问: 175727
  • 博文数量: 32
  • 博客积分: 2510
  • 博客等级: 少校
  • 技术积分: 290
  • 用 户 组: 普通用户
  • 注册时间: 2004-10-16 12:00
文章分类

全部博文(32)

分类: 系统运维

2022-04-14 09:38:17

转自博客园@忽而今夏:https://www.cnblogs.com/tianzhenyun/p/5284311.html



Tomcat 启用 HTTPS

  最近在阿里云购买了一个云服务器ECS,简单的部署了个人网站,使用ip地址访问一切正常,我之前在TK上申请了一个免费域名,就绑定了二级域名到阿里云,第一天正常访问,第二天就不行了,访问站点直接提示域名未注册(未备案.....),无法访问,显然被阿里云拦截了,通过IP地址访问还是正常的。

  后来GOOGLE了一下,一般是备案域名和主机。。。,还有一种方式是启用HTTPS,http是明文传输,阿里云能够看到我访问的域名,发现在域名未注册然后拦截,但HTTPS是加密的,所以可以正常访问(哈哈)。

引用链接:




基本步骤:

  1. 使用 java 创建一个 keystore 文件
  2. 配置 Tomcat 以使用该 keystore 文件
  3. 测试
  4. 配置应用以便使用 SSL ,例如



执行 keytool -genkey -alias tomcat -keyalg RSA 结果如下

  1. loiane:bin loiane$ keytool -genkey -alias tomcat -keyalg RSA
  2. Enter keystore password: password
  3. Re-enter new password: password
  4. What is your first and last name?
  5.   [Unknown]: Loiane Groner
  6. What is the name of your organizational unit?
  7.   [Unknown]: home
  8. What is the name of your organization?
  9.   [Unknown]: home
  10. What is the name of your City or Locality?
  11.   [Unknown]: Sao Paulo
  12. What is the name of your State or Province?
  13.   [Unknown]: SP
  14. What is the two-letter country code for this unit?
  15.   [Unknown]: BR
  16. Is CN=Loiane Groner, OU=home, O=home, L=Sao Paulo, ST=SP, C=BR correct?
  17.   [no]: yes
  18.   
  19. Enter key password for
  20.     (RETURN if same as keystore password): password
  21. Re-enter new password: password

这样就在用户的主目录下创建了一个 .keystore 文件


配置 Tomcat 以使用 keystore 文件

打开 server.xml 找到下面被注释的这段

  1. <!--
  2. <Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"
  3.     maxThreads="150" scheme="https" secure="true"
  4.     clientAuth="false" sslProtocol="TLS" />
  5. -->


取消注释,并将内容改为

点击(此处)折叠或打开

  1. Connector SSLEnabled="true" acceptCount="100" clientAuth="false"
  2.     disableUploadTimeout="true" enableLookups="false" maxThreads="25"
  3.     port="8443" keystoreFile="${user.home}/.keystore" keystorePass="password"
  4.     protocol="org.apache.coyote.http11.Http11NioProtocol" scheme="https"
  5.     secure="true" sslProtocol="TLS" />


测试1:只启用HTTPS,不启用HTTP

在server.xml中,为8080端口的Connector加上注释,并且去掉8443端口的Connector的注释。然后启动tomcat。


分别通过http\https访问docs:


  1. http://localhost:8080/docs、https://localhost:8443/docs


结果:只有https可以访问。


测试2:HTTP、HTTPS同时启用

在server.xml中,去掉8080、8443端口的Connector的注释然后启动tomcat。

分别用http\https访问docs:


点击(此处)折叠或打开

  1. http://localhost:8080/docs、https://localhost:8443/docs


结果:两者都可正常访问。


配置应用使用 SSL

打开应用的 web.xml 文件,增加配置如下:

点击(此处)折叠或打开

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


将 URL 映射设为 /* ,这样你的整个应用都要求是 HTTPS 访问,而 transport-guarantee 标签设置为 CONFIDENTIAL 以便使应用支持 SSL。

如果你希望关闭 SSL ,只需要将 CONFIDENTIAL 改为 NONE 即可


SSL证书服务

文章到这里基本上就应该结束了,可是,当我通过https访问网站时问题提示“There is a problem with this website’s security certificate.”,自己还好说无所谓,但是别人访问的时候也提示,可能很大程度上,别人可能就不会再访问了。。。。,

所以需要SSL证书服务,网上也有很多免费的() 

我选择的是

Create a local Certificate Signing Request (CSR)

使用你之前创建的.keystore

  1. keytool -certreq -keyalg RSA -alias tomcat -file certreq.csr -keystore <your_keystore_filename>


一般顺利的话最终你可以拿到一些*.crt文件,

接下来就是把这些文件导入到.keystore文件里。

比如我拿到的是四个压缩包

点击(此处)折叠或打开

  1. for Apache.zip
  2. for IIS.zip
  3. for Nginx.zip
  4. for Other Server.zip


然后我使用的是for Other Server,里面有四个.crt

  1. 1_cross_Intermediate.crt
  2. 2_issuer_Intermediate.crt
  3. 3_user_contacts09.ml.crt
  4. root.crt


先执行root,1_cross_Intermediate.crt,2_issuer_Intermediate.crt

  1. keytool -import -alias <root> -keystore <your_keystore_filename> -trustcacerts -file <filename_of_the_chain_certificate>

最后执行 3_user_contacts09.ml.crt

  1. keytool -import -alias tomcat -keystore <your_keystore_filename> -file <your_certificate_filename>


最后得到的.keystore直接替换之前的就可以了,现在再次访问就不会提示你证书问题了,哈哈。。。。。

阅读(1193) | 评论(0) | 转发(0) |
0

上一篇:解决FTP因windows防火墙拦截的方法

下一篇:没有了

给主人留下些什么吧!~~