我正在使用帶有自定義 AuthenticationProvider 的 Spring Boot Security 來保護 Java Spring Boot 應用程式。通過瀏覽器訪問應用程式的嘗試被定向到自定義登錄頁面。我的安全配置類的主體粘貼在下面:-
@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 埠,并為 Application Load Balancer 添加了相應的偵聽器。
問題是當用戶嘗試通過 https 在瀏覽器中訪問應用程式時,Spring Security 會將用戶轉發到使用 http 而不是 https 作為協議的登錄頁面,例如用戶在瀏覽器中輸入以下地址:-
https://my-app:22223/content/documents
..并在這里轉發..
http://my-app:22223/content/login
因為這是一個 https 埠,所以用戶會看到這個錯誤頁面:-

如果用戶在瀏覽器地址欄中手動將協議更改為 https,則可以正常作業。
如果有人能告訴我為什么 Spring Boot Security 會以這種方式運行,以及我可以采取哪些步驟來強制它在登錄 URL 中使用 https,我將不勝感激。非常感謝您閱讀我的帖子!
uj5u.com熱心網友回復:
您應該配置 ALB 以終止 SSL(即注冊證書等)。如果配置正確,ALB 將自動添加一個標頭 (X-Forwarded-Proto),告訴 Spring Security 它需要使用 HTTPS 進行重定向。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/466488.html
上一篇:從串列中僅選擇一個用戶
