首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Spring Boot 2安全基本身份验证

Spring Boot 2安全基本身份验证
EN

Stack Overflow用户
提问于 2018-03-29 04:14:21
回答 2查看 10.3K关注 0票数 1

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

代码语言:javascript
运行
复制
@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这样的用户。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-03-29 07:22:53

不要从void configure(AuthenticationManagerBuilder auth)调用超方法。它将disableLocalConfigureAuthenticationBldr标志设置为true,从而导致您的AuthenticationManagerBuilder被忽略。最后,您的void configure(AuthenticationManagerBuilder auth)方法应该如下所示:

代码语言:javascript
运行
复制
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
    auth.inMemoryAuthentication()
            .withUser("username").password("password").roles("USER");
}
票数 6
EN

Stack Overflow用户

发布于 2018-05-14 16:26:51

在spring Boot2.x中,您必须按照herehere的描述实现您自己的UserDetailsService

示例:

代码语言:javascript
运行
复制
@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();
    }
}  
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49543394

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档