Tomcat配置证书,使用https

本文档详细介绍了如何配置Tomcat(war包)和SpringBoot(jar包)以实现HTTPS访问。对于Tomcat,涉及了在conf目录下创建cert文件夹,server.xml的SSL配置以及http到https的重定向。对于SpringBoot,展示了如何通过配置类添加额外的Connector实现http到https的自动重定向。所有配置均确保了默认端口的直接访问能力。

本文档针对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)。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值