【问题标题】:Spring Security simulataneous usage of multiple PasswordEncodersSpring Security 同时使用多个密码编码器
【发布时间】:2014-08-15 17:12:33
【问题描述】:

我有使用 bcrypt(默认参数)对 Spring Security 进行简单配置的 spring 应用程序,并且测试工作正常,但是我想计划这个应用程序使管理员或用户能够更改密码并选择身份验证参数以可以使用,例如: 1)bcrypt (BCryptPasswordEncoder) 2) sha(StandardPasswordEncoder)等哈希函数,

所以问题是如何更改以下类(或特别是 AuthenticationManagerBuilder)以反映某些用户可以将他们的密码存储为 sha 散列但其他存储为 bcrypt,考虑到数据库表已经有一个指定在密码列中存储哪种散列的列,即 bcrypt 或 sha。

@Configuration
@EnableWebMvcSecurity
@EnableGlobalMethodSecurity(prePostEnabled=true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Autowired
    private UserDetailsService iUserDetailsService;


    @Override
    protected void configure(HttpSecurity http) throws Exception {

        http
//          .csrf().disable()
//          .headers().disable()
            .headers()
            .contentTypeOptions()
            .xssProtection()
            .cacheControl()
            .httpStrictTransportSecurity()
            .frameOptions()
                .addHeaderWriter(new StaticHeadersWriter("X-Content-Security-Policy","script-src 'self"))
                .addHeaderWriter(new StaticHeadersWriter("Content-Security-Policy","script-src 'self'"))
                .addHeaderWriter(new StaticHeadersWriter("X-WebKit-CSP","default-src 'self'"))
            .and()
            .exceptionHandling()
                .accessDeniedHandler(syncAccessDeniedHandler())
            .and()
            .authorizeRequests()
                .antMatchers(   "/register",
                                "/static/**",
                                "/h2/**",
                                "/resources/**",
                                "/static/css/**", 
                                "/static/img/**" , 
                                "/static/js/**", 
                                "/static/pdf/**",
                                "/resources/static/css/**", 
                                "/resources/static/img/**" , 
                                "/resources/static/js/**", 
                                "/resources/static/pdf/**",
                                "/pdf/**",
                                "/css/**",
                                "/js/**",
                                "/img/**"

                                ).permitAll()
                .antMatchers("/admin/dashboard/**").hasAnyRole("STUDENT", "ADMIN")
                .antMatchers("/admin/network/**").hasRole("ADMIN")
                .anyRequest().authenticated()
                .and()
            .formLogin()
                .loginPage("/login")
                .permitAll()
                .and()
            .logout()
                .permitAll();
    }



    @Autowired
    public void registerAuthentication(AuthenticationManagerBuilder auth) throws Exception {
        auth
        .userDetailsService(iUserDetailsService).passwordEncoder(pwEncoder());        
    }

    @Bean
    public BCryptPasswordEncoder pwEncoder() {
        return new BCryptPasswordEncoder();        
    }


    @Bean
    public SyncAccessDeniedHandler syncAccessDeniedHandler() {
        String uri = "/403";
        return new SyncAccessDeniedHandler(uri);
    }



}

【问题讨论】:

  • 为什么要对任何帐户使用 SHA 而不是 bcrypt?此外,您可以将它们与字符串的格式区分开来(bcrypt 非常独特),而不需要单独的 DB 列。所以你可以实现一个PasswordEncoder,它查看字符串,然后委托给适当的编码器。
  • 我之所以真正问这个问题是因为我打算将来能够使用 scrypt (tarsnap.com/scrypt.html)。因此,当您提到实现 PasswordEncoder 时,它会在字符串中查找,然后根据字符串使用正确的编码器。你知道那里有没有例子吗?
  • 使用 scrypt 完全是另一回事,因为它没有标准的字符串格式。您必须自己在字符串中编码参数。但是,仍然可以区分 bcrypt 字符串或普通的十六进制编码字符串。
  • 正如我提到的,数据库已经有一个额外的列,说明在那里存储了什么样的哈希,即 bcrypt、scrypt、md5,如果在哈希上使用 pbkdf2,那么我有一个列说明多少轮已经制作了 PBKDF2。所以区分不同的哈希没有问题,我只是在寻找示例实现,即它应该如何在 Spring Security 中以正确的方式完成,因为我以前从未做过。我对 Spring Security 很陌生。
  • 你能做到吗?我需要做同样的事情。我在 db 中有一个用于加密类型的列,然后应该存在用户级加密。如果完成了,您可以发布您的答案吗?

标签: encoding spring-security passwords bcrypt sha


【解决方案1】:

也许您可以使用 Spring 5.0 中的 DelegatingPasswordEncoder 或对其进行扩展以创建您自己的版本。

https://docs.spring.io/spring-security/site/docs/current/api/org/springframework/security/crypto/password/DelegatingPasswordEncoder.html

【讨论】:

    猜你喜欢
    • 2012-01-17
    • 2014-04-23
    • 2023-03-09
    • 2019-10-26
    • 2016-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-19
    相关资源
    最近更新 更多