springboot 密码加密与比较密码简单笔记

需要的依赖

<dependency>
	<groupId>org.springframework.security</groupId>
	<artifactId>spring-security-core</artifactId>
</dependency>

在service层的代码

private BCryptPasswordEncoder bCryptPasswordEncoder;

public 类名() {
	bCryptPasswordEncoder = new BCryptPasswordEncoder();
}

@Override
public int 业务方法名(String oldPassword, String newPassword, HttpSession session) {

    // 检查旧密码是否准确 
    if (!bCryptPasswordEncoder.matches(oldPassword, password)){
        return 0;
    }
    
    // 新密码加密
    String updatePassword = bCryptPasswordEncoder.encode(newPassword);

    return 1;
}

你可能感兴趣的:(springboot)