在 Tomcat 上配置 SSL 并设置从 HTTP 到 HTTPS 的自动重定向的步骤

**安全接口层(SSL)**是通过互联网提供消息安全的加密协议,它基于私钥和公共钥匙的概念工作,并在通过网络发送消息之前进行加密。要在Tomcat上配置SSL,我们需要一个可以使用Java _keytool_创建的数字证书,用于开发环境。

创建SSL证书

按照以下步骤创建自己的数字证书。

 1$ keytool -genkey -alias tomcat -keyalg RSA -keystore mycertificate.cert
 2Enter keystore password:
 3Re-enter new password:
 4What is your first and last name?
 5  [Unknown]:  Pankaj Kumar
 6What is the name of your organizational unit?
 7  [Unknown]:  Dev
 8What is the name of your organization?
 9  [Unknown]:  JournalDev
10What is the name of your City or Locality?
11  [Unknown]:  Bangalore
12What is the name of your State or Province?
13  [Unknown]:  Karnataka
14What is the two-letter country code for this unit?
15  [Unknown]:  IN
16Is CN=Pankaj Kumar, OU=Dev, O=JournalDev, L=Bangalore, ST=Karnataka, C=IN correct?
17  [no]:  Yes
18
19Enter key password for <tomcat>
20    (RETURN if same as keystore password):
21Re-enter new password:
22$ ls
23mycertificate.cert

我已经使用了密码更改的Keystore和密钥,但你可以使用任何你想要的。现在我们的数字证书已经准备好了,下一步是启用HTTPS通信端口在Tomcat,并设置它使用我们的数字证书提供SSL支持。

托马斯HTTPS

要启用SSL,打开 ~Tomcat_Installation/conf/server.xml 文件,并发出下面的评论:

1<Connector port="8443" maxHttpHeaderSize="8192"
2               maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
3               enableLookups="false" disableUploadTimeout="true"
4               acceptCount="100" scheme="https" secure="true"
5               keystoreFile="/Users/Pankaj/tomcat/conf/mycertificate.cert"
6           clientAuth="false" sslProtocol="TLS" />

To avoid any misplacement of the certificate, I have put that in the tomcat conf directory. Now restart Tomcat and try to access any web application over https with port 8443. Tomcat SSL Enabled

Tomcat 重定向 HTTP 到 HTTPS

因此,我们可以在 HTTP 和 HTTPS 端口上访问任何 Web 应用程序,我们可以设置 tomcat 来将所有 HTTP 请求重定向到 HTTPS 端口,并使用某些配置。

  1. 联合国 在TomcatInstallation/conf/server.xml中,用于HTTP 连接器,将重定向端口设置为HTTPS连接器端口. 它看起来会有点像:
 1<!- 8080号端口上定义一个非SSL HTTP/1.1 连接器 - > (-MKBR1_) < Connector port="8090" > maxHtpheaderSize="8192" (-MKBR1_) MaxThreads="150" minSpareThreads="25" maxSpareThreads="75" (-MKBR1_) 启用Lookups="false" transfort="Count="100"(-MKBR1_) 连接Timout="20000" 禁用下载时间=""8192" (-MKBR1_) <Li> > > (Li> > > Tomcat Install/web.xml_(sermt-t-t-t-ttt MKBR1_) 
 2<pre> 
 3<!-由潘克杰为自动从HTTP转向HTTPS而添加 -- > 
 4<security-construction > 
 5<web-资源-收集 > 
 6<web-资源-名称 > Entire应用程序 </web-资源-名称 > (_) MKBR1_) <url-pattern>/*</url-pattern > 
 7</web-resource-collection > 
 8<user-data-control> (_) MKBR1_) < 交通-担保 > > CONFidential < / 交通-担保 > 
 9< / 用户-数据-约束 > 
10< / 安全-约束 > 
11> 
12
13现在重新启动Tomcat,所有HTTP请求将自动重定向到HTTPS,HTTPS://localhost:8080/axis2将自动重定向到HTTPS://localhost:8443/axis2 **Note**:如果您不想在URL中提供端口,然后使用80HTTP和443为HTTPS。在这种情况下,您可以跳过第一步自动重定向HTTP请求到HTTPS,因为它将自动选择默认端口443
14
15* [Java Web Application Tutorial](/community/tutorials/java-web-application-tutorial-for-beginners Java Web Application Tutorial for Beginners)
16* [Java Servlet Tutorial](/community/tutorials/servlet-jsp-tutorial Java Servlet Tutorial with Examples for Beginners)
Published At
Categories with 技术
Tagged with
comments powered by Disqus