1.需要的jar包
<!--安全框架 2020.09.24 add by lm -->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
</dependency>
2.前端传递参数
//分别为原先的密码与新密码(原先的密码并不重要,有无皆可),展示controller内容
@RequestMapping("/updatePassWord")
public Result updatePassWord(@RequestBody Map<String,String> entity ){
//原先的密码
String oldName = entity.get("old");
//新密码
String newName = entity.get("new");
//验证传递是否正确
System.out.println(oldName+"--"+newName);
//通过spring-security容器获取登录id,此name为下文做铺垫,只有在登录验证当前工程下才能使用。
String name = SecurityContextHolder.getContext().getAuthentication().getName();
System.out.println(name);
return sellerService.updatePassWord(oldName,newName,name);
}
3.接口展示
//修改密码
Result updatePassWord(String oldName, String newName,String name);
4.service展示
@Override
public Result updatePassWord(String oldName, String newName,String name) {
//利用登录人id查询全部信息
TbSeller seller = findOne(name);
//BCrypt加密验证策略
BCryptPasswordEncoder bc = new BCryptPasswordEncoder();
//判断前端输入的原密码和加密后的密码是否一致
boolean matches = bc.matches(oldName, seller.getPassword());
if(matches){
String encode = bc.encode(newName);
seller.setPassword(encode);
update(seller);
Result result = new Result();
result.setSuccess(true);
result.setMessage("修改成功啊!!!!!");
return result;
}
return null;
}