Encrypting InMemoryAuthentication passwords with Bcrypt

前端 未结 2 1451
长发绾君心
长发绾君心 2021-01-01 03:11

Before I use Bcrypt on a custom implementation of UserDetailsService, I first want to see if I can use it in an in-memory database.

package com.patrick.Secu         


        
相关标签:
2条回答
  • 2021-01-01 03:48

    With creating/exposing the PasswordEncoder bean this warning pops up which ultimately prevents me from accessing the login path:

    o.s.s.c.bcrypt.BCryptPasswordEncoder     : Encoded password does not look like BCrypt
    

    This is because the password you're providing is not encoded with BCrypt. Instead of passing "password" directly as the password it needs to be encoded first.

    For testing purposes, an easy way of doing this would be to just get a hold of your password encoder and encode it in your configure method like this

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        String password = passwordEncoder().encode("password");
        auth.inMemoryAuthentication().withUser("admin").password(password).roles("ADMIN");
    }
    
    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }
    
    0 讨论(0)
  • 2021-01-01 04:08

    With Spring Security 5 you can prefix password with id of selected PasswordEncoder. If you want to use plain password, then simply use {noop} prefix, this will delegate password encoder to NoOpPasswordEncoder.

    Example code:

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
       auth.inMemoryAuthentication()
              .withUser("admin").password("{noop}password").roles("ADMIN");
    }
    
    0 讨论(0)
提交回复
热议问题