【问题标题】:Spring Boot 2 security basic authenticationSpring Boot 2 安全基础认证
【发布时间】:2018-09-07 15:55:17
【问题描述】:

为什么以下基本安全配置不适用于 inMemoryAuthentication() 子句?

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

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

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication()
            .withUser("username").password("password");
        super.configure(auth);
    }

}

应用初始化后,仍然只有Spring自己生成的默认user,没有username这样的用户。

【问题讨论】:

    标签: java spring spring-boot spring-security


    【解决方案1】:

    不要从void configure(AuthenticationManagerBuilder auth) 调用超级方法。它将disableLocalConfigureAuthenticationBldr 标志设置为true,这会导致您的AuthenticationManagerBuilder 被忽略。最后你的void configure(AuthenticationManagerBuilder auth) 方法应该是这样的:

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication()
                .withUser("username").password("password").roles("USER");
    }
    

    【讨论】:

    • 这有点奇怪,因为在大多数情况下方法应该调用super。但是谢谢你的通知,似乎对我有用
    【解决方案2】:

    在 spring boot 2.x 中,您必须实现自己的 UserDetailsS​​ervice,如 herehere 所述

    例子:

    @Configuration
    public class SecurityConfig extends WebSecurityConfigurerAdapter {
    
        private static final Logger log = LogManager.getLogger();
    
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            // Note: 
            // Use this to enable the tomcat basic authentication (tomcat popup rather than spring login page)
            // Note that the CSRf token is disabled for all requests
            log.info("Disabling CSRF, enabling basic authentication...");
            http
            .authorizeRequests()
                .antMatchers("/**").authenticated() // These urls are allowed by any authenticated user
            .and()
                .httpBasic();
            http.csrf().disable();
        }
    
        @Bean
        public UserDetailsService userDetailsService() {
            // Get the user credentials from the console (or any other source): 
            String username = ...
            String password = ...
    
            // Set the inMemoryAuthentication object with the given credentials:
            InMemoryUserDetailsManager manager = new InMemoryUserDetailsManager();
            String encodedPassword = passwordEncoder().encode(password);
            manager.createUser(User.withUsername(username).password(encodedPassword).roles("USER").build());
            return manager;
        }
    
        @Bean
        public PasswordEncoder passwordEncoder() {
            return new BCryptPasswordEncoder();
        }
    }  
    

    【讨论】:

    • 如何从数据库中获取用户名和密码?
    • @SamZiggler 你只需要实现你自己的 UserDetailsS​​ervice,而不依赖 InMemoryUserDetailsManager
    猜你喜欢
    • 2021-01-17
    • 2018-05-19
    • 2018-02-14
    • 2021-01-12
    • 1970-01-01
    • 2020-11-28
    • 2019-03-10
    • 2016-12-20
    相关资源
    最近更新 更多