1.下载pfx证书:作者使用的是阿里云的免费证书
2.下载tomcat, 作者使用的是tomcat-10.1.13
3.配置server.xml
在tomcat\conf目录下找到server.xml
首先将80端口的http请求转发到443端口
<Connector port="80" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="443"
maxParameterCount="1000"
/>
再配置443端口
<Connector port="443" protocol="org.apache.coyote.http11.Http11NioProtocol"
maxThreads="150" SSLEnabled="true"
maxParameterCount="1000"
>
<UpgradeProtocol className="org.apache.coyote.http2.Http2Protocol" />
<SSLHostConfig>
<Certificate certificateKeystoreFile="D:\tomcat-10.1.13\conf\cert\www.example.com.pfx"
certificateKeystorePassword="your_password"
type="RSA" />
</SSLHostConfig>
</Connector>
最后配置conf目录下的web.xml
在文件末尾</welcome-file-list>后面加上下面的代码:
<login-config>
<auth-method>CLIENT-CERT</auth-method>
<realm-name>Client Cert Users-only Area</realm-name>
</login-config>
<security-constraint>
<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>
经过上面的配置,作者实现了https访问和http自动跳转https,仅供参考。