云e办(后端)——个人中心
一、个人中心
-
在普通项目中需要获取当前登录用户的信息,一般做法是在登录成功后,将当前用户信息存入session中。在需要使用当前用户的时候从session里面读取。更新用户信息也是直接通过数据库进行相应的更新。
-
在Spring Security中要如何获取用户信息和更新用户信息呢?
在Spring Security中提供了一个 Authentication 对象,我们可以在 Controller 或者 Service 中,直接注入 Authentication ,注入成功后,就能直接使用。这样我们就能通过 Authentication 对象直接获取用户信息。 -
在Spring Security中更新用户信息,除了正常的去数据库进行相应的更新之外,还需要重新构建Authentication 对象,这样才能在项目中正确的获取到更新后的用户信息。
二、个人中心具体代码
1.controller 修改信息和密码
package com.xxxx.server.controller;
import com.xxxx.server.pojo.Admin;
import com.xxxx.server.pojo.RespBean;
import com.xxxx.server.service.IAdminService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import java.util.Map;
/**
* 个人信息
*/
@RestController
public class AdminInfoController {
@Autowired
private IAdminService adminService;
@ApiOperation(value = "更新当前用户信息")
@PutMapping("/admin/info")
public RespBean updateAdmin(@RequestBody Admin admin, Authentication authentication) {
//更新成功,重新构建Authentication对象
if (adminService.updateById(admin)) {
/**
* 1.用户对象
* 2.凭证(密码)
* 3.用户角色
*/
SecurityContextHolder.getContext().setAuthentication(new
UsernamePasswordAuthenticationToken(admin,
authentication.getCredentials(),
authentication.getAuthorities()));
return RespBean.success("更新成功!");
}
return RespBean.error("更新失败!");
}
@ApiOperation(value = "更新用户密码")
@PutMapping("/admin/pass")
public RespBean updateAdminPassword(@RequestBody Map<String,Object> info){
String oldPass = (String) info.get("oldPass");
String pass = (String) info.get("pass");
Integer adminId = (Integer) info.get("adminId");
return adminService.updatePassword(oldPass,pass,adminId);
}
}
2.service 更改密码:
/**
* 更新用户密码
*
* @param oldPass
* @param pass
* @param adminId
* @return
*/
@Override
public RespBean updatePassword(String oldPass, String pass, Integer adminId) {
Admin admin = adminMapper.selectById(adminId);
//铭文加密密码,需要转换
BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
if (encoder.matches(oldPass, admin.getPassword())) {
admin.setPassword(encoder.encode(pass));
int result = adminMapper.updateById(admin);
if (1 == result) {
return RespBean.success("更新成功!");
}
}
return RespBean.error("更新失败!");
}
三、Bug解决
当我们更新个人信息同时附带个人权限,会发现报错。
原因:这是我们的 Admin 实体类实现了 UserDetails 接口,重写了 getAuthorities() 方法,但是Admin 实体类却没有对应的 Collection<? extends GrantedAuthority> 属性,也无法创建含有Collection<? extends GrantedAuthority> 属性的构造函数。JSON无法进行反序列化,导致报错
解决:自定义反序列化类,在 getAuthorities() 方法定义使用自定义的反序列化类进行
1.CustomAuthorityDeserializer
package com.xxxx.server.config;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import java.io.IOException;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
/**
* 自定义Authority解析器
*
* @author zhoubin
*/
public class CustomAuthorityDeserializer extends JsonDeserializer {
@Override
public Object deserialize(JsonParser jp, DeserializationContext ctxt)
throws IOException {
ObjectMapper mapper = (ObjectMapper) jp.getCodec();
JsonNode jsonNode = mapper.readTree(jp);
List<GrantedAuthority> grantedAuthorities = new LinkedList<>();
Iterator<JsonNode> elements = jsonNode.elements();
while (elements.hasNext()) {
JsonNode next = elements.next();
JsonNode authority = next.get("authority");
grantedAuthorities.add(new
SimpleGrantedAuthority(authority.asText()));
}
return grantedAuthorities;
}
}