Springboot中设置http和https共同存在,且根据需要配置重定向

目前在项目中需要同时支持http和https,且在dev环境中,需要有参数来配置是否将http重定向到https
application.properties文件配置通用参数
application-dev.properties文件配置dev开发环境的参数
application-prod.properties文件配置生产环境的参数

配置jks
  • 生成jks

使用jdk自带的工具,根据提示按需输入

keytool -genkey -alias tomcat -keyalg RSA -keystore ./support-portal.jks
输入密钥库口令:
再次输入新口令:
您的名字与姓氏是什么?
  [Unknown]:  ma
您的组织单位名称是什么?
  [Unknown]:  t
您的组织名称是什么?
  [Unknown]:  t
您所在的城市或区域名称是什么?
  [Unknown]:  nanjing
您所在的省/市/自治区名称是什么?
  [Unknown]:  jiangsu
该单位的双字母国家/地区代码是什么?
  [Unknown]:  cn
CN=ma, OU=t, O=t, L=nanjing, ST=jiangsu, C=cn是否正确?
  [否]:  是

输入 <tomcat> 的密钥口令
        (如果和密钥库口令相同, 按回车):
再次输入新口令:

Warning:
JKS 密钥库使用专用格式。建议使用 "keytool -importkeystore -srckeystore ./support-portal.jks -destkeystore ./support-portal.jks -deststoretype pkcs12" 迁移到行业标准格式 PKCS12。
keytool -importkeystore -srckeystore ./server.jks -destkeystore ./support-portal.jks -deststoretype pkcs12
正在将密钥库 ./server.jks 导入到 ./support-portal.jks...
输入目标密钥库口令:
再次输入新口令:
输入源密钥库口令:
已成功导入别名 tomcat 的条目。
已完成导入命令: 1 个条目成功导入, 0 个条目失败或取消
  • 将jks文件复制到项目的resource/下
配置springboot

在application.properties中添加参数

下面参数中 ******* 代表密码,和第一步生成jks文件一致

server.ssl.key-store=classpath:support-portal.jks
server.ssl.key-store-password=*******
server.ssl.keyStoreType=PKCS12
server.ssl.keyAlias=tomcat
配置tomcat和server
  • 在application-dev.properties中添加参数
http.redirect.enable=false
http.port=8080

http.redirect.enable 当这个参数为false时,http和https将同时可以使用,https端口是8443{server.port},http端口为{http.port}
当配置为true时,http和https也可以使用,但是http的请求,全部重定向到https

  • 新建配置文件HttpsSetting.java

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.beans.factory.annotation.Value;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

@Component
public class HttpsSetting {

    /**
     * this is the http port default 80.
     */
    @Value("${http.port:80}")
    private Integer httpPort;

    /**
     * this is the https port default 443.
     */
    @Value("${server.port:443}")
    private Integer httpsPort;

    /**
     * weather http redirect enabled.
     */
    @Value("${http.redirect.enable:true}")
    private Boolean httpRedirectEnable;


    /**
     * this is the configuration of tomcat.
     * when the enable is false, the http requests would not redirect to https, also port.
     * @return
     */
    @Bean
    public TomcatServletWebServerFactory servletContainer() {
        if (httpRedirectEnable) {
            TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory() {
                @Override
                protected void postProcessContext(Context context) {
                    // http redirect to https
                    SecurityConstraint constraint = new SecurityConstraint();
                    constraint.setUserConstraint("CONFIDENTIAL");
                    SecurityCollection collection = new SecurityCollection();
                    collection.addPattern("/*");
                    constraint.addCollection(collection);
                    context.addConstraint(constraint);
                }
            };

            tomcat.addAdditionalTomcatConnectors(httpConnector());
            return tomcat;
        } else {
            TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
            tomcat.addAdditionalTomcatConnectors(httpConnector());
            return tomcat;
        }

    }

    /**
     *  it configure the connector, when get the http request port is 8080 or 80, it will redirect to 8443 or 443.
     * @return
     */
    private Connector httpConnector() {
        Connector connector = new Connector(TomcatServletWebServerFactory.DEFAULT_PROTOCOL);
        connector.setScheme("http");
        connector.setPort(httpPort);
        //the port of listened  Connector
        if (httpRedirectEnable) {
            connector.setSecure(false);
            //when the https is enabled, it will redirect to this port
            connector.setRedirectPort(httpsPort);
        }
        return connector;
    }

}

启动项目

在启动日志中可以看到
在这里插入图片描述
8080端口和8443端口都正常启动,使用restletclient测试
在这里插入图片描述在这里插入图片描述

  • 把http.redirect.enable设置为true,再次启动项目

依然可以看到http和https端口都启动了

在这里插入图片描述

可以看到http的请求被重定向到https了,而http的请求是正常执行的。

可以在application-prod.properties文件中配置server.port=443 http.port=80

配置完毕

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值