繁体   English   中英

无法在 Spring Boot 安全性中使用基本身份验证登录

[英]Can't sign in using basic auth in spring boot security

当我尝试使用内存用户名和密码登录时,它总是抛出无效。

@Override
    protected void configure(HttpSecurity http) throws Exception {
        http
        .csrf().disable()
        .authorizeRequests()
        .anyRequest()
        .authenticated()
        .and()
        .httpBasic();
    }
    
    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth)
            throws Exception {
        auth
            .inMemoryAuthentication()
                .withUser("user").password("password").roles("USER");
    }

在此处输入图片说明

即使我输入了正确的用户名和密码,该对话框也会始终显示。

在 configureGlobal(.....) 中,我们需要使用像BCryptPasswordEncoder这样的passwordEncoder ,或者如果我们不想使用任何密码编码器,那么我们必须像password("{noop}password") {noop} 表示使用NoOpPasswordEncoder 来验证这个密码。

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

@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
    auth.inMemoryAuthentication()
    .withUser("user").password("{noop}password").roles("USER");
}

由于您使用的是 inMemoryAuth,我假设您这样做是为了学习 spring 并且您的密码是“password”。 在这种情况下,您还应该让 spring 知道您输入的明文密码应保持原样,这是通过使用 NoOpPasswordEncoder 完成的。

@Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth)
            throws Exception {
        auth
            .inMemoryAuthentication()
               .passwordEncoder(NoOpPasswordEncoder.getInstance())
               .withUser("user").password("password").roles("USER");
    } 

NoOpPasswordEncoder 将显示为已弃用,因为它通常不打算使用。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM