tomcat https访问设置

来源:互联网 发布:mssql 查找语句进程 编辑:程序博客网 时间:2024/05/01 11:08

1> 使用jdk工具生成key文件

[plain] view plaincopyprint?
  1. keytool -genkey -alias tomcat -keyalg RSA -keypass changeit -storepass changeit -keystore server.keystore -validity 3600  

 导出证书: 

[html] view plaincopyprint?
  1. keytool -export -keystore server.keystore -alias tomcat -file server.cer  

2>到tomcat/config/server.xml中找到

[html] view plaincopyprint?
  1. <!-- Define a SSL HTTP/1.1 Connector on port 8443  
  2.      This connector uses the JSSE configuration, when using APR, the   
  3.      connector should be using the OpenSSL style configuration  
  4.      described in the APR documentation -->  
  5. <!--  
  6. <Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"  
  7.            maxThreads="150" scheme="https" secure="true"  
  8.            clientAuth="false" sslProtocol="TLS" />  
  9. -->  
去掉注释,加入key文件配置

[html] view plaincopyprint?
  1. <Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"  
  2.            maxThreads="150" scheme="https" secure="true"  
  3.            clientAuth="false" sslProtocol="TLS"   
  4.   keystoreFile="server.keystore"      
  5.   keystorePass="changeit"/>  

保存后重启tomcat可通过 https://ip:8443/webproject(你的web项目)通过https访问,以上使用8443端口,若改为443在访问时可不加端口,因为443是https默认端口


强制https访问

1.tomcat下所有应用都强制https访问

在tomcat\conf\web.xml中的</welcome-file-list>后面加上以下配置:

[html] view plaincopyprint?
  1. <login-config>  
  2.     <!-- Authorization setting for SSL -->  
  3.     <auth-method>CLIENT-CERT</auth-method>  
  4.     <realm-name>Client Cert Users-only Area</realm-name>  
  5. </login-config>  
  6. <security-constraint>  
  7.     <!-- Authorization setting for SSL -->  
  8.     <web-resource-collection >  
  9.         <web-resource-name >SSL</web-resource-name>  
  10.         <url-pattern>/*</url-pattern>  
  11.     </web-resource-collection>  
  12.     <user-data-constraint>  
  13.         <transport-guarantee>CONFIDENTIAL</transport-guarantee>  
  14.     </user-data-constraint>  
  15. </security-constraint>  

2.单个应用强制https访问

WEB-INF/web.xml的</welcome-file-list>后面加上以下配置:

[html] view plaincopyprint?
  1. <login-config>  
  2.     <!-- Authorization setting for SSL -->  
  3.     <auth-method>CLIENT-CERT</auth-method>  
  4.     <realm-name>Client Cert Users-only Area</realm-name>  
  5. </login-config>  
  6. <security-constraint>  
  7.     <!-- Authorization setting for SSL -->  
  8.     <web-resource-collection >  
  9.         <web-resource-name >SSL</web-resource-name>  
  10.         <url-pattern>/*</url-pattern>  
  11.     </web-resource-collection>  
  12.     <user-data-constraint>  
  13.         <transport-guarantee>CONFIDENTIAL</transport-guarantee>  
  14.     </user-data-constraint>  
  15. </security-constraint>  

这样输入http://ip:8080/webproject会强制转到 https://ip:8443/webproject


若https端口配置为其它端口了记得把http转接端口一起改了

[html] view plaincopyprint?
  1. <Connector port="8080" protocol="HTTP/1.1"   
  2.            connectionTimeout="20000"   
  3.            redirectPort="<span style="color:#FF0000;">8443</span>URIEncoding="UTF-8" />  

浏览器导入证书: