Spring 转发到登录页面时使用 http 而不是 https 启动安全

Spring Boot Security using http instead of https when forwarding to login page

我正在使用 Spring 引导安全和自定义 AuthenticationProvider 来保护 Java Spring 引导应用程序。尝试通过浏览器访问应用程序会被定向到自定义登录页面。我的安全配置正文 class 粘贴在下面:-

@EnableWebSecurity
@Configuration
public class SecurityConfiguration {

    @Bean
    public AuthenticationProvider authenticationProvider() {
        return new DocumentumAuthenticationProvider();
    }

    @Bean
    public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
        http
                .authorizeRequests()
                .anyRequest().authenticated()
                .and()
                .formLogin()
                .loginPage("/content/login")
                .permitAll()
                .and()
                .logout()
                .logoutUrl("/content/logout")
                .logoutSuccessUrl("/content/logout")
                .permitAll();
        return http.build();
    }

    @Bean
    public WebSecurityCustomizer webSecurityCustomizer() {
        return (web) -> web.ignoring().antMatchers("/retrieve/**", "/upload/**", "/content/css/**", "/content/scripts/**", "/content/images/**", "/content/images/**");
    }

    @Bean
    public BCryptPasswordEncoder encodePWD() {
        return new BCryptPasswordEncoder();
    }
}

当我 运行 我的服务在我的 IDE 本地时,一切正常。对于下一步,我将我的应用程序容器化并将其部署到 AWS EC2 服务器。我为应用程序配置了自定义 HTTPS 端口,并为应用程序负载均衡器添加了相应的侦听器。

问题是,当用户尝试通过 https 在浏览器中访问应用程序时,Spring 安全性会将用户转发到使用 http 而不是 https 作为协议的登录页面,例如用户在浏览器中输入以下地址:-

https://my-app:22223/content/documents

..并转发到这里..

http://my-app:22223/content/login

因为这是一个 https 端口,所以用户看到这个错误页面:-

如果用户在浏览器地址栏中手动将协议更改为 https,则可以正常工作。

如果有人能告诉我为什么 Spring Boot Security 会这样,以及我可以采取什么步骤强制它在登录中使用 https URL,我将不胜感激.非常感谢阅读我的 post!

您应该配置 ALB 以终止 SSL(即注册证书等)。如果配置正确,ALB 将自动添加一个 header (X-Forwarded-Proto),告诉 Spring 安全它需要使用 HTTPS 进行重定向。