首页 > 解决方案 > Spring Boot Security - 从 http 8080 重定向到 https 433 不能正常工作

问题描述

我需要帮助。我用spring security创建了一个简单的项目。但是从 8080 重定向到 433 并没有达到我的预期。

    @Bean
public ServletWebServerFactory servletContainer() {
    // Enable SSL Traffic
    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);
        }
    };

    // Add HTTP to HTTPS redirect
    tomcat.addAdditionalTomcatConnectors(httpToHttpsRedirectConnector());

    return tomcat;
}

private Connector httpToHttpsRedirectConnector() {
    Connector connector = new Connector(TomcatServletWebServerFactory.DEFAULT_PROTOCOL);
    connector.setScheme("http");
    connector.setPort(8080);
    connector.setSecure(false);
    connector.setRedirectPort(443);
    return connector;
}

从 localhoste:8080/ 自动重定向后,url 如下所示: https://localhost

服务器没有响应。我必须像这样手动将端口号添加到 url: https://localhost:443/

我的应用程序属性:

server.port=433
server.ssl.enabled=true
server.ssl.key-store=src/main/resources/ssl_security.p12
server.ssl.key-store-password=secret
server.ssl.key-store-type=PKCS12
server.ssl.key-alias=ssl_security

我做错事情了?或者这可能是我的一些网络浏览器配置?

以下链接到github:

https://github.com/scina/SpringBootSecurity

标签: spring-bootsslredirectspring-security

解决方案


推荐阅读