1. 调整 新增修改时 添加 create_by 以及 update_By
This commit is contained in:
parent
14c4218f17
commit
f0a3a39f57
|
|
@ -30,6 +30,10 @@ public interface SecurityConstants {
|
|||
* 用户名字段
|
||||
*/
|
||||
String DETAILS_USERNAME = "username";
|
||||
/**
|
||||
* 登录端
|
||||
*/
|
||||
String APP_CODE = "appCode";
|
||||
|
||||
/**
|
||||
* 授权信息字段
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
package com.chushang.security.context;
|
||||
package com.chushang.common.core.context;
|
||||
|
||||
import com.alibaba.ttl.TransmittableThreadLocal;
|
||||
import com.chushang.common.core.constant.SecurityConstants;
|
||||
|
|
@ -0,0 +1,38 @@
|
|||
//
|
||||
// Source code recreated from a .class file by IntelliJ IDEA
|
||||
// (powered by FernFlower decompiler)
|
||||
//
|
||||
|
||||
package com.chushang.common.core.enums;
|
||||
|
||||
import com.chushang.common.core.exception.ResultException;
|
||||
import lombok.Getter;
|
||||
|
||||
@Getter
|
||||
public enum PassCodeEnum {
|
||||
PUBLIC("PUBLIC", "公开", false),
|
||||
PLATFORM("PLATFORM", "管理平台", true),
|
||||
MINI("MINI", "小程序", true),
|
||||
APP("APP", "应用", true),
|
||||
;
|
||||
|
||||
private final String code;
|
||||
private final String name;
|
||||
private final Boolean openReg;
|
||||
|
||||
private PassCodeEnum(String code, String name, boolean openReg) {
|
||||
this.code = code;
|
||||
this.name = name;
|
||||
this.openReg = openReg;
|
||||
}
|
||||
|
||||
public static PassCodeEnum getEnumByCode(String code) {
|
||||
PassCodeEnum[] var1 = values();
|
||||
for (PassCodeEnum e : var1) {
|
||||
if (e.getCode().equals(code)) {
|
||||
return e;
|
||||
}
|
||||
}
|
||||
throw new ResultException("非法的应用");
|
||||
}
|
||||
}
|
||||
|
|
@ -9,7 +9,7 @@ import com.chushang.datascope.annotation.DataScope;
|
|||
import com.chushang.datascope.constants.ScopeConstants;
|
||||
import com.chushang.datascope.entity.DataScopeEntity;
|
||||
import com.chushang.datascope.enums.ScopeKeyWord;
|
||||
import com.chushang.security.context.SecurityContextHolder;
|
||||
import com.chushang.common.core.context.SecurityContextHolder;
|
||||
import com.chushang.security.utils.SecurityUtils;
|
||||
import com.chushang.security.entity.vo.LoginUser;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ package com.chushang.common.mybatis.config;
|
|||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
|
||||
import com.chushang.common.core.context.SecurityContextHolder;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.ibatis.reflection.MetaObject;
|
||||
import org.springframework.util.ClassUtils;
|
||||
|
|
@ -23,7 +24,9 @@ public class MybatisPlusMetaObjectHandler implements MetaObjectHandler {
|
|||
log.debug("mybatis plus start insert fill ....");
|
||||
}
|
||||
LocalDateTime now = LocalDateTime.now();
|
||||
|
||||
// 修改人, 创建人
|
||||
Long userId = SecurityContextHolder.getUserId();
|
||||
fillValIfNullByName("createBy", userId, metaObject, true);
|
||||
fillValIfNullByName("createTime", now, metaObject, false);
|
||||
fillValIfNullByName("updateTime", now, metaObject, false);
|
||||
}
|
||||
|
|
@ -32,6 +35,8 @@ public class MybatisPlusMetaObjectHandler implements MetaObjectHandler {
|
|||
public void updateFill(MetaObject metaObject) {
|
||||
log.debug("mybatis plus start update fill ....");
|
||||
fillValIfNullByName("updateTime", LocalDateTime.now(), metaObject, true);
|
||||
Long userId = SecurityContextHolder.getUserId();
|
||||
fillValIfNullByName("updateBy", userId, metaObject, true);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@ package com.chushang.security.auth;
|
|||
import com.chushang.security.annotation.Logical;
|
||||
import com.chushang.security.annotation.RequiresPermissions;
|
||||
import com.chushang.security.annotation.RequiresRoles;
|
||||
import com.chushang.security.context.SecurityContextHolder;
|
||||
import com.chushang.common.core.context.SecurityContextHolder;
|
||||
import com.chushang.security.service.TokenService;
|
||||
import com.chushang.security.utils.SecurityUtils;
|
||||
import com.chushang.common.core.exception.auth.NotLoginException;
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
package com.chushang.security.interceptor;
|
||||
|
||||
import com.chushang.security.auth.AuthUtil;
|
||||
import com.chushang.security.context.SecurityContextHolder;
|
||||
import com.chushang.common.core.context.SecurityContextHolder;
|
||||
import com.chushang.security.utils.SecurityUtils;
|
||||
import com.chushang.common.core.constant.SecurityConstants;
|
||||
import com.chushang.common.core.util.ServletUtils;
|
||||
|
|
|
|||
|
|
@ -39,7 +39,7 @@ public class TokenService
|
|||
/**
|
||||
* 创建令牌
|
||||
*/
|
||||
public Map<String, Object> createToken(LoginUser loginUser)
|
||||
public Map<String, Object> createToken(LoginUser loginUser, String appCode)
|
||||
{
|
||||
String token = IdUtils.getId(31);
|
||||
Long userId = loginUser.getUserId();
|
||||
|
|
@ -55,6 +55,7 @@ public class TokenService
|
|||
claimsMap.put(SecurityConstants.USER_KEY, tokenKey);
|
||||
claimsMap.put(SecurityConstants.DETAILS_USER_ID, userId);
|
||||
claimsMap.put(SecurityConstants.DETAILS_USERNAME, username);
|
||||
claimsMap.put(SecurityConstants.APP_CODE, appCode);
|
||||
|
||||
// 接口返回信息
|
||||
Map<String, Object> rspMap = new HashMap<>();
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
package com.chushang.security.utils;
|
||||
|
||||
import com.chushang.security.context.SecurityContextHolder;
|
||||
import com.chushang.common.core.context.SecurityContextHolder;
|
||||
import com.chushang.common.core.constant.SecurityConstants;
|
||||
import com.chushang.common.core.constant.TokenConstants;
|
||||
import com.chushang.common.core.util.ServletUtils;
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
package com.chushang.auth.controller;
|
||||
|
||||
import com.chushang.auth.service.UserService;
|
||||
import com.chushang.common.core.enums.PassCodeEnum;
|
||||
import com.chushang.common.core.util.JwtUtils;
|
||||
import com.chushang.common.core.util.StringUtils;
|
||||
import com.chushang.common.core.web.AjaxResult;
|
||||
|
|
@ -31,13 +32,40 @@ public class UserController
|
|||
final TokenService tokenService;
|
||||
final UserService userService;
|
||||
|
||||
/**
|
||||
* 后台登录
|
||||
*/
|
||||
@PostMapping("login")
|
||||
public AjaxResult login(@RequestBody LoginBody form)
|
||||
{
|
||||
// 用户登录
|
||||
LoginUser<SysUser, DataScopeEntity> loginUser = userService.login(form.getUsername(), form.getPassword());
|
||||
LoginUser<SysUser, DataScopeEntity> loginUser = userService.login(form.getUsername(), form.getPassword(), PassCodeEnum.PLATFORM.getCode());
|
||||
// 获取登录token
|
||||
return AjaxResult.success(tokenService.createToken(loginUser));
|
||||
return AjaxResult.success(tokenService.createToken(loginUser, PassCodeEnum.PLATFORM.getCode()));
|
||||
}
|
||||
|
||||
/**
|
||||
* 前台登录
|
||||
*/
|
||||
@PostMapping("/app/login")
|
||||
public AjaxResult appLogin(@RequestBody LoginBody form)
|
||||
{
|
||||
// 用户登录
|
||||
LoginUser<SysUser, DataScopeEntity> loginUser = userService.login(form.getUsername(), form.getPassword(), PassCodeEnum.APP.getCode());
|
||||
// 获取登录token
|
||||
return AjaxResult.success(tokenService.createToken(loginUser, PassCodeEnum.APP.getCode()));
|
||||
}
|
||||
|
||||
/**
|
||||
* 小程序登录
|
||||
*/
|
||||
@PostMapping("/mini/login")
|
||||
public AjaxResult miniLogin(@RequestBody LoginBody form)
|
||||
{
|
||||
// 用户登录
|
||||
LoginUser<SysUser, DataScopeEntity> loginUser = userService.login(form.getUsername(), form.getPassword(), PassCodeEnum.MINI.getCode());
|
||||
// 获取登录token
|
||||
return AjaxResult.success(tokenService.createToken(loginUser, PassCodeEnum.MINI.getCode()));
|
||||
}
|
||||
|
||||
@DeleteMapping("logout")
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
package com.chushang.auth.service;
|
||||
|
||||
import com.chushang.common.core.enums.PassCodeEnum;
|
||||
import com.chushang.common.core.constant.SecurityConstants;
|
||||
import com.chushang.common.core.exception.ResultException;
|
||||
import com.chushang.common.core.util.IPUtils;
|
||||
|
|
@ -34,8 +35,10 @@ public class UserService {
|
|||
/**
|
||||
* 登录
|
||||
*/
|
||||
public LoginUser<SysUser, DataScopeEntity> login(String username, String password)
|
||||
public LoginUser<SysUser, DataScopeEntity> login(String username, String password, String appCode)
|
||||
{
|
||||
PassCodeEnum pce = PassCodeEnum.getEnumByCode(appCode);
|
||||
if (pce.getOpenReg()) {
|
||||
// 查询用户信息
|
||||
long start = System.currentTimeMillis();
|
||||
Result<LoginUser<SysUser, DataScopeEntity>> rLoginUser = remoteUserService.getUserInfo(username, SecurityConstants.INNER);
|
||||
|
|
@ -44,13 +47,17 @@ public class UserService {
|
|||
if (Result.FAIL_CODE == rLoginUser.getCode()){
|
||||
throw new ResultException(rLoginUser.getMsg());
|
||||
}
|
||||
|
||||
if (ObjectUtils.isEmpty(rLoginUser) || ObjectUtils.isEmpty(rLoginUser.getData()) ){
|
||||
recordLoginInfo(username, LoginStatusEnum.ACCOUNT_EMPTY);
|
||||
throw new ResultException("登录用户:" + username + " 不存在");
|
||||
}
|
||||
LoginUser<SysUser, DataScopeEntity> loginUser = rLoginUser.getData();
|
||||
SysUser sysUser = loginUser.getSysUser();
|
||||
Boolean isPlatform = sysUser.getIsPlatform();
|
||||
if (!isPlatform && !PassCodeEnum.PLATFORM.equals(pce)){
|
||||
recordLoginInfo(username, LoginStatusEnum.ACCOUNT_TYPE_ERROR);
|
||||
throw new ResultException("对不起,您的账号:" + username + " 不是平台账号");
|
||||
}
|
||||
Boolean status = sysUser.getStatus();
|
||||
if (!status)
|
||||
{
|
||||
|
|
@ -66,6 +73,9 @@ public class UserService {
|
|||
recordLoginInfo(username, LoginStatusEnum.LOGIN_SUCCESS);
|
||||
loginUser.setSysUser(sysUser);
|
||||
return loginUser;
|
||||
} else {
|
||||
throw new ResultException("非法请求端");
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
|
|
|||
|
|
@ -84,6 +84,11 @@ public class AuthFilter implements GlobalFilter, Ordered
|
|||
}
|
||||
String userid = JwtUtils.getUserId(claims);
|
||||
String username = JwtUtils.getUserName(claims);
|
||||
// appCode 用于区分 小程序,app以及平台请求, 应当不同的请求有不同的code, 根据code 判断是否允许创建
|
||||
String appCode = JwtUtils.getValue(claims, SecurityConstants.APP_CODE);
|
||||
// todo 此处应该根据appCode 判断url 是否允许
|
||||
log.info("appCode {}", appCode);
|
||||
|
||||
if (StringUtils.isEmpty(userid) || StringUtils.isEmpty(username))
|
||||
{
|
||||
return unauthorizedResponse(exchange, "令牌验证失败");
|
||||
|
|
@ -93,6 +98,7 @@ public class AuthFilter implements GlobalFilter, Ordered
|
|||
addHeader(mutate, SecurityConstants.USER_KEY, userkey);
|
||||
addHeader(mutate, SecurityConstants.DETAILS_USER_ID, userid);
|
||||
addHeader(mutate, SecurityConstants.DETAILS_USERNAME, username);
|
||||
addHeader(mutate, SecurityConstants.APP_CODE, appCode);
|
||||
// 内部请求来源参数清除
|
||||
removeHeader(mutate);
|
||||
|
||||
|
|
|
|||
|
|
@ -20,6 +20,7 @@ public enum LoginStatusEnum implements IEnum<Integer> {
|
|||
ACCOUNT_EMPTY(3, "登录用户不存在"),
|
||||
ACCOUNT_STATUS_ERROR(4, "用户已停用,请联系管理员"),
|
||||
ACCOUNT_PASS_ERROR(5, "用户密码错误"),
|
||||
ACCOUNT_TYPE_ERROR(7, "非平台账号登录"),
|
||||
;
|
||||
|
||||
@JsonValue
|
||||
|
|
|
|||
|
|
@ -88,9 +88,10 @@ public class SysUser extends BaseEntity {
|
|||
*/
|
||||
@TableField(updateStrategy = FieldStrategy.NOT_NULL)
|
||||
private String updateBy;
|
||||
|
||||
|
||||
private String deptName;
|
||||
/**
|
||||
* 是否可以登录平台, true 可以, false 不可以, 默认true
|
||||
*/
|
||||
private Boolean isPlatform;
|
||||
|
||||
@TableField(exist = false)
|
||||
private SysDept dept;
|
||||
|
|
|
|||
|
|
@ -17,6 +17,7 @@
|
|||
<result property="nickName" column="nick_name"/>
|
||||
<result property="gender" column="gender"/>
|
||||
<result property="avatar" column="avatar"/>
|
||||
<result property="isPlatform" column="is_platform"/>
|
||||
<association property="dept" resultMap="deptResult"/>
|
||||
<collection property="roles" javaType="java.util.List" resultMap="RoleResult"/>
|
||||
</resultMap>
|
||||
|
|
@ -41,7 +42,7 @@
|
|||
|
||||
<sql id="selectUserVo">
|
||||
select u.user_id, u.dept_id, u.username, u.password, u.status, u.del_state, u.create_by, u.create_time,u.update_time,u.salt,
|
||||
u.phone,u.nick_name,u.gender,u.avatar,u.email,
|
||||
u.phone,u.nick_name,u.gender,u.avatar,u.email, u.is_platform,
|
||||
d.parent_dept_id, d.ancestors, d.dept_name, d.order_num as dept_order_num,d.status as dept_status,
|
||||
r.role_id, r.role_name, r.role_key,r.order_num as role_order_num, r.data_scope, r.status as role_status
|
||||
from sys_user u
|
||||
|
|
|
|||
Loading…
Reference in New Issue