本文档针对war和jar的打包方式分别讲述配置方法。
一、War包使用Tomcat部署
1.环境说明
tomcat 版本:apache-tomcat-8.5.53
2.在conf下创建cert文件夹,用于存放pfx证书
3.server.xml配置文件
<!-- 配置证书,https ,使用https 默认端口 443 -->
<Connector port="443" protocol="org.apache.coyote.http11.Http11NioProtocol"
maxThreads="1000" acceptCount="1000" SSLEnabled="true" scheme="https" secure="true"
clientAuth="false" sslProtocol="TLS"
keystoreFile="./conf/cert/xxxx__xxxx.com.pfx"
keystoreType="PKCS12"
keystorePass="xxxxxxx"
ciphers="TLS_RSA_WITH_AES_128_CBC_SHA,TLS_RSA_WITH_AES_256_CBC_SHA,TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256,TLS_RSA_WITH_AES_128_CBC_SHA256,TLS_RSA_WITH_AES_256_CBC_SHA256" />
注:使用默认端口,可以在url不写明端口号直接访问。
4.重启tomcat,验证配置是否成功
http方式:
https方式:
注:以上配置http(80端口)和https(443端口)都可访问,若想将http全部重定向为https,可以在tomcat 的 web.xml中增加以下配置:
<!-- http redirectTo https : Authorization setting for SSL -->
<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>
此部分参考大佬的文章如下,十分感谢。如果想实现其他复杂配置,建议看下面的文章。
Tomcat 8 配置ssl证书(.pfx)实现 https 访问
二、Jar 包,内置Tomcat(SpringBoot项目)
此部分是针对于SpringBoot项目打成Jar包部署,实现https方式访问。
1.环境说明
SpringBoot 2.3.4.RELEASE
2.添加配置
import org.apache.catalina.Context;
import org.apache.catalina.connector.Connector;
import org.apache.tomcat.util.descriptor.web.SecurityCollection;
import org.apache.tomcat.util.descriptor.web.SecurityConstraint;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import static org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory.DEFAULT_PROTOCOL;
/**
* @param applicationBuilder
* @return
*/
@Configuration
public class HttpToHttpsConfig {
/**
* http重定向到https
* @return
*/
@Bean
public TomcatServletWebServerFactory servletContainer() {
TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory() {
@Override
protected void postProcessContext(Context context) {
SecurityConstraint constraint = new SecurityConstraint();
constraint.setUserConstraint("CONFIDENTIAL");
SecurityCollection collection = new SecurityCollection();
collection.addPattern("/*");
constraint.addCollection(collection);
context.addConstraint(constraint);
}
};
tomcat.addAdditionalTomcatConnectors(httpConnector());
return tomcat;
}
@Bean
public Connector httpConnector() {
// Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
Connector connector = new Connector(DEFAULT_PROTOCOL);
connector.setScheme("http");
//Connector监听的http的端口号
connector.setPort(80);
connector.setSecure(false);
//监听到http的端口号后转向到的https的端口号
connector.setRedirectPort(443);
return connector;
}
}
注:使用默认端口,可以在url不写明端口号直接访问。
采用上述配置后,http(端口80)访问会自动重定向到https(端口443)。