簡體   English   中英

使用 Spring WebSecurityConfigurerAdapter 時,如何讓我的 Elastic Beanstalk 實例保持健康?

[英]How do I keep my Elastic Beanstalk instance healthy when using Spring WebSecurityConfigurerAdapter?

語境

我在 Elastic Beanstalk 上托管了一個 Java SpringBoot 應用程序,它正在運行。 我剛剛合並了一個添加 WebSecurityConfigurerAdapter 的分支,在加載頁面時需要用戶名和密碼。

在本地運行時,環境屬性位於application.properties中,它們在 Travis 和 Elastic Beanstalk 上的 Configuration -> Software 下設置。

當我Go to Environment時,一個警報提示我輸入用戶名和密碼,之后它就可以工作了。

正如預期的那樣,在配置中設置不正確的用戶名或密碼會導致502錯誤。

問題

此實例的運行狀況為“嚴重”, 100.0 % of the requests are erroring with HTTP 4xx.

我相信我在日志中發現了相關錯誤:

ConfigServletWebServerApplicationContext: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'springSecurityFilterChain' defined in class path resource [org/springframework/security/config/annotation/web/configuration/WebSecurityConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.servlet.Filter]: Factory method 'springSecurityFilterChain' threw exception; nested exception is java.lang.IllegalArgumentException: username cannot be null

新文件,SecurityConfig.java

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Autowired
    private Environment environment;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .csrf().disable()
            .authorizeRequests().anyRequest().authenticated()
            .and()
            .httpBasic();
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication()
            .withUser(environment.getProperty("USERNAME")).password("{noop}" + environment.getProperty("PASSWORD")).roles("USER");
    }
}

我對此的理解是,實例的 Health 是通過加載/來評估的,此時找不到用戶名,因此頁面無法加載。

如果是這種情況,我怎樣才能讓運行狀況檢查不會因為 http 安全性而失敗?

Spring 具有用於打開健康檢查端點的“執行器”

通過添加 gradle 依賴compile group: 'org.springframework.boot', name: 'spring-boot-starter-actuator' ,應用程序獲得一個/actuator/health端點,ELB 負載均衡器可以配置為執行檢查/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM