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

标签 spring spring-mvc spring-boot spring-security bcrypt

我有一个 SpringBoot 2.0.1.RELEASE 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 后出现此错误

最佳答案

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

关于spring - BCryptPasswordEncoder - 编码密码看起来不像 BCrypt,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49771558/

相关文章:

java - 交易异常 : Transaction not successfully started

java - Spring Controller 是线程安全的吗

java - 带有重音符号的编码错误。峰会形成时的 Spring-Mvc

java - 如何修复 "Field ... required a bean of type ... that could not be found"异常 Spring Boot

java - Spring boot @Cacheble 与 Ehcache

spring - 带有 RepositoryRestResource-s 和常规 Controller 的 Spring REST HATEOAS 中的根请求的自定义响应

Spring Batch Process 指示器模式

spring - 使用 @RequestMapping 匹配 URL 模式

java - Spring不兼容的版本?

java - 如何为空值注册 Spring 转换器?