spring:Spring Security实践过程中的报错总结

问题1

报错

You have entered a password with no PasswordEncoder. If that is your intent, it should be prefixed with {noop}.

解决

Spring Security 期望密码使用 PasswordEncoder 进行编码。如果你想使用明文密码(不推荐在生产环境中使用),你需要用 {noop} 前缀来标识。

  1. 在Spring Security配置中,确保你有一个PasswordEncoder bean。Spring Security 5推荐使用BCryptPasswordEncoder,因为它提供了较强的安全性。
@Bean  
public PasswordEncoder passwordEncoder() {
     
    return new BCryptPasswordEncoder();  
}
  1. 使用PasswordEncoder来加密密码。例如,如果你有一个用户服务,可以在创建或更新用户时加密密码。
@Autowired  
private PasswordEncoder passwordEncoder;  

public void createUser(String username, String rawPassword) {
     
    String encodedPassword = passwordEncoder.encode(rawPassword);  
    // 存储encodedPassword到数据库  
}

问题2

报错

我在客户端调用自定义的路由 /login,提示302,服务端报错
o.s.s.c.bcrypt.BCryptPasswordEncoder : Empty encoded password

解决

其实是因为接口和Spring Security默认的接口冲突了,如/login、/logout

  1. 修改默认登录接口

你可以通过配置Spring Security的HttpSecurity来修改默认的登录接口路径。例如,将默认的登录路径改为 /auth/login:

import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.<
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Lvan的前端笔记

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值