You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Spring Security UserDetailsService时遇BCrypt密码格式错误

解决Spring Security中“Encoded password does not look like BCrypt”错误

问题场景

使用Spring Security的UserDetailsService实现用户认证时触发该错误,但不使用该组件时功能正常。注册环节已通过BCryptPasswordEncoder对密码加密,但调试发现系统直接将输入的明文密码与数据库中的BCrypt加密密码做对比,未自动执行密码匹配逻辑。

核心原因

Spring Security不会自动启用BCrypt密码匹配机制,必须在配置类中显式指定密码编码器;同时原代码中存在用户不存在的判断逻辑错误,可能引发异常流程。

解决方案

1. 配置Spring Security使用BCryptPasswordEncoder

在Spring Security配置类中声明BCryptPasswordEncoder Bean,并将其与UserDetailsService关联,确保框架使用BCrypt算法完成密码匹配:

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private UserDetailsService userDetailsService;

    // 声明BCrypt密码编码器Bean
    @Bean
    public BCryptPasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        // 关联UserDetailsService和密码编码器
        auth.userDetailsService(userDetailsService)
            .passwordEncoder(passwordEncoder());
    }

    // 根据业务需求配置其他安全规则,如授权、登录页面等
}

2. 修复UserDetailsService中的判断逻辑

原代码错误判断username是否为null,应改为判断查询到的employee对象是否为null,避免用户不存在时仍继续执行后续逻辑:

@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
    Employee employee = employeeRepository.findByUserName(username);
    System.out.println(employee);
    // 修正判断条件:检查employee是否存在
    if (employee == null) {
        throw new UsernameNotFoundException("Invalid user name or password");
    }
     return new User(employee.getUserName(), employee.getPassword(),
            mapRolesToAuthorities(employee.getRoles()));
}

3. 验证数据库中密码格式

确认数据库中存储的密码是合法的BCrypt加密串:

  • 合法BCrypt密码以$2a$、$2b$或$2y$开头
  • 长度固定为60个字符
  • 若存在未加密的旧密码,需重新注册用户或手动更新为BCrypt加密后的密码

内容的提问来源于stack exchange,提问作者Tilmeez Ur Rehman Bhatti

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.17 14:20:48