spring-security Spring安全配置中的BcryptEncoder配置

6ioyuze2  于 2022-11-11  发布在  Spring
关注(0)|答案(1)|浏览(215)

所以我创建了这个restservice,但是我在编码器配置上遇到了麻烦。
我创建了一个config类来设置passwordencoderBean,如下面的回复所示。
我的程式码可以编译。但是当我尝试登入时,我收到“错误的认证”,而且我确定我使用的认证是正确的。此外,我的数据库中的密码是Bcryptencoded,前面有{bcrypt}。我猜想我的passwordEncoder设定错误。设定错误在哪里?
下面是我的密码配置:

@Configuration
public class PasswordEncoderConfig {
    @Bean
    public PasswordEncoder passwordEncoder() {
        return PasswordEncoderFactories.createDelegatingPasswordEncoder();
    }
}

当前SpringSecurity配置:

@EnableWebSecurity
class SecurityConfiguration extends WebSecurityConfigurerAdapter{
    private static final String ADMIN = "ROLE_ADMIN";
    private static final String WORKER = "ROLE_WORKER";

    private final DataSource dataSource;
    private PasswordEncoder bcryptencoder;

    public SecurityConfiguration(DataSource dataSource,  PasswordEncoder bcryptencoder) {
        this.dataSource = dataSource;
        this.bcryptencoder = bcryptencoder;
    }

    /*@Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }*/

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.jdbcAuthentication().dataSource(dataSource)
            .usersByUsernameQuery("select voornaam as username, password as password, true as enabled from gebruikers where voornaam = ?")
            .passwordEncoder(bcryptencoder)
            .authoritiesByUsernameQuery("select voornaam as username, role as authorities from gebruikers where voornaam = ?");

    }
    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring()
        .mvcMatchers("/images/**")
        .mvcMatchers("/css/**")
        .mvcMatchers("/js/**");
    }
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.cors().and().csrf().disable();
        /*remove after postman, @cross origin*/
        http.formLogin();
        http.authorizeHttpRequests(requests -> requests
            .mvcMatchers("/**").hasAnyAuthority(ADMIN, WORKER)
            .mvcMatchers("/gebruikers/**").hasAnyAuthority(ADMIN, WORKER));
        http.logout();

    }

}
kzipqqlq

kzipqqlq1#

要做的就是使用

@Bean
public PasswordEncoder passwordEncoder()
{
    return PasswordEncoderFactories.createDelegatingPasswordEncoder();
}

它使用BCrypt作为默认值,但为将来的迁移提供了一个更好的接口。

With Factory:
{bcrypt}$2a$10$Zz3xIJON0d1GI0vqMULIKOHCImVnFCWMNRE3Vw0ElvSmfCqGcDV5W

Without:
$2a$10$Zz3xIJON0d1GI0vqMULIKOHCImVnFCWMNRE3Vw0ElvSmfCqGcDV5W

当您使用工厂并提供不带前缀的bcrypt哈希时,它将被视为无效而拒绝。
编辑:正如Chaosfire所说,您定义了一个循环bean定义。您可以使用bean声明的方法,而不是将其注入到字段中,spring会将示例注入到方法调用中,所以您最终得到的是相同的密码编码器,它是您在bean声明中提供的。

@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
    auth.jdbcAuthentication().dataSource(dataSource)
        .usersByUsernameQuery("select voornaam as username, password as password, true as enabled from gebruikers where voornaam = ?")
        .passwordEncoder(passwordEncoder()) // referencing bean, not field
        .authoritiesByUsernameQuery("select voornaam as username, role as authorities from gebruikers where voornaam = ?");

相关问题