Spring Boot 支持Https请求和http请求

声明:文章的创建引用两位大佬的blog,地址:

一文教你将 SpringBoot 网站升级为 HTTPS

spring boot支持https请求

一、创建一个spring boot工程

Spring Boot 支持Https请求和http请求_第1张图片

Spring Boot 支持Https请求和http请求_第2张图片

Spring Boot 支持Https请求和http请求_第3张图片

Spring Boot 支持Https请求和http请求_第4张图片

Spring Boot 支持Https请求和http请求_第5张图片

目录结构:

Spring Boot 支持Https请求和http请求_第6张图片

新建package

Spring Boot 支持Https请求和http请求_第7张图片

二、生成我们需要证书文件

1.生成证书命令:(前提是我们已经安装了jdk)

keytool -genkey -alias ox  -storetype PKCS12 -keyalg RSA -keysize 2048  -keystore keystore.p12 -validity 3650

2.把生成的证书放到项目中

Spring Boot 支持Https请求和http请求_第8张图片

三、修改配置及编码

1.修改我们的配置文件

Spring Boot 支持Https请求和http请求_第9张图片

2.新建配置类及controller

2.1.配置类:

package com.hn.y.config;

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.boot.web.servlet.server.ServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class TomcatHttpsConfig {

    @Bean
    public ServletWebServerFactory servletContainer() {
        TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory() {
            @Override
            protected void postProcessContext(Context context) {
                SecurityConstraint securityConstraint = new SecurityConstraint();
                securityConstraint.setUserConstraint("CONFIDENTIAL");
                SecurityCollection collection = new SecurityCollection();
                collection.addPattern("/*");
                securityConstraint.addCollection(collection);
                context.addConstraint(securityConstraint);
            }
        };
        tomcat.addAdditionalTomcatConnectors(redirectConnector());
        return tomcat;
    }

    private Connector redirectConnector() {
        Connector connector = new Connector(TomcatServletWebServerFactory.DEFAULT_PROTOCOL);
        connector.setScheme("http");
        connector.setPort(8081);
        connector.setSecure(true);//设置为false表示支持http和https两种请示,true表示只支持https
        connector.setRedirectPort(8080);
        return connector;
    }

}

2.2.controller类

package com.hn.y.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class IndexController {

    @RequestMapping(value = "/index",produces = "text/plain;charset=UTF-8")
    public String index(){
        return "Hello Spring Boot https";
    }
}

3.启动效果:

Spring Boot 支持Https请求和http请求_第10张图片

by the way.

如果是spring boot版本是1.5.10配置类需要如下:

@Configuration
public class TomcatHttpConfig {
    @Bean
    public EmbeddedServletContainerFactory servletContainer(){
        TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory() {
            @Override
            protected void postProcessContext(Context context) {
                SecurityConstraint securityConstraint = new SecurityConstraint();
                securityConstraint.setUserConstraint("CONFIDENTIAL");
                SecurityCollection collection = new SecurityCollection();
                collection.addPattern("/*");
                securityConstraint.addCollection(collection);
                context.addConstraint(securityConstraint);
            }
        };

        factory.addAdditionalTomcatConnectors(initiateHttpConnector());
        return factory;
    }

    private Connector initiateHttpConnector() {
        Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
        connector.setScheme("http");
        connector.setPort(8080);
        connector.setSecure(true);设置为false表示支持http和https两种请示,true表示只支持https
        connector.setRedirectPort(8100);
        return connector;
    }

其他版本没有测试过。具体原因:

Spring Boot 支持Https请求和http请求_第11张图片 旧版本

 

 

 

 

 

 

 

Spring Boot 支持Https请求和http请求_第12张图片 新版本

如有大佬觉得有问题,欢迎批评指正。谢谢

 

你可能感兴趣的:(Spring,Boot,Java,Web,tomcat)