代码之家  ›  专栏  ›  技术社区  ›  en Peris

BCryptPasswordEncoder-编码的密码看起来不像BCrypt

  •  2
  • en Peris  · 技术社区  · 6 年前

    我有一个SpringBoot 2.0.1。发布mvc应用程序,这是我的配置文件

    @Configuration
    @EnableWebSecurity
    public class SecurityConfig extends WebSecurityConfigurerAdapter {
    
    
        @Autowired
        private Environment env;
    
    
        @Override
        protected void configure(HttpSecurity http) throws Exception {
    
            final List<String> activeProfiles = Arrays.asList(env.getActiveProfiles());
            if (activeProfiles.contains("dev")) {
                http.csrf().disable();
                http.headers().frameOptions().disable();
            }
    
            http
                    .authorizeRequests()
                    .antMatchers(publicMatchers()).permitAll()
                    .and()
                    .formLogin().loginPage("/login").defaultSuccessUrl("/elcordelaciutat/config")
                    .failureUrl("/login?error").permitAll()
                    .and()
                    .logout().permitAll();
        }
    
        @Autowired
        public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
    
             PasswordEncoder encoder = PasswordEncoderFactories.createDelegatingPasswordEncoder();
             UserDetails userDetails = User.withUsername("elcor")
                      .password(encoder.encode("elcor"))
                      .roles("ADMIN")
                      .build();
             auth.inMemoryAuthentication().withUser(userDetails);
    
        }
    
    
        private String[] publicMatchers() {
    
             /** Public URLs. */
            final String[] PUBLIC_MATCHERS = {
                    "/webjars/**",
                    "/css/**",
                    "/js/**",
                    "/images/**",
                    "/",
                    "/about/**",
                    "/contact/**",
                    "/error/**/*",
                    "/console/**"
            };
    
            return PUBLIC_MATCHERS;
    
        }
    
    }
    

    但当我登录到该应用程序时,我在日志文件中收到了以下消息:

    2018-04-11 11:27  [http-nio-5678-exec-7] WARN  o.s.s.c.b.BCryptPasswordEncoder - Encoded password does not look like BCrypt
    

    我无法登录。。。密码正确无误。将我的应用程序从SpringBoot 1更新到SpringBoot 2后,我出现此错误

    1 回复  |  直到 6 年前
        1
  •  5
  •   phisch    5 年前

    Spring Security在版本5中引入了一些重大更改。其中之一是在哈希中包含用于对密码进行哈希运算的算法。这使得迁移更加容易。

    密码的一般格式为:

    {id}encodedPassword 
    

    另请注意:如果您将密码存储在数据库中并设置了固定长度,这也可能导致意外截断哈希末尾,因为前面的id会增加哈希的长度。

    我还将一个项目从Spring Boot 1/Spring 4迁移到Spring Boot 2/Spring 5,并从BCrypt迁移到PBKDF2。

    我的密码编码器现在如下所示:

    public PasswordEncoder passwordEncoder() {
        // This is the ID we use for encoding.
        String currentId = "pbkdf2.2018";
    
        // List of all encoders we support. Old ones still need to be here for rolling updates
        Map<String, PasswordEncoder> encoders = new HashMap<>();
        encoders.put("bcrypt", new BCryptPasswordEncoder());
        encoders.put(currentId, new Pbkdf2PasswordEncoder(PBKDF2_2018_SECRET, PBKDF2_2018_ITERATIONS, PBKDF2_2018_HASH_WIDTH));
    
        return new DelegatingPasswordEncoder(currentId, encoders);
    }
    

    它还需要更新数据库并在所有当前哈希前面加上前缀 {bcrypt} (我以前只使用BCrypt)

    资料来源: Spring Blog