Custom User-Zoyi Framework

Custom User Table Integration Ruoyi Framework

1. Create a new user table

2. Inherit the original entity of the system

@TableName("dd_user")
@Data
@Accessors(chain = true)
//继承原先的表并加上自己的字段
public class DdUserPO extends SysUser implements Serializable {
    
    
    private static final long serialVersionUID=1L;

    /**
     * 0 企业用户 1个人用户
     */
    @TableField(value = "user_type")
    private String userType;

    /**
     * 公司表id
     */
    @TableField(value = "company_id")
    private Long companyId;

    /**
     * 公司名称
     */
    @TableField(value = "company_name")
    private String companyName;

3. Customize the method of judging the password and generating token

/**
 * 用护端登录方法
 *
 * @param loginBody 登录信息
 * @return 结果
 */
@PostMapping("/user/login")
public BaseResult userLogin(@RequestBody LoginBody loginBody) {
    
    
    // 生成令牌
    String token = loginService.userLogin(loginBody.getUsername(), loginBody.getPassword(), loginBody.getCode(),
            loginBody.getUuid());
    Map<String, Object> resultMap = new HashMap<>();
    resultMap.put(Constants.TOKEN, token);
    return BaseResult.success(resultMap);
}
/**
 * 用户端登录验证
 *
 * @param username 用户名
 * @param password 密码
 * @param code     验证码
 * @param uuid     唯一标识
 * @return 结果
 */
public String userLogin(String username, String password, String code, String uuid) {
    
    
    //验证用户名密码
    DdUserPO userPO=new DdUserPO();
    userPO.setUserName(username);
    DdUserPO user = userMapper.checkPwd(userPO);
    if (user==null || !SecurityUtils.matchesPassword(password,user.getPassword())) throw new ServiceException("用户名或密码错误");
    LoginUser loginUser = new LoginUser(user.getUserId(),user.getDeptId(),user,null); //自己组装需要的LoginUser
    recordLoginInfo(loginUser.getUserId());
    // 生成token
    return tokenService.createToken(loginUser);
}

4. Customize the menu method

/**
 * 获取用户路由信息
 *
 * @return 路由信息
 */
@GetMapping("getUserRouters")
public BaseResult getUserRouters() {
    
    
    List<SysMenu> menus = menuService.selectAllMenu(); //这里自己写sql查询自己需要的菜单列表
    return BaseResult.success(menuService.buildMenus(menus));
}

5. The login call method of the front-end modification framework

6. Delete the permission verification annotation @PreAuthorize

/**
 * 新增company
 *
 * @param ddTrainDetailCompanyVO 参数对象
 * @return 通用数据对象
 */
@ApiImplicitParams({
    
    
        @ApiImplicitParam(paramType = "body", dataType = "DdTrainDetailCompanyVO", name = "ddTrainDetailCompanyVO", value = "参数对象")
})
@ApiOperation(value = "新增公司课程信息", notes = "新增公司课程信息", httpMethod = "POST")
//@PreAuthorize("@ss.hasPermi('train:company:add')") //删除这个注解
@Log(title = "company", businessType = BusinessType.INSERT)
@PostMapping
public BaseResult add(@RequestBody @Validated({
    
    Insert.class}) DdTrainDetailCompanyVO ddTrainDetailCompanyVO) {
    
    
    DdTrainDetailCompanyPO ddTrainDetailCompanyPO = DdTrainDetailCompanyMapstruct.INSTANCE.vo2po(ddTrainDetailCompanyVO);
    return BaseResult.success(ddTrainDetailCompanyService.save(ddTrainDetailCompanyPO));
}

You can also modify the logic here

/**
 * 验证用户是否具备某权限
 *
 * @param permission 权限字符串
 * @return 用户是否具备某权限
 */
public boolean hasPermi(String permission) {
    
    
    if (StringUtils.isEmpty(permission)) {
    
    
        return false;
    }
    LoginUser loginUser = SecurityUtils.getLoginUser();
    if (StringUtils.isNull(loginUser) || CollectionUtils.isEmpty(loginUser.getPermissions())) {
    
    
        return false;
    }
    return hasPermissions(loginUser.getPermissions(), permission);
}

おすすめ

転載: blog.csdn.net/weixin_52016779/article/details/125927789