How to use Spring security without password encoding?

前端 未结 4 2554
误落风尘
误落风尘 2021-02-20 10:49

I\'m trying to learn Spring security currently. I used BCryptPasswordEncoder to encode user password before persisting into a database

Code:



        
4条回答
  •  情话喂你
    2021-02-20 11:15

    Unsure if it is what you're after but a little cheap workaround I used so I didn't have to worry about encoding my database yet was

    @Configuration
    @EnableWebSecurity
    public class SecurityConfig extends WebSecurityConfigurerAdapter {
    
    @Autowired
      DataSource dataSource;
    
      @Autowired
      public void configAuthentication(AuthenticationManagerBuilder auth) throws Exception {
        auth.jdbcAuthentication().dataSource(dataSource)
            .usersByUsernameQuery("select username,CONCAT('{noop}',password),true from 
    public.\"Users\" where username=?")
            .authoritiesByUsernameQuery("select username,role from public.\"Users\" where 
    username=?");
    }
    ...
    

    Note instead of grabbing the password I am concatenating it with {noop} first to tell the system to not use encoding.

    Probably a sin but it works for now.

提交回复
热议问题