关于Spring中的BCryptedPasswordEncoder

问题描述 投票:0回答:1

我在Spring中遇到BCryptPasswordEncoder的问题。如果我使用SQL Server存储加密的密码,则当我尝试登录时,会收到一条消息,例如:“编码的密码看起来不像BCrypt”。

但是,当我使用MySQL并使用相同的密码时,我可以正常登录。

如何使用SQL Server登录?

这里是扩展WebSecurityConfigurerAdapter类的代码,其中包含BCryptPasswordEncoder

@Autowired
private UserServiceImpl userService;

@Bean
public PasswordEncoder passwordEncoder() {
    return new BCryptPasswordEncoder();
}

@Autowired
public void configure(AuthenticationManagerBuilder auth) throws Exception {
    auth.userDetailsService(userService).passwordEncoder(passwordEncoder());
}

@Override
protected void configure(HttpSecurity http) throws Exception {
    // TODO Auto-generated method stub
    http.csrf().disable().authorizeRequests().antMatchers("/students").hasAnyRole("USER", "ADMIN")
            .antMatchers("/students/addStudent").hasRole("ADMIN").antMatchers("/students/edit/*").hasRole("ADMIN")
            .antMatchers("/students/{id}").hasRole("ADMIN").anyRequest().authenticated().and().formLogin()
            .loginPage("/login").defaultSuccessUrl("/students").permitAll().and().logout()
            .invalidateHttpSession(true).clearAuthentication(true)
            .logoutRequestMatcher(new AntPathRequestMatcher("/logout")).logoutSuccessUrl("/login?logout")
            .permitAll().and().exceptionHandling().accessDeniedPage("/access-denied");
}

enter image description here

spring spring-boot
1个回答
0
投票

假设您使用的是最新的Spring Security 5.x,您需要阅读一下Spring的文档,因为它们做了一些可能不熟悉的重大更改,特别是关于password storage

您可以尝试使用passwordEncoder更改DelegatingPasswordEncoder Bean

@Bean
public static PasswordEncoder passwordEncoder() {
      return PasswordEncoderFactories.createDelegatingPasswordEncoder();
}

根据Spring文档:

此更改可确保现在使用BCrypt通过默认情况下,允许以旧格式验证密码,并允许用于将来升级密码存储。

© www.soinside.com 2019 - 2024. All rights reserved.