SpringBoot Security:Encoded password does not look like BCrypt 解决

本文详细解析了在使用SpringBootSecurity时遇到的“EncodedpassworddoesnotlooklikeBCrypt”错误,介绍了如何通过BCryptPasswordEncoder正确配置密码加密,确保用户登录成功。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

之前用spring security的时候 报了这个错误 ,可以不用配置.passwordEncoder()
结果今天使用springboot security 又碰到了同样的问题 ,而且不配置也会报错 ,密码输入正确也登录失败。。
SpringBoot Security:Encoded password does not look like BCrypt
错误代码:

@Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication()
                .withUser("admin")
                .password("123")
                .roles("ADMIN");
    }

正确代码:

@Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication()
                .withUser("admin")
                .password(new BCryptPasswordEncoder().encode("123"))
                .roles("ADMIN");
    }

在设置密码的时候使用BCryptPasswordEncoder()加密就行了!
注册时同理:

	account.setPassword(new BCryptPasswordEncoder().encode("123456"));
    accountService.addAccount(account);

在这里插入图片描述
$2a$是固定开头,10是默认strength, 可以手动设置一个[4.31]区间内的int值

@Bean
    public PasswordEncoder passwordEncoder(){
        return new BCryptPasswordEncoder(10);
    }
还有一种过时的方法。。。

.passwordEncoder(NoOpPasswordEncoder.getInstance())
告诉security不使用加密
不过既然是划线了 就不建议使用了…


修改密码: SpringSecurity 加密后需要修改密码

评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值