Spring Boot:Tomcat 重定向到 HTTPS 8443,无论我指定哪个 HTTPS 端口

时间:2021-01-28 18:13:40

标签: spring spring-boot tomcat hsts

我使用了一种相当标准的方法将 Spring Boot 的嵌入式 Tomcat 从 HTTP 重定向到 HTTPS,这在许多教程中重复。该方法非常适用于端口 HTTP 8080 和 HTTPS 8443,这些端口也在本教程中作为示例重复。但是,将这些端口的数量更改为较少使用的值会产生许多问题,如下所述。

教程中的方法如下。应用属性:

server.http.port=8080
server.http.interface=0.0.0.0

server.port: 8443
server.ssl.enabled: true
server.ssl.key-store: classpath:selfsigned.jks
server.ssl.key-store-password: password
server.ssl.key-store-type: JKS
server.ssl.key-alias: selfsigned

然后配置额外的HTTP端口:

@Component
public class HttpServer {
    @Value("${server.port}") int HTTPS_PORT;
    
    @Bean
    public ServletWebServerFactory servletContainer(@Value("${server.http.port}") int httpPort) {
        Connector connector = new Connector(TomcatServletWebServerFactory.DEFAULT_PROTOCOL);
        connector.setPort(httpPort);
        connector.setRedirectPort(HTTPS_PORT);
        TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory() {
            @Override
            protected void postProcessContext(Context context) {
                ((StandardJarScanner) context.getJarScanner()).setScanManifest(false);
            }
        };
        tomcat.addAdditionalTomcatConnectors(connector);
        return tomcat;
    }
}

最后是安全配置:

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception{
        http.cors().and().csrf().disable();
        http.requiresChannel().anyRequest().requiresSecure();
        http.headers().frameOptions().sameOrigin();
        http.portMapper()
                .http(Integer.parseInt(Prop.get("server.http.port")))
                .mapsTo(Integer.parseInt(Prop.get("server.port")));
    }
    @Bean
    CorsConfigurationSource corsConfigurationSource() {
        CorsConfiguration configuration = new CorsConfiguration();
        configuration.setAllowedOrigins(Arrays.asList("*"));
        configuration.setAllowedMethods(Arrays.asList("*"));
        configuration.setAllowedHeaders(Arrays.asList("*"));
        configuration.setAllowCredentials(true);
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", configuration);
        return source;
    }
}

与教程的唯一区别是我禁用了对 TomcatServletWebServerFactory 中 Jar 清单文件的扫描,否则如果包含 tomcat-embed-jasper(请参阅 here)会出现问题。

这个:

server.http.port=8080
server.port: 8443

正如我所说,与教程中一样完美。这:

server.http.port=8080
server.port: 5001

仍然重定向到 HTTPS 8443,但 8443 没有任何内容,因为 Tomcat 在 8080 和 5001 侦听:

INFO 7360 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 5001 (https) 8080 (http) with context path ''

我扫描了 8443 应用中的每个文件,包括源代码和二进制文件。那里没有 8443 的单个实例。我还使用调试器来验证 setPort()setRedirectPort()portMapper() 中的值是否正确。

这个:

server.http.port=5000
server.port: 5001

产生:

0:0:0:0:0:0:0:1 - - [28/Jan/2021:18:44:46 +0100] "GET /a HTTP/1.1" 302 -
0:0:0:0:0:0:0:1 - - [28/Jan/2021:18:44:46 +0100] "GET /a HTTP/1.1" 302 -
0:0:0:0:0:0:0:1 - - [28/Jan/2021:18:44:46 +0100] "GET /a HTTP/1.1" 302 -
0:0:0:0:0:0:0:1 - - [28/Jan/2021:18:44:46 +0100] "GET /a HTTP/1.1" 302 -
...

直到浏览器抱怨重定向过多。

该网站在 localhost 上进行了测试,证书无效,因此 Chrome 要求安全例外。尽管如此,Chrome 仍会将站点置于 HTTP 严格传输安全机制中,这可以在 chrome://net-internals/#hsts 处进行验证。是不是 Spring Boot 在 HSTS 标头中发送了一些导致重定向到 8443 的内容?

总而言之,似乎 80808443 有点特殊并且在某处声明,可能在 Tomcat 或 Spring Boot 中。 如何摆脱这些默认设置? 任何调试方式,例如重定向背后的原因?增加Tomcat、Spring Boot日志的冗长?

更新 1 为了禁用 HSTS,我修改了安全代码:

    http.headers().frameOptions().sameOrigin()
            .httpStrictTransportSecurity().disable();

并从 Chrome 的 HSTS 中删除了 localhost,但没有帮助。

更新 2 当应用程序在具有有效证书的外部服务器上运行时,问题仍然存在。

项目依赖:

    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-jasper</artifactId>
        <scope>compile</scope>
    </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-webflux</artifactId>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>
    <dependency>
        <groupId>org.projectreactor</groupId>
        <artifactId>reactor-spring</artifactId>
        <version>1.0.1.RELEASE</version>
    </dependency>

0 个答案:

没有答案