简体   繁体   English

使用 Spring WebSecurityConfigurerAdapter 时,如何让我的 Elastic Beanstalk 实例保持健康?

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

Context语境

I'm hosting a Java SpringBoot application on Elastic Beanstalk, and it's running.我在 Elastic Beanstalk 上托管了一个 Java SpringBoot 应用程序,它正在运行。 I've just merged a branch that adds WebSecurityConfigurerAdapter, requiring a username and password on loading the page.我刚刚合并了一个添加 WebSecurityConfigurerAdapter 的分支,在加载页面时需要用户名和密码。

When run locally, the environment properties are in application.properties , and they are set in Travis and on Elastic Beanstalk under Configuration -> Software.在本地运行时,环境属性位于application.properties中,它们在 Travis 和 Elastic Beanstalk 上的 Configuration -> Software 下设置。

When I Go to Environment , an alert prompts me for username and password, after which it works.当我Go to Environment时,一个警报提示我输入用户名和密码,之后它就可以工作了。

Setting incorrect username or password in Configuration causes 502 errors, as expected.正如预期的那样,在配置中设置不正确的用户名或密码会导致502错误。

Problem问题

The health of this instance is 'Severe', with 100.0 % of the requests are erroring with HTTP 4xx.此实例的运行状况为“严重”, 100.0 % of the requests are erroring with HTTP 4xx.

I believe that I've found the relevant error in the logs:我相信我在日志中发现了相关错误:

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

New file, SecurityConfig.java新文件,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");
    }
}

My understanding of this is that the Health of the instance is evaluated by loading / , at which point no username is found so the page fails to load.我对此的理解是,实例的 Health 是通过加载/来评估的,此时找不到用户名,因此页面无法加载。

If this is the case, how can I allow the Health checks to not fail because of http security?如果是这种情况,我怎样才能让运行状况检查不会因为 http 安全性而失败?

Spring has an 'Actuator' for opening up a Health Check endpoint Spring 具有用于打开健康检查端点的“执行器”

By adding the gradle dependency compile group: 'org.springframework.boot', name: 'spring-boot-starter-actuator' , the application gains an /actuator/health endpoint that the ELB Load Balancer can be configured to perform checks on instead of / .通过添加 gradle 依赖compile group: 'org.springframework.boot', name: 'spring-boot-starter-actuator' ,应用程序获得一个/actuator/health端点,ELB 负载均衡器可以配置为执行检查/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从Spring Java Elastic Beanstalk应用程序连接到我的AWS DynamoDB实例? - How do I connect to my AWS DynamoDB instance from a Spring Java Elastic Beanstalk app? 如何使用 AWS CLI 2 将更新的 JAR 上传到现有的 Java Elastic Beanstalk 实例? - How do I upload an updated JAR to an existing Java Elastic Beanstalk instance using the AWS CLI 2? 如何从 Elastic Beanstalk 的 Spring Boot 应用程序中获取客户端主机名和/或客户端 IP 地址? - How do I get the Client Hostname and/or client IP address from within a Spring Boot application in Elastic Beanstalk? 当我在 aws 弹性 beantalk 中部署 Spring Boot 项目时,如何压缩 Spring Boot 中的 MultipartFie[] 大小? - How can i compress the MultipartFie[] size in spring boot when i deploy the spring boot project in aws elastic beanstalk.? 部署到AWS Elastic Beanstalk后如何访问servlet? - How do I access servlets after deploying to AWS Elastic Beanstalk? 如何在AWS Elastic Beanstalk环境中配置负载均衡器? - How do I configure a load balancer in an AWS Elastic Beanstalk environment? 如何为弹性beanstalk tomcat提供配置 - How do I supply configuration to elastic beanstalk tomcat 如何使用Elastic Beanstalk和Java使用cron作业? - How do you use cron jobs using Elastic Beanstalk and Java? AWS Elastic Beanstalk(单实例):如何为 Spring 启动应用程序启用 HTTPS? - AWS Elastic Beanstalk (single instance) : how to enable HTTPS for a Spring Boot application? 使用Elastic Beanstalk时保留WAR名称 - Keeping WAR name when using Elastic Beanstalk
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM