获取组织ID修改为缓存中获取

This commit is contained in:
liuzhaotian
2024-05-21 11:56:20 +08:00
parent 2b4051ff9e
commit 13c4a3842b
3 changed files with 157 additions and 8 deletions
@@ -71,6 +71,12 @@
<groupId>com.github.yulichang</groupId>
<artifactId>mybatis-plus-join-boot-starter</artifactId> <!-- MyBatis 联表查询 -->
</dependency>
<!-- redis 相关 -->
<dependency>
<groupId>com.cf.imes</groupId>
<artifactId>cf-spring-boot-starter-redis</artifactId>
</dependency>
</dependencies>
</project>
@@ -0,0 +1,81 @@
package com.cf.imes.framework.mybatis;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler;
import com.cf.imes.framework.common.enums.UserTypeEnum;
import lombok.Data;
import lombok.EqualsAndHashCode;
import java.time.LocalDateTime;
import java.util.List;
@Data
public class OAuth2AccessTokenDO {
/**
* 编号,数据库递增
*/
private Long id;
/**
* 访问令牌
*/
private String accessToken;
/**
* 刷新令牌
*/
private String refreshToken;
/**
* 用户编号
*/
private Long userId;
/**
* 用户类型
*
* 枚举 {@link UserTypeEnum}
*/
private Integer userType;
/**
* 客户端编号
*
* 关联 {@link OAuth2ClientDO#getId()}
*/
private String clientId;
/**
* 授权范围
*/
private List<String> scopes;
/**
* 过期时间
*/
private LocalDateTime expiresTime;
/**
* 是否大型数据库
*/
private Boolean large;
/**
* 数据库编号
*/
private Integer dbNo;
/**
* 数据表编号
*/
private Integer tableNo;
/**
* 数据源编码
*/
private String dataCode;
/**
* 组织id
*/
private Long organId;
/**
* 用户昵称
*/
private String nickname;
/**
* 是否超级管理员
*/
private Boolean isSupAdmin;
}
@@ -1,11 +1,18 @@
package com.cf.imes.framework.mybatis.core.handler;
import cn.hutool.core.util.StrUtil;
import com.cf.imes.framework.common.util.json.JsonUtils;
import com.cf.imes.framework.mybatis.OAuth2AccessTokenDO;
import com.cf.imes.framework.mybatis.core.dataobject.BaseDO;
import com.cf.imes.framework.web.core.util.WebFrameworkUtils;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.util.StringUtils;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.Objects;
@@ -20,9 +27,16 @@ import java.util.Objects;
*/
public class DefaultDBFieldHandler implements MetaObjectHandler {
@Resource
private StringRedisTemplate stringRedisTemplate;
@Autowired
private HttpServletRequest request;
private static final String AUTHORIZATION_BEARER = "Bearer";
private static final String OAUTH2_ACCESS_TOKEN = "oauth2_access_token:%s";
@Override
public void insertFill(MetaObject metaObject) {
@@ -51,19 +65,31 @@ public class DefaultDBFieldHandler implements MetaObjectHandler {
baseDO.setUpdater(loginUserName);
}
}
// 获取当前用户登录的组织ID
Long organId = WebFrameworkUtils.getOrganId(request);
// 从请求中获取到 Token
String authorization = obtainAuthorization(request, "Authorization", "token");
this.strictInsertFill(metaObject, "organId", Long.class, organId);
Long organId = null;
/**
* 用户首次密码登陆的时候,不会有token带入,直接捕捉异常抛出即可,组织ID后续会根据用户登录的组织进行填入,
* 这里的组织ID字段填入即使为空也可,后续的请求中都会带有token,再从缓存中获取到 token 数据,再获取组织ID,填入即可
*/
try {
// 获取 token 对应的缓存数据,并得到组织 ID
OAuth2AccessTokenDO oAuth2AccessTokenDO = get(authorization);
organId = oAuth2AccessTokenDO.getOrganId();
}catch (Exception e){
}
this.strictInsertFill(metaObject, "organId", Long.class, organId );
}
@Override
public void updateFill(MetaObject metaObject) {
// 更新时间为空,则以当前时间为更新时间
@@ -80,9 +106,45 @@ public class DefaultDBFieldHandler implements MetaObjectHandler {
setFieldValByName("updater", loginUserName, metaObject);
}
// 获取当前用户登录的组织ID
Long organId = WebFrameworkUtils.getOrganId(request);
// 从请求中获取到 Token
String authorization = obtainAuthorization(request, "Authorization", "token");
Long organId = null;
try {
// 获取 token 对应的缓存数据,并得到组织 ID
OAuth2AccessTokenDO oAuth2AccessTokenDO = get(authorization);
organId = oAuth2AccessTokenDO.getOrganId();
}catch (Exception e){
}
this.strictUpdateFill(metaObject, "organId", Long.class, organId );
this.strictUpdateFill(metaObject, "organId", Long.class, organId);
}
// 从请求中获取到 token
public static String obtainAuthorization(HttpServletRequest request,
String headerName, String parameterName) {
// 1. 获得 Token。优先级:Header > Parameter
String token = request.getHeader(headerName);
if (StrUtil.isEmpty(token)) {
token = request.getParameter(parameterName);
}
if (!StringUtils.hasText(token)) {
return null;
}
// 2. 去除 Token 中带的 Bearer
int index = token.indexOf(AUTHORIZATION_BEARER + " ");
return index >= 0 ? token.substring(index + 7).trim() : token;
}
// 从缓存中获取到缓存的数据
public OAuth2AccessTokenDO get(String accessToken) {
String redisKey = String.format(OAUTH2_ACCESS_TOKEN, accessToken);
return JsonUtils.parseObject(stringRedisTemplate.opsForValue().get(redisKey), OAuth2AccessTokenDO.class);
}
}