beetlsql3-dev
Mlxa0324 2 years ago
parent e948415a7c
commit a6f7dcac80

@ -1,6 +1,7 @@
package com.ibeetl.admin.core.util;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.ibeetl.admin.core.conf.MVCConf;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
@ -18,12 +19,14 @@ import java.net.UnknownHostException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import static cn.hutool.core.util.ObjectUtil.defaultIfBlank;
import static com.ibeetl.admin.core.service.CorePlatformService.LOGIN_USER_TTL;
import static com.ibeetl.admin.core.service.CorePlatformService.TOKEN_KEY;
import static com.ibeetl.admin.core.util.servlet.ServletUtils.getRequest;
/**
@ -105,7 +108,7 @@ public class HttpRequestLocal {
if (headerToken == null) {
return null;
}
DefaultRedisMap<String, Object> redisMap = tokenRedisMap.get(headerToken);
DefaultRedisMap<String, Object> redisMap = tokenRedisMap.get(standardRedisTokenKey(headerToken));
if (redisMap == null) {
return null;
}
@ -117,7 +120,7 @@ public class HttpRequestLocal {
if (headerToken == null) {
return;
}
DefaultRedisMap<String, Object> redisMap = tokenRedisMap.get(headerToken);
DefaultRedisMap<String, Object> redisMap = tokenRedisMap.get(standardRedisTokenKey(headerToken));
if (redisMap == null) {
return ;
}
@ -131,6 +134,12 @@ public class HttpRequestLocal {
* @param obj
*/
public void setSessionValueByToken(String token, String key, Object obj){
// token 标准化
token = jwtTokenSplitTimestamp(Objects.requireNonNull(token))[0];
// token的二次加工用于分组
if (token != null) {
token = TOKEN_KEY + ":" + token;
}
DefaultRedisMap<String, Object> defaultRedisMap = tokenRedisMap.get(token);
if (defaultRedisMap == null) {
defaultRedisMap = new DefaultRedisMap<>(token, stringRedisTemplate);
@ -149,7 +158,7 @@ public class HttpRequestLocal {
if (headerToken == null) {
return;
}
DefaultRedisMap<String, Object> redisMap = tokenRedisMap.get(headerToken);
DefaultRedisMap<String, Object> redisMap = tokenRedisMap.get(standardRedisTokenKey(headerToken));
if (redisMap == null) {
return ;
}
@ -164,7 +173,7 @@ public class HttpRequestLocal {
if (headerToken == null) {
return;
}
tokenRedisMap.remove(headerToken);
tokenRedisMap.remove(standardRedisTokenKey(headerToken));
}
@ -197,13 +206,31 @@ public class HttpRequestLocal {
return token;
}
/**
* RedisTokenKey
* @param token
* @return
*/
public static String standardRedisTokenKey(String token) {
if (StrUtil.isBlank(token)) {
return "";
}
if (token.startsWith(TOKEN_KEY)) {
return token;
}
return TOKEN_KEY + ":" + token;
}
public Object getAttribute(String key){
String headerToken = getHeaderToken();
if (headerToken == null) {
return null;
}
DefaultRedisMap<String, Object> redisMap = tokenRedisMap.get(headerToken);
DefaultRedisMap<String, Object> redisMap = tokenRedisMap.get(standardRedisTokenKey(headerToken));
if (redisMap == null) {
return null;
}
@ -214,7 +241,7 @@ public class HttpRequestLocal {
if (headerToken == null) {
return;
}
DefaultRedisMap<String, Object> redisMap = tokenRedisMap.get(headerToken);
DefaultRedisMap<String, Object> redisMap = tokenRedisMap.get(standardRedisTokenKey(headerToken));
if (redisMap == null) {
return ;
}

@ -335,7 +335,7 @@ var Common = {
// 只取第一个出现的TOKEN值
if (key.toUpperCase().endsWith("_UUID_TOKEN")) {
var tokenKey = sessionStorage.getItem("TOKEN_KEY");
var token = $.cookie(tokenKey);
var token = this.cookieGet(tokenKey);
if (token != null) {
$.ajaxSetup({headers: { token }})
}
@ -344,6 +344,18 @@ var Common = {
}
}
},
/**
* cookie获取
*/
cookieGet: function(key) {
for (let str of document.cookie.split(";")) {
var kv = str.split("=");
if (kv[0] === key) {
return kv[1];
}
}
return null;
},
post: function (url, paras, next) {
this.ajaxInit()
$.ajax({

@ -112,6 +112,7 @@
var keyIdOrToken = '', tokenKey = ''
for (let key of Object.keys(rsp.data)) {
debugger
// key 长得像 {}_{}_{}_UUID_TOKEN
//将用户名Token存入缓存中
if (key.toUpperCase().endsWith("_UUID_TOKEN")) {
@ -123,13 +124,7 @@
}
// 跳转至PC端
if(rsp.data.isPc === 'true'){
window.location.href = "${ctxPath}/index.do";
}
// 跳转学生端
else{
window.location.href = "${ctxPath}/index.do?key_id=" + keyIdOrToken;
}
window.location.href = "${ctxPath}/index.do?key_id=" + keyIdOrToken;
}else {
layer.msg(rsp.msg);
return;

@ -87,6 +87,8 @@ public class InterceptorConfig implements WebMvcConfigurer, InitializingBean {
"/",
"/login.json",
"/pc/index.do",
"/index.do",
"/api/studentClientLink/getList.do",
"/css/**",
"/js/**",
"/fonts/**",

@ -16,6 +16,7 @@ import java.util.concurrent.TimeUnit;
import static com.ibeetl.admin.core.service.CorePlatformService.LOGIN_USER_TTL;
import static com.ibeetl.admin.core.service.CorePlatformService.TOKEN_KEY;
import static com.ibeetl.admin.core.util.HttpRequestLocal.AUTHORIZATION;
import static com.ibeetl.admin.core.util.HttpRequestLocal.jwtTokenSplitTimestamp;
/**
* Redis Token
@ -49,11 +50,18 @@ public class RedisTokenInterceptor implements HandlerInterceptor {
//获取请求头中的token
//根据前端的请求来确定Header中的参数
String token = request.getHeader(AUTHORIZATION);
if(StringUtils.isEmpty(token)) {
return true;
}
// 标准化token有时候会在token后面加时间戳。这里要过滤掉时间戳。
token = jwtTokenSplitTimestamp(token)[0];
if(StringUtils.isEmpty(token)) {
return true;
}
//获取redis中的token键对于的用户
Map<Object, Object> userMap = stringRedisTemplate.opsForHash().entries(TOKEN_KEY + ":" + token);
final String redisTokenKey = TOKEN_KEY + ":" + token;
Map<Object, Object> userMap = stringRedisTemplate.opsForHash().entries(redisTokenKey);
if(userMap.isEmpty()){
return true;
}
@ -62,7 +70,7 @@ public class RedisTokenInterceptor implements HandlerInterceptor {
//将CoreUser存到ThreadLocal线程中
UserHolder.saveUser(coreUser);
//刷新Redis中token的有效时间
stringRedisTemplate.expire(TOKEN_KEY + ":" + token, LOGIN_USER_TTL, TimeUnit.DAYS);
stringRedisTemplate.expire(redisTokenKey, LOGIN_USER_TTL, TimeUnit.DAYS);
return true;
}

@ -829,9 +829,13 @@ public class IndexController {
this.platformService.setLoginUserByToken(token, info.getUser(), info.getCurrentOrg(), info.getOrgs(), uSystem);
// 登录信息存放Session。这里可能是学校管理员
webPlatformService.setUserInfoToSessionByIdentity(token, info.getUser());
Map<String, String> res = insertCookie(response, user, tokenKey);
res.put("isPc", "true");
return JsonResult.success(res);
insertCookie(response, user, tokenKey);
String keyId = fastUUID().toString(true);
indexTokenMap.put(keyId, token);
return JsonResult.success(MapUtil.builder("isPc", "true")
.put(createTokenKey(user), keyId)
.build());
}
}

Loading…
Cancel
Save