摘要:在这个Spring Security程序中,我可以使用(sam / abc125)和(neal / $ ENCRYPTED_abc125)登录 . (sam / abc125)登录不应该失败吗?

详细信息:在学习新东西的过程中,我从Websystique Spring Security 4 Hibernate Role Based Login Example复制了一个教程Spring Security项目 . 工作良好 . 特别是(sam / abc125)和(kenny / abc127)的登录/密码组合 . 这些登录工作正常,网页将您引导到正确的位置 .

我想将Bcrypt密码加密(来自Websystique Spring Security 4 Hibernate Password Encoder Bcrypt Example)添加到此示例中 - 将其添加到基于角色的示例似乎比将基于角色的内容添加到加密项目版本更可取 . 所以我仔细地将代码从启用加密的项目复制到基于角色的项目中 .

我对一些JSP页面稍作调整 - 缺少格式化所需的一些DIV . 然后我添加了一个新的用户记录(neal / $ ENCRYPTED_abc125) . $ ENCRYPTED_abc125是使用BCryptPasswordEncoder正确加密的“abc125” . 我检查了网页提供的“快速编码器”程序结果 .

即使密码“abc125”存储在用户表中的clear中,网站也接受(sam / abc125)登录 . 即使密码$ ENCRYPTED_abc125以加密方式存储在用户表中,它也接受(neal / $ ENCRYPTED_abc125)登录 . 它也接受(kenny / abc127) .

由于所有身份验证都隐藏在Spring Security中,因此无法通过它进行调试 . 对于我的问题,我留下了专业知识,这是:

Is there something within Spring Security that detects that the user table has an unencrypted password and encrypts it before comparing it with the user-supplied password text?

请注意,明文密码早于转换为加密存储,而不适用于实时版本 . 我的问题是试图弄清楚某些东西是否破损或是否按设计工作 .

这是项目的安全配置 .

@Configuration
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Autowired
    @Qualifier("customUserDetailsService")
    UserDetailsService userDetailsService;

    @Autowired
    CustomSuccessHandler customSuccessHandler;


    @Autowired
    public void configureGlobalSecurity(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService);
        auth.authenticationProvider(authenticationProvider()); 
    }

    @Bean
    public DaoAuthenticationProvider authenticationProvider() {
        DaoAuthenticationProvider authenticationProvider = new DaoAuthenticationProvider();
        authenticationProvider.setUserDetailsService(userDetailsService);
        authenticationProvider.setPasswordEncoder(passwordEncoder());
        return authenticationProvider;
    }

    @Bean
    public PasswordEncoder psswordEncoder() {  
        return new BCryptPasswordEncoder();
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
      http.authorizeRequests()
        //.antMatchers("/", "/home").permitAll()
        .antMatchers("/", "/home").access("hasRole('USER')")
        .antMatchers("/admin/**", "/newuser").access("hasRole('ADMIN')")
        .antMatchers("/db/**").access("hasRole('ADMIN') and hasRole('DBA')")
        //.and().formLogin().loginPage("/login")
        .and().formLogin().loginPage("/login").successHandler(customSuccessHandler)
        .usernameParameter("ssoId").passwordParameter("password")
        .and().csrf()
        .and().exceptionHandling().accessDeniedPage("/Access_Denied");
    }

}