如何在jdbcAuthentication中使用DelegatingPasswordEncoder?

时间:2018-06-26 07:21:56

标签: java spring-boot spring-security

我正在尝试将 DelegatingPasswordEncoder jdbcAuthentication()一起使用。但是,当我尝试执行此操作时,它的HTTP状态为403。但是当我使用inMemoryAuthentication()执行此操作时,它可以正常工作。谁能解释为什么会这样?我正在犯任何错误?

我正在使用bcrypt作为编码器,并且密码存储在数据库中,并以算法ID开头,如下例所示。

{bcrypt}$2a$10$t1E8PjjYqqp0Uovp6jVgS.r7J7yNzoH0pV3egIbzqQta0yznloJcG

ApplicationSecurityConfig.java

@Configuration
public class ApplicationSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private AppConfigProperties acp;

    @Autowired
    private DataSource dataSource;

    @Autowired
    private ApplicationUserRepo appUserRepo;

    protected void configure(AuthenticationManagerBuilder auth) throws Exception {

        String idForEncode = acp.getApplicationEncoding();
        Map encoders = new HashMap<>();
        encoders.put("bcrypt", new BCryptPasswordEncoder());
        encoders.put("noop", NoOpPasswordEncoder.getInstance());
        encoders.put("pbkdf2", new Pbkdf2PasswordEncoder());
        encoders.put("scrypt", new SCryptPasswordEncoder());
        encoders.put("sha256", new StandardPasswordEncoder());

        PasswordEncoder pwEncoder = new DelegatingPasswordEncoder(idForEncode, encoders);

        /*System.out.println(pwEncoder.encode("gad"));

        System.out.println(new BCryptPasswordEncoder().encode("gad"));

        Connection conn = dataSource.getConnection();
        PreparedStatement ps = conn.prepareStatement("select password from application_user where user_name='gad'");

        ResultSet rs = ps.executeQuery();

        while (rs.next()) {
            System.out.println(new BCryptPasswordEncoder().matches("gad", rs.getString(1)));
        }*/

        auth.jdbcAuthentication().dataSource(dataSource)
                                        .passwordEncoder(pwEncoder)
                                        .usersByUsernameQuery("select user_name, password, enabled from application_user where user_name=?")
                                        .authoritiesByUsernameQuery("select user_name, user_role from application_user_roles where user_name=?");

        /*auth.inMemoryAuthentication().passwordEncoder(pwEncoder)
                                    .withUser("gad").password(pwEncoder.encode("gad")).roles("USER")
                                    .and()
                                    .withUser("admin").password(pwEncoder.encode("admin")).roles("ADMIN");*/
    }

    protected void configure(HttpSecurity http) throws Exception {

        http.httpBasic().and().authorizeRequests()
                                    .antMatchers("/welcome/**").hasAnyRole("USER", "ADMIN")
                                    .antMatchers("/**").hasRole("ADMIN")
                                    .and()
                                    .csrf().disable().headers().frameOptions().disable()
                                    .and()
                                    .exceptionHandling().accessDeniedPage("/accessDenied");
    }

}

0 个答案:

没有答案