2
votes

I have a SpringBoot 2.0.1.RELEASE mvc application, Here is my config file

@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;

    }

}

But when I log to the application I got this message in the log file:

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

and I can't log in...and the password is correct. I have this error after update my app from SpringBoot 1 to SpringBoot 2

1
With a quick search I've found this . It could be that the hash format has changed because of a newer version of Bcrypt. - Ron Nabuurs

1 Answers

5
votes

Spring Security introduced some major changes with version 5. One of them is to include the algorithm used for hashing a password in the hash. This allows for easier migration.

The general format for a password is:

{id}encodedPassword 

As a side note: if you store your password in a database and have set an fix length, this could also lead to a case where you accidentally truncate the end of the hash because with the id in front of it the length of the hash increases.

I also migrated a project from Spring Boot 1 / Spring 4 to Spring Boot 2 / Spring 5 and went from BCrypt to PBKDF2.

My password encoder now looks like this:

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);
}

It also required to update the database and prefix all current hashes with {bcrypt} (I used BCrypt exclusively before)

Source: Spring Blog