新增 用户登录与用户信息加载
This commit is contained in:
parent
85b96101b3
commit
7dfb83cff9
@ -2,6 +2,7 @@ package com.guaiguailang.harmony;
|
||||
|
||||
import com.github.yitter.contract.IdGeneratorOptions;
|
||||
import com.github.yitter.idgen.YitIdHelper;
|
||||
import org.mybatis.spring.annotation.MapperScan;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
|
@ -2,6 +2,7 @@ package com.guaiguailang.harmony.config;
|
||||
|
||||
import cn.dev33.satoken.stp.StpInterface;
|
||||
import com.guaiguailang.harmony.mapper.AuthMapper;
|
||||
import com.guaiguailang.harmony.mapper.UserMapper;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
@ -18,6 +19,8 @@ import java.util.Map;
|
||||
public class StpInterfaceImpl implements StpInterface {
|
||||
@Autowired
|
||||
AuthMapper authMapper;
|
||||
@Autowired
|
||||
UserMapper userMapper;
|
||||
|
||||
/**
|
||||
* 返回一个账号所拥有的权限码集合
|
||||
@ -61,7 +64,7 @@ public class StpInterfaceImpl implements StpInterface {
|
||||
public List<String> getRoleList(Object loginId, String loginType) {
|
||||
Long uid = (Long)loginId;//得到用戶id
|
||||
List<String> list = new ArrayList<String>();
|
||||
list.add(authMapper.getUserById(uid).getRoleId());
|
||||
list.add(userMapper.getUserById(uid).getRoleId());
|
||||
log.info("用户{}角色:{}", uid.toString(), list.toString());
|
||||
return list;
|
||||
}
|
||||
|
@ -1,5 +1,7 @@
|
||||
package com.guaiguailang.harmony.controller;
|
||||
|
||||
import cn.dev33.satoken.stp.StpUtil;
|
||||
import cn.dev33.satoken.util.SaResult;
|
||||
import com.guaiguailang.harmony.domain.dto.ParamLogin;
|
||||
import com.guaiguailang.harmony.domain.vo.ResponseResult;
|
||||
import com.guaiguailang.harmony.service.AuthService;
|
||||
@ -69,6 +71,20 @@ public class AuthController {
|
||||
)
|
||||
@PostMapping("/login")
|
||||
public ResponseEntity loginByAccount(@RequestBody ParamLogin loginParam) throws NoSuchAlgorithmException {
|
||||
/*
|
||||
测试账号:xrilang
|
||||
测试密码:e10adc3949ba59abbe56e057f20f883e
|
||||
*/
|
||||
return ResponseEntity.ok(authService.loginByAccount(loginParam));
|
||||
}
|
||||
@Operation(
|
||||
summary = "用户注销 退出登录",
|
||||
description = "用户退出登录",
|
||||
tags = {"系统认证接口"}
|
||||
)
|
||||
@PostMapping("/logout")
|
||||
public ResponseEntity logout() {
|
||||
StpUtil.logout();
|
||||
return ResponseEntity.ok().build();
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,31 @@
|
||||
package com.guaiguailang.harmony.controller;
|
||||
|
||||
import com.guaiguailang.harmony.domain.dto.ParamLogin;
|
||||
import com.guaiguailang.harmony.service.UserService;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
|
||||
@RestController
|
||||
@Tag(name="用户相关接口")
|
||||
@RequestMapping("/user")
|
||||
public class UserController {
|
||||
@Autowired
|
||||
private UserService userService;
|
||||
@Operation(
|
||||
summary = "用户信息",
|
||||
description = "包含了用户权限的用户信息",
|
||||
tags = {"用户相关接口"}
|
||||
)
|
||||
@GetMapping("/info")
|
||||
public ResponseEntity loginByAccount(){
|
||||
return ResponseEntity.ok(userService.getUserInfo());
|
||||
}
|
||||
}
|
@ -1,57 +1,12 @@
|
||||
package com.guaiguailang.harmony.domain.entity;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class SystemAction {
|
||||
|
||||
private long id;
|
||||
private Long id;
|
||||
private String tag;
|
||||
private String describe;
|
||||
private long defaultCheck;
|
||||
private long status;
|
||||
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
|
||||
public String getTag() {
|
||||
return tag;
|
||||
}
|
||||
|
||||
public void setTag(String tag) {
|
||||
this.tag = tag;
|
||||
}
|
||||
|
||||
|
||||
public String getDescribe() {
|
||||
return describe;
|
||||
}
|
||||
|
||||
public void setDescribe(String describe) {
|
||||
this.describe = describe;
|
||||
}
|
||||
|
||||
|
||||
public long getDefaultCheck() {
|
||||
return defaultCheck;
|
||||
}
|
||||
|
||||
public void setDefaultCheck(long defaultCheck) {
|
||||
this.defaultCheck = defaultCheck;
|
||||
}
|
||||
|
||||
|
||||
public long getStatus() {
|
||||
return status;
|
||||
}
|
||||
|
||||
public void setStatus(long status) {
|
||||
this.status = status;
|
||||
}
|
||||
|
||||
private Boolean defaultCheck;
|
||||
private int status;
|
||||
}
|
||||
|
@ -8,5 +8,6 @@ public class SystemPermissionAction {
|
||||
private long id;
|
||||
private String actionTag;
|
||||
private String permissionId;
|
||||
private long status;
|
||||
private int status;
|
||||
private Long uid;
|
||||
}
|
||||
|
@ -1,47 +1,12 @@
|
||||
package com.guaiguailang.harmony.domain.entity;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class SystemRole {
|
||||
|
||||
private long id;
|
||||
private String roleName;
|
||||
private String roleId;
|
||||
private long status;
|
||||
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
|
||||
public String getRoleName() {
|
||||
return roleName;
|
||||
}
|
||||
|
||||
public void setRoleName(String roleName) {
|
||||
this.roleName = roleName;
|
||||
}
|
||||
|
||||
|
||||
public String getRoleId() {
|
||||
return roleId;
|
||||
}
|
||||
|
||||
public void setRoleId(String roleId) {
|
||||
this.roleId = roleId;
|
||||
}
|
||||
|
||||
|
||||
public long getStatus() {
|
||||
return status;
|
||||
}
|
||||
|
||||
public void setStatus(long status) {
|
||||
this.status = status;
|
||||
}
|
||||
|
||||
private String description;
|
||||
}
|
||||
|
@ -0,0 +1,10 @@
|
||||
package com.guaiguailang.harmony.domain.vo;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class ActionEntitySet {
|
||||
private String action;
|
||||
private String describe;
|
||||
private boolean defaultCheck;
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
package com.guaiguailang.harmony.domain.vo;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
public class PermissionInfo {
|
||||
private String roleId;//角色id 文本型
|
||||
private String permissionId;// 权限di,文本型
|
||||
private String permissionName;// 权限名称,文本型
|
||||
private List<ActionEntitySet> actionEntitySet;
|
||||
private List actionList;
|
||||
private Object dataAccess;//object类型是因为还没确定好它的内容类型
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
package com.guaiguailang.harmony.domain.vo;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
public class RoleInfo {
|
||||
private String id;//roleId是文本型
|
||||
private String name;//显示的角色名称
|
||||
private String description;
|
||||
private int status;
|
||||
private String creatorId;
|
||||
private LocalDateTime createTime;
|
||||
private int deleted;
|
||||
private List<PermissionInfo> permissions;
|
||||
}
|
@ -30,8 +30,8 @@ public class UserInfoLogin{
|
||||
private String creator;
|
||||
// 角色权限
|
||||
private String merchantCode;//商户编号
|
||||
// private String roleId;
|
||||
private String role;//角色 如果是一个人有多个角色,此处应当是列表,但是本系统设定一人一个角色,就算多角色,登录的时候就要选择角色登录
|
||||
private String roleId;
|
||||
private RoleInfo role;//角色 如果是一个人有多个角色,此处应当是列表,但是本系统设定一人一个角色,就算多角色,登录的时候就要选择角色登录
|
||||
// token 相关
|
||||
private String token;
|
||||
private LocalDateTime expireTime;
|
||||
|
@ -16,14 +16,27 @@ public interface AuthMapper {
|
||||
// 有没有想过注解写SQL的方式好不好,对比xml写SQL的方式哪种更好?
|
||||
// 与其说好不好,不如说 合不合适。简单SQL写注解,方便又省事;复杂SQL写xml,高效且清晰;就这样。
|
||||
|
||||
@Select("SELECT * from user_info where id=#{id} AND status=1")
|
||||
UserInfo getUserById(@Param("id") Long id);
|
||||
|
||||
/**
|
||||
* 获取用户权限列表
|
||||
* @param id 用户id
|
||||
* @return List<SystemPermission> 用户权限列表
|
||||
*/
|
||||
List<SystemPermission> getUserPermissionList(@Param("id") Long id);
|
||||
|
||||
List<SystemAction> getPermissionActionList(String permissionId);
|
||||
|
||||
List<Map<String, Object>> getAllPermissionsAndActionsByUserId(Long uid);
|
||||
@Select("SELECT * from user_info where username=#{username} AND status=1")
|
||||
UserInfo getUserByAccount(String username);
|
||||
@Select("SELECT * from system_role where role_id=#{roleId} AND status=1")
|
||||
SystemRole getUserRoleById(@Param("roleId") String roleId);
|
||||
/**
|
||||
* 根据权限ID获取动作列表
|
||||
* @param permissionId 权限ID
|
||||
* @return 动作列表
|
||||
*/
|
||||
List<SystemAction> getActionByPermissionId(@Param("permissionId") String permissionId);
|
||||
// @Select("SELECT * from system_role_permission where role_id=#{roleId} AND status=1")
|
||||
List<SystemPermission> getUserPermissionListByRoleId(String roleId);
|
||||
|
||||
List<SystemAction> getActionByPermissionIdAndUid(String permissionId, Long uid);
|
||||
}
|
||||
|
@ -0,0 +1,14 @@
|
||||
package com.guaiguailang.harmony.mapper;
|
||||
|
||||
import com.guaiguailang.harmony.domain.entity.UserInfo;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
import org.apache.ibatis.annotations.Select;
|
||||
|
||||
@Mapper
|
||||
public interface UserMapper {
|
||||
@Select("SELECT * from user_info where id=#{id} AND status=1")
|
||||
UserInfo getUserById(@Param("id") Long id);
|
||||
@Select("SELECT * from user_info where username=#{username} AND status=1")
|
||||
UserInfo getUserByAccount(String username);
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
package com.guaiguailang.harmony.service;
|
||||
|
||||
import com.guaiguailang.harmony.domain.vo.ResponseResult;
|
||||
|
||||
public interface UserService {
|
||||
ResponseResult getUserInfo();
|
||||
}
|
@ -7,6 +7,7 @@ import com.guaiguailang.harmony.domain.entity.UserInfo;
|
||||
import com.guaiguailang.harmony.domain.vo.ResponseResult;
|
||||
import com.guaiguailang.harmony.domain.vo.UserInfoLogin;
|
||||
import com.guaiguailang.harmony.mapper.AuthMapper;
|
||||
import com.guaiguailang.harmony.mapper.UserMapper;
|
||||
import com.guaiguailang.harmony.service.AuthService;
|
||||
import com.guaiguailang.harmony.utils.PasswordEncryptor;
|
||||
import jdk.jshell.execution.Util;
|
||||
@ -23,9 +24,12 @@ import java.time.LocalDateTime;
|
||||
public class AuthServiceImpl implements AuthService {
|
||||
@Autowired
|
||||
private final AuthMapper authMapper;
|
||||
@Autowired
|
||||
private final UserMapper userMapper;
|
||||
|
||||
public AuthServiceImpl(AuthMapper authMapper) {
|
||||
public AuthServiceImpl(AuthMapper authMapper, UserMapper userMapper) {
|
||||
this.authMapper = authMapper;
|
||||
this.userMapper = userMapper;
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -33,7 +37,7 @@ public class AuthServiceImpl implements AuthService {
|
||||
|
||||
|
||||
// 根据账号查询用户
|
||||
UserInfo userInfo = authMapper.getUserByAccount(loginParam.getUsername());
|
||||
UserInfo userInfo = userMapper.getUserByAccount(loginParam.getUsername());
|
||||
if (userInfo == null) {
|
||||
return ResponseResult.error("用户不存在");
|
||||
}
|
||||
@ -57,7 +61,7 @@ public class AuthServiceImpl implements AuthService {
|
||||
BeanUtils.copyProperties(userInfo, userInfoLogin);
|
||||
userInfoLogin.setToken(token);
|
||||
userInfoLogin.setExpireTime(expireTime);
|
||||
userInfoLogin.setRole(userInfo.getRoleId());
|
||||
userInfoLogin.setRoleId(userInfo.getRoleId());
|
||||
|
||||
return ResponseResult.success(userInfoLogin);
|
||||
}
|
||||
|
@ -0,0 +1,83 @@
|
||||
package com.guaiguailang.harmony.service.impl;
|
||||
|
||||
import cn.dev33.satoken.stp.StpUtil;
|
||||
import com.guaiguailang.harmony.domain.entity.SystemAction;
|
||||
import com.guaiguailang.harmony.domain.entity.SystemPermission;
|
||||
import com.guaiguailang.harmony.domain.entity.SystemRole;
|
||||
import com.guaiguailang.harmony.domain.entity.UserInfo;
|
||||
import com.guaiguailang.harmony.domain.vo.*;
|
||||
import com.guaiguailang.harmony.mapper.AuthMapper;
|
||||
import com.guaiguailang.harmony.mapper.UserMapper;
|
||||
import com.guaiguailang.harmony.service.UserService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Slf4j
|
||||
@Service
|
||||
public class UserServiceImpl implements UserService {
|
||||
@Autowired
|
||||
private final UserMapper userMapper;
|
||||
@Autowired
|
||||
private final AuthMapper authMapper;
|
||||
|
||||
public UserServiceImpl(UserMapper userMapper, AuthMapper authMapper) {
|
||||
this.userMapper = userMapper;
|
||||
this.authMapper = authMapper;
|
||||
}
|
||||
// 如果存在大量的引用,有些可以使用字段注入的方式,以便构造函数过于臃肿
|
||||
|
||||
@Override
|
||||
public ResponseResult getUserInfo() {
|
||||
log.info("查询用户{}的用户信息", StpUtil.getLoginId()); // object类型 输出值1
|
||||
UserInfo userInfo = userMapper.getUserById(Long.parseLong((String) StpUtil.getLoginId()));// 报错 class java.lang.String cannot be cast to class java.lang.Long (java.lang.String and java.lang.Long are in module java.base of loader 'bootstrap')
|
||||
// 用户基本信息准备
|
||||
UserInfoLogin userInfoLogin = new UserInfoLogin();
|
||||
BeanUtils.copyProperties(userInfo, userInfoLogin);
|
||||
// 用户角色准备
|
||||
userInfoLogin.setRoleId(userInfo.getRoleId());
|
||||
SystemRole systemRole = authMapper.getUserRoleById(userInfo.getRoleId());
|
||||
RoleInfo roleInfo = new RoleInfo();
|
||||
// BeanUtils.copyProperties(systemRole, roleInfo);
|
||||
roleInfo.setId(systemRole.getRoleId());
|
||||
roleInfo.setName(systemRole.getRoleName());
|
||||
roleInfo.setDescription(systemRole.getDescription());
|
||||
//权限准备
|
||||
Long uid = userInfo.getId();
|
||||
// log.info("开始为用户{}准备权限。",uid);
|
||||
// List<SystemPermission> permissionInfoList = authMapper.getUserPermissionList(uid);
|
||||
List<SystemPermission> permissionInfoList = authMapper.getUserPermissionListByRoleId(userInfo.getRoleId());
|
||||
// log.info("权限列表({}):{}",permissionInfoList.size(),permissionInfoList.toString());
|
||||
List<PermissionInfo> permissionInfos = new ArrayList<>();
|
||||
int i = 0;
|
||||
for (SystemPermission permission : permissionInfoList) {
|
||||
log.info("得到的权限:{}", permission.toString());
|
||||
PermissionInfo permissionInfo = new PermissionInfo();
|
||||
permissionInfo.setRoleId(systemRole.getRoleId());
|
||||
permissionInfo.setPermissionId(permission.getPermissionId());
|
||||
permissionInfo.setPermissionName(permission.getPermissionName());
|
||||
// 开始获取action
|
||||
List<SystemAction> systemActionList = authMapper.getActionByPermissionIdAndUid(permission.getPermissionId(),userInfoLogin.getId());
|
||||
List<ActionEntitySet> actionEntitySets = new ArrayList<>();
|
||||
for (SystemAction systemAction : systemActionList) {
|
||||
ActionEntitySet actionEntitySet = new ActionEntitySet();
|
||||
actionEntitySet.setAction(systemAction.getTag());
|
||||
actionEntitySet.setDescribe(systemAction.getDescribe());
|
||||
actionEntitySet.setDefaultCheck(systemAction.getDefaultCheck());
|
||||
actionEntitySets.add(actionEntitySet);
|
||||
}
|
||||
permissionInfo.setActionEntitySet(actionEntitySets);
|
||||
// 更改计数值
|
||||
i++;
|
||||
permissionInfos.add(permissionInfo);
|
||||
}
|
||||
roleInfo.setPermissions(permissionInfos);
|
||||
userInfoLogin.setRole(roleInfo);
|
||||
log.info(userInfoLogin.toString());
|
||||
return ResponseResult.success(userInfoLogin);
|
||||
}
|
||||
}
|
@ -5,7 +5,7 @@
|
||||
<mapper namespace="com.guaiguailang.harmony.mapper.AuthMapper">
|
||||
|
||||
<!-- SQL for getting user permissions -->
|
||||
<select id="getUserPermissionList" parameterType="long" resultType="com.guaiguailang.harmony.domain.entity.SystemPermission">
|
||||
<select id="getUserPermissionList" resultType="com.guaiguailang.harmony.domain.entity.SystemPermission">
|
||||
SELECT *
|
||||
FROM system_role_permission srp
|
||||
INNER JOIN system_permission sp ON srp.permission_id = sp.id
|
||||
@ -15,7 +15,7 @@
|
||||
</select>
|
||||
|
||||
<!-- SQL for getting actions by permission id -->
|
||||
<select id="getPermissionActionList" parameterType="string" resultType="com.guaiguailang.harmony.domain.entity.SystemAction">
|
||||
<select id="getPermissionActionList" resultType="com.guaiguailang.harmony.domain.entity.SystemAction">
|
||||
SELECT sa.*
|
||||
FROM system_permission_action spa
|
||||
LEFT JOIN system_action sa ON spa.action_tag = sa.tag
|
||||
@ -23,13 +23,31 @@
|
||||
</select>
|
||||
|
||||
<!-- SQL for getting all permissions and actions by user id -->
|
||||
<select id="getAllPermissionsAndActionsByUserId" parameterType="long" resultType="map">
|
||||
<select id="getAllPermissionsAndActionsByUserId" resultType="map">
|
||||
SELECT sp.permission_id, sa.tag
|
||||
FROM user_info ui
|
||||
JOIN system_role_permission srp ON ui.role_id = srp.role_id
|
||||
JOIN system_permission sp ON srp.permission_id = sp.permission_id
|
||||
JOIN system_permission_action spa ON sp.permission_id = spa.permission_id
|
||||
JOIN system_action sa ON spa.action_tag = sa.tag
|
||||
WHERE ui.id = #{userId} AND ui.deleted = 0 AND srp.status = 1 AND spa.status = 1 AND sa.status = 1
|
||||
WHERE ui.id = #{userId} AND ui.deleted = 0 AND srp.status = 1 AND spa.status = 1 AND sa.status = 1 AND spa.uid=#{userId}
|
||||
</select>
|
||||
<!-- 获取指定权限ID下的动作列表 -->
|
||||
<select id="getActionByPermissionId" resultType="com.guaiguailang.harmony.domain.entity.SystemAction">
|
||||
SELECT *
|
||||
FROM system_action sa
|
||||
JOIN system_permission_action spa ON sa.tag= spa.action_tag
|
||||
WHERE spa.permission_id = #{permissionId} AND sa.status = 1
|
||||
</select>
|
||||
|
||||
<select id="getUserPermissionListByRoleId" resultType="com.guaiguailang.harmony.domain.entity.SystemPermission">
|
||||
select * from system_role_permission where role_id=#{roleId} AND status=1
|
||||
</select>
|
||||
<select id="getActionByPermissionIdAndUid" resultType="com.guaiguailang.harmony.domain.entity.SystemAction">
|
||||
SELECT *
|
||||
FROM system_action sa
|
||||
JOIN system_permission_action spa ON sa.tag = spa.action_tag
|
||||
WHERE spa.permission_id = #{permissionId} AND sa.status = 1 AND uid=#{uid}
|
||||
</select>
|
||||
|
||||
</mapper>
|
6
src/main/resources/mapper/UserMapper.xml
Normal file
6
src/main/resources/mapper/UserMapper.xml
Normal file
@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
|
||||
<mapper namespace="com.guaiguailang.harmony.mapper.UserMapper">
|
||||
</mapper>
|
@ -12,4 +12,8 @@ class HarmonyLifeServerApplicationTests {
|
||||
void contextLoads() {
|
||||
}
|
||||
|
||||
public void add(int a){
|
||||
System.out.println(a);
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user