【问题标题】:Spring Boot 2.0.0.M4 breaks http basic auth in application.ymlSpring Boot 2.0.0.M4 打破了 application.yml 中的 http 基本身份验证
【发布时间】:2017-10-25 00:56:27
【问题描述】:

Spring Boot 1.5.6.RELEASE 遵循了我在下面的 application.yml 中指定的基本身份验证用户名和密码。

我已升级到 2.0.0.M4,现在应用程序始终以默认的“用户”和随机生成的密码启动。基本上下面的设置总是被完全忽略。

我在发行说明/文档中看到了一些特定于简化启用/禁用执行器安全性的更改。我没有看到任何具体的内容。

有什么想法吗?

来自我的 application.yml

security:
  basic:
    enabled: true
    realm: some-service
  user:
    name: example_user
    password: example_password

更新:

我已经确认这个功能从 Spring Boot 2.0.0.M4 开始就被明确删除了

在附录中:

M4 参考中缺少所有 security.basic.* 系列的东西:

https://docs.spring.io/spring-boot/docs/2.0.0.M4/reference/html/common-application-properties.html

但出现在 M3 参考中:

https://docs.spring.io/spring-boot/docs/2.0.0.M3/reference/html/common-application-properties.html

我能够暂时降级到 M3 以恢复以前的功能,但仍然希望获得一些关于替换它的指导。对于这种情况,我只需要一个硬编码的基本身份验证用户。我知道我可以使用对象配置来进行更复杂的设置。

【问题讨论】:

    标签: spring-boot spring-security


    【解决方案1】:

    编辑 2018-01-31:

    从 Spring Boot 2.0.0-RC1 (source) 开始,已恢复自动配置单个用户的功能(通过 spring.security.user 配置键)。

    原答案:

    从 Spring Boot 2.0.0-M4 开始,不推荐使用 Spring Boot security.* 属性。您可以在 Release Notes 中阅读相关内容:

    安全自动配置已完全重新审视:开发人员应阅读the companion blog post 并参阅Spring Boot 2.0 Security wiki page 以了解有关更改的更多详细信息。

    为了恢复基本的身份验证功能,您可以注册一个自定义WebSecurityConfigurerAdapter,如下所示:

    @Configuration
    public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
    
        @Bean
        public InMemoryUserDetailsManager inMemoryUserDetailsManager() {
            return new InMemoryUserDetailsManager(
                    User.withDefaultPasswordEncoder().username("user").password("password")
                            .authorities("ROLE_USER").build(),
                    User.withDefaultPasswordEncoder().username("admin").password("admin")
                            .authorities("ROLE_ACTUATOR", "ROLE_USER").build());
        }
    
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http.authorizeRequests()
                    .requestMatchers(EndpointRequest.to("health", "info")).permitAll()
                    .requestMatchers(EndpointRequest.toAnyEndpoint()).hasRole("ACTUATOR")
                    .requestMatchers(StaticResourceRequest.toCommonLocations()).permitAll()
                    .antMatchers("/**").hasRole("USER")
                    .and()
                .cors()
                    .and()
                .httpBasic();
        }
    
    }
    

    (这还将为 Actuator 端点配置基本身份验证)

    如果您还需要从.properties 文件中读取用户名和密码,您只需使用@Value 注释注入值即可。

    【讨论】:

      猜你喜欢
      • 2019-04-25
      • 2011-02-11
      • 1970-01-01
      • 2013-01-11
      • 2018-04-13
      • 1970-01-01
      • 2017-07-31
      • 1970-01-01
      • 2014-09-22
      相关资源
      最近更新 更多