【问题标题】:How to redirect http traffic to https with spring boot 2.3.1如何使用 Spring Boot 2.3.1 将 http 流量重定向到 https
【发布时间】:2020-10-07 19:47:24
【问题描述】:

下午好,我正在使用spring boot并且已经实现了http安全模式(https),但是我需要知道如何将http请求重定向到https,以及是否需要指定一个特定的安全端口,比如443,或者我可以用任何端口做到这一点。目前我使用端口 8080 8443

我的班级

主类

package com.prueba.https;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

@SpringBootApplication
public class PruebaHttpsApplication {

    public static void main(String[] args) {
        SpringApplication.run(PruebaHttpsApplication.class, args);
    }

}

请求类

package com.prueba.http.request;

public class PruebaRequest {

    public String nombre;

    public String getNombre() {
        return nombre;
    }

    public void setNombre(String nombre) {
        this.nombre = nombre;
    }

}

响应类

package com.prueba.https.response;

public class PruebaResponse {

    public String saludo;

    public String getSaludo() {
        return saludo;
    }

    public void setSaludo(String saludo) {
        this.saludo = saludo;
    }


}

控制器类

package com.prueba.https.controller;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import com.prueba.http.request.PruebaRequest;
import com.prueba.https.response.PruebaResponse;

@RestController
public class PruebaController {


    @PostMapping(value = "/PruebaHTTPS") 
    public Object PruebaController(@RequestBody PruebaRequest req) {
        PruebaResponse res = new PruebaResponse();
        res.setSaludo("Hola " +req.getNombre());
        return res;

    }

}

应用程序属性

server.port=8443

server.ssl.key-store = classpath:keystore.p12
server.ssl.key-store-password = pass
server.ssl.keyStoreType = PKCS12
server.ssl.keyAlias = tomcat

pom xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.prueba.https</groupId>
    <artifactId>PruebaHTTPS</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>PruebaHTTPS</name>
    <description>Pruebas HTTPS</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>


        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

我的项目结构

Conexion HTTPS

Conexion HTTP

更新

使用以下适用于我的 spring 版本的代码,但是它给我的 http 请求错误:

package com.prueba.https;
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.autoconfigure.EnableAutoConfiguration;
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.ComponentScan;
import org.springframework.context.annotation.Configuration;

@ComponentScan
@Configuration
@EnableAutoConfiguration
public class WebsocketSourceConfiguration {

      @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(getHttpConnector());
        return tomcat;
      }

      private Connector getHttpConnector() {
        Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
        connector.setScheme("http");
        connector.setPort(8080);
        connector.setSecure(false);
        connector.setRedirectPort(8443);
        return connector;
      }



}

HTTPS (8443)

HTTP(8443)

HTTP(8080)

为什么我不能指向 http:8443 并将我重定向到 https?

【问题讨论】:

    标签: java spring-boot ssl https


    【解决方案1】:

    要将您的 HTTP 请求重定向到 HTTPS 请求,您需要添加以下配置:

    @Bean
    public EmbeddedServletContainerFactory servletContainer() {
      TomcatEmbeddedServletContainerFactory tomcat = 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);
          }
        };
    
      tomcat.addAdditionalTomcatConnectors(redirectConnector());
      return tomcat;
    }
    
    private Connector redirectConnector() {
      Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
      connector.setScheme("http");
      connector.setPort(8080);
      connector.setSecure(false);
      connector.setRedirectPort(8443);
      return connector;
    }
    

    我希望这对你有用。

    【讨论】:

    • 在您的帮助下更新我的答案,但是端口出现错误。你知道它可能是什么吗?
    • 将 8080 放入重定向 connect() 第 3 行的 http 设置端口。
    • 更新我的方法。为什么我不能指向 http:8443 并将我重定向到 https? http: 8080 也给我报错。
    猜你喜欢
    • 2014-12-26
    • 2015-12-06
    • 2019-09-21
    • 2012-04-19
    • 2019-05-15
    • 2017-12-25
    • 2014-04-27
    • 1970-01-01
    • 2021-03-31
    相关资源
    最近更新 更多