Tomcat制作SSL证书,并添加强制重定向访问页面。
Linux环境下可利用keytool命令生成证书。
keytool -genkey -v -alias Key -keyalg RSA -validity 3650 -keystore /usr/local/tomcat/conf/1.keystore
相关参数含义:
keytool -genkey:自动使用默认的算法生成公钥和私钥
-alias[名称]:给证书取个别名
-keyalg:制定密钥的算法,如果需要制定密钥的长度,可以再加上keysize参数,密钥长度默认为1024位,使用DSA算法时,密钥长度必须在512到1024之间,并且是64的整数倍
-keystore:参数可以指定密钥库的名称。密钥库其实是存放迷药和证书文件,密钥库对应的文件如果不存在会自动创建。
-validity:证书的有效日期,默认是90天
-keypass changeit:不添加证书密码
-storepass changeit:不添加存储证书的密码
输入命令后提示:
Enter keystore password:123456 //密码 Re-enter new password:123456 //确认密码 What is your first and last name? //你的名字 [Unknown]: 1 What is the name of your organizational unit? //你单位的名称 [Unknown]: 1 What is the name of your organization? //你组织的名称 [Unknown]: 1 What is the name of your City or Locality? //所在城市 [Unknown]: lasa What is the name of your State or Province? //所在省份 [Unknown]: xizang What is the two-letter country code for this unit? //两个字符的国家识别码 [Unknown]: CN Is CN=ffcs.cn, OU=ffcs.cn, O=ffcs.cn, L=lasa, ST=xizang, C=CN correct? //确认以上信息 [no]: Y Generating 2,048 bit RSA key pair and self-signed certificate (SHA256withRSA) with a validity of 3,650 days for: CN=1, OU=1, O=1, L=lasa, ST=xizang, C=CN Enter key password for <Key> (RETURN if same as keystore password): //直接回车即可 [Storing /usr/local/tomcat/conf/1.keystore] //提示保存路径
编辑server.xml:
<Connector port="8443" protocol="org.apache.coyote.http11.Http11NioProtocol" maxThreads="150" SSLEnabled="true"> <SSLHostConfig> <Certificate certificateKeystoreFile="conf/1.keystore" type="RSA" certificateKeystorePassword="123456" /> </SSLHostConfig> </Connector>
重启Tomcat即可。
扩展:
在配置文件中会存在以下配置信息,此项配置为重定向,但并非强制重定向。
<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />
需要强制重定向请编辑配置文件:web.xml中加入以下内容
...... </welcome-file-list> <login-config> <!-- Authorization setting for SSL --> <auth-method>CLIENT-CERT</auth-method> <realm-name>Client Cert Users-only Area</realm-name> </login-config> <security-constraint> <!-- Authorization setting for SSL --> <web-resource-collection > <web-resource-name >SSL</web-resource-name> <url-pattern>/*</url-pattern> </web-resource-collection> <user-data-constraint> <transport-guarantee>CONFIDENTIAL</transport-guarantee> </user-data-constraint> </security-constraint> </web-app>
参考资料:
https://www.jianshu.com/p/f556fd3c66ca
https://blog.csdn.net/jeep_ouc/article/details/32343553
发表评论