Springboot使用内置tomcat时https的配置

步骤:

1:使用JDK自带工具keytool生成密钥对(.keystore文件)

2:在springboot项目中配置https

3:再在springboot项目中配置http(http和https同时可以访问)

4:在springboot项目中配置将http强制映射到https

第一步:

  可以参考我的另一篇文章https://blog.csdn.net/ITWANGBOIT/article/details/96475876

第二步:

  第一步生成的.keystore文件可以放在springboot项目的两个地方,配置不一样,但效果都一样

 (1)将.keystore放在类路径下:

配置如下:尤其注意文件路径,classpath:

(2)将.keystore放在项目根目录下:

  配置如下:尤其注意文件路径,没有classpath了

   经过以上配置之后,启动springboot项目,然后就可以通过https://localhost:8443来访问该项目。

第三步:

    需要既可以通过http的8080端口访问项目,又可以通过https的8443端口访问项目。但是spring boot 的appplication.yml或者appplication.properties只能配置一个端口,这就需要我们手动再添加一个Connector了。

   @Bean 
    public EmbeddedServletContainerFactory servletContainerFactory(){ 
        TomcatEmbeddedServletContainerFactory tomcatConfig = new TomcatEmbeddedServletContainerFactory(); 
        tomcatConfig.addAdditionalTomcatConnectors(this.newHttpConnector()); 
        return tomcatConfig; 
    }
     
    private Connector newHttpConnector() { 
        Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol"); 
        connector.setScheme("http"); 
        connector.setPort(8080); 
        connector.setSecure(false); 
        return connector; 
    }

这样以来,就可以通过两种协议访问项目,如下图:

但是有的系统,是只允许https去访问,当在地址栏里输入了http时,会自动转换成https

第四步:

  配置将http强制映射为https,将第三步Connector修改为以下代码:

  @Bean 
  public EmbeddedServletContainerFactory servletContainerFactory(){ 
      TomcatEmbeddedServletContainerFactory tomcatConfig = new  TomcatEmbeddedServletContainerFactory(){ 
          @Override 
          protected void postProcessContext(Context context) {
              SecurityConstraint securityConstraint = new SecurityConstraint(); 
              securityConstraint.setUserConstraint("CONFIDENTIAL"); 
              SecurityCollection collection = new SecurityCollection(); 
              // 配置以/*结尾的path。这样配置表示全部请求使用安全模式,必须走https 
              collection.addPattern("/*"); 
              //还可以配置哪些请求必须走https,这表示以/home/开头的请求必须走https 
              // collection.addPattern("/home/*"); 
              securityConstraint.addCollection(collection); 
              context.addConstraint(securityConstraint); 
          } 
      }; 
      tomcatConfig.addAdditionalTomcatConnectors(this.newHttpConnector()); 
      return tomcatConfig; 
  }
     
  private Connector newHttpConnector() { 
      Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol"); 
      connector.setScheme("http"); 
      connector.setPort(8080); 
      connector.setSecure(false); 
      // 如果只需要支持https访问,这里把收到的http请求跳转到https的端口 
      connector.setRedirectPort(8443); 
      return connector; 
  } 

经过以上配置之后,当在浏览器地址栏里输入http://localhost:8080,并回车请求资源时,地址栏的地址将自动变成

https://localhost:8443。

其中有一个注意点,就是Connector设置的setRedirectPort(8443)端口,要和application.yml配置文件中配置的https端口一致。

以上为springboot项目使用内置tomcat进行https的设置,还有一篇文章是非springboot项目在外置tomcat下的https的设置,文章地址为:https://blog.csdn.net/ITWANGBOIT/article/details/101367103

原文地址:https://www.cnblogs.com/hzcya1995/p/13302491.html