栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

040-云E办

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

040-云E办

040-云E办_ 个人中心

一、个人中心二、个人中心操作

controller 修改信息和密码service 更改密码: 三、Bug解决

CustomAuthorityDeserializerpojo/admin

一、个人中心

在普通项目中需要获取当前登录用户的信息,一般做法是在登录成功后,将当前用户信息存入session中。在需要使用当前用户的时候从session里面读取。更新用户信息也是直接通过数据库进行相应的更
新。

在Spring Security中要如何获取用户信息和更新用户信息呢?
在Spring Security中提供了一个 Authentication 对象,我们可以在 Controller 或者 Service 中,直接注入 Authentication ,注入成功后,就能直接使用。这样我们就能通过 Authentication 对象直接获取用户信息。

在Spring Security中更新用户信息,除了正常的去数据库进行相应的更新之外,还需要重新构建Authentication 对象,这样才能在项目中正确的获取到更新后的用户信息。具体代码如下

 
二、个人中心操作 
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)) {
            
            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 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);
    }
}
service 更改密码:
 
    @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 属性,也无法创建含有Collection 属性的构造函数。JSON无法进行反序列化,导致报错

解决:自定义反序列化类,在 getAuthorities() 方法定义使用自定义的反序列化类进行

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;

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 grantedAuthorities = new linkedList<>();
        Iterator elements = jsonNode.elements();
        while (elements.hasNext()) {
            JsonNode next = elements.next();
            JsonNode authority = next.get("authority");
            grantedAuthorities.add(new
                    SimpleGrantedAuthority(authority.asText()));
        }
        return grantedAuthorities;
    }
}
pojo/admin

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/760273.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号