Commit 1448fccb by 袁伟铭

1.0.0

parent b85ecdb6
......@@ -4,6 +4,7 @@ import com.zq.common.config.redis.BaseCacheKeys;
import com.zq.common.config.redis.RedisUtils;
import com.zq.common.config.security.SecurityProperties;
import com.zq.common.context.ContextUtils;
import com.zq.common.utils.AssertUtils;
import com.zq.common.vo.ApiTokenVo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
......@@ -30,9 +31,12 @@ public class UserInfoInterceptor extends HandlerInterceptorAdapter {
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
if (request.getRequestURI().contains("/app/")) {
String token = getToken(request);
log.info(">> [UserInfo token] {}", token);
ApiTokenVo tokenVo = redisUtils.getObj(BaseCacheKeys.appTokenKey(token), ApiTokenVo.class);
ContextUtils.setUserContext(tokenVo);
log.debug(">> [UserInfo token] {}", token);
if (StringUtils.isNotBlank(token)) {
ApiTokenVo tokenVo = redisUtils.getObj(BaseCacheKeys.appTokenKey(token), ApiTokenVo.class);
AssertUtils.notNull(tokenVo, "登录信息过期");
ContextUtils.setUserContext(tokenVo);
}
}
return true;
}
......
......@@ -16,6 +16,7 @@
package com.zq.common.config.limit;
import com.zq.common.annotation.Limit;
import com.zq.common.context.ContextUtils;
import com.zq.common.http.HttpRequestUtils;
import com.zq.common.utils.AssertUtils;
import org.apache.commons.lang3.StringUtils;
......@@ -66,16 +67,17 @@ public class LimitAspect {
Limit limit = signatureMethod.getAnnotation(Limit.class);
LimitType limitType = limit.limitType();
String key = limit.key();
// 构建key
if (StringUtils.isBlank(key)) {
if (limitType == LimitType.IP) {
key = HttpRequestUtils.getClientIp(request);
} else if (limitType == LimitType.USER_ID) {
Long userId = ContextUtils.getUserUserId();
key = userId != null ? userId.toString() : HttpRequestUtils.getClientIp(request);
} else {
// 获取方法名
key = signatureMethod.getName();
if ("sendCode".equals(key)) {
// 获取方法的第一个参数
key = (String) joinPoint.getArgs()[0];
}
}
}
......
......@@ -24,6 +24,8 @@ package com.zq.common.config.limit;
public enum LimitType {
// 默认
CUSTOMER,
// by ip addr
// by ip USER_ID
USER_ID,
// by ip address
IP
}
......@@ -12,6 +12,8 @@ public abstract class BaseCacheKeys {
private static final String APP_TOKEN = PREFIX + "app-token.";
private static final String ADMIN_TOKEN = PREFIX + "admin-token.";
/**
* 构建app端用户token的缓存key
*
......@@ -22,4 +24,14 @@ public abstract class BaseCacheKeys {
return APP_TOKEN + token;
}
/**
* 构建admin端用户token的缓存key
*
* @param token admin登陆后的token
* @return
*/
public static String adminTokenKey(String token) {
return ADMIN_TOKEN + token;
}
}
......@@ -29,7 +29,7 @@ public class ContextUtils {
public static Long getUserUserId() {
ApiTokenVo apiTokenVo = ThreadContext.get(APP_TOKEN_CONTEXT_KEY);
return apiTokenVo.getUserId();
return apiTokenVo == null ? null : apiTokenVo.getUserId();
}
public static void setAdminContext(OnlineUserDto onlineUserDto) {
......@@ -44,7 +44,7 @@ public class ContextUtils {
public static Long getAdminUserId() {
OnlineUserDto userDto = ThreadContext.get(ADMIN_TOKEN_CONTEXT_KEY);
return userDto.getUserId();
return userDto == null ? null : userDto.getUserId();
}
}
......
package com.zq.user.manager;
public abstract class UserCacheKeys {
import com.zq.common.config.redis.BaseCacheKeys;
public static final long APP_TOKEN_EXPIRE_MINUTES = 60 * 24 * 2;
/**
* @author wilmiam
* @since 2021-07-10 16:38
*/
public class UserCacheKeys extends BaseCacheKeys {
public static final String PREFIX = "wine.";
public static final long APP_TOKEN_EXPIRE_MINUTES = 60 * 24 * 2;
private static final String AUTH_CODE = PREFIX + "auth-code.";
private static final String APP_TOKEN = PREFIX + "app-token.";
private static final String ADMIN_TOKEN = PREFIX + "admin-token.";
private static final String LIVE_APP_TOKEN = PREFIX + "live-app-token.";
private static final String LIVE_ADMIN_TOKEN = PREFIX + "live-admin-token.";
private static final String LIVE_APP_TOKEN = PREFIX + "live-app-token.";
/**
* 构建手机验证码的缓存key
*
......@@ -27,16 +27,6 @@ public abstract class UserCacheKeys {
}
/**
* 构建app端用户token的缓存key
*
* @param token app登陆后的token
* @return
*/
public static String appTokenKey(String token) {
return APP_TOKEN + token;
}
/**
* 用户当前apptoken的缓存key
*
* @param memberId
......@@ -46,5 +36,4 @@ public abstract class UserCacheKeys {
return LIVE_APP_TOKEN + memberId;
}
}
......@@ -128,7 +128,8 @@ public class UserService {
redisUtils.setObj(UserCacheKeys.appTokenKey(token), tokenVo, UserCacheKeys.APP_TOKEN_EXPIRE_MINUTES);
// 重新登录删除前一个token实现单点登录
redisUtils.deleteStr(UserCacheKeys.liveAppTokenKey(appUser.getId()));
String cacheToken = redisUtils.getStr(UserCacheKeys.liveAppTokenKey(appUser.getId()));
redisUtils.deleteObj(UserCacheKeys.appTokenKey(cacheToken));
// 限制同一时间同一帐号只能在一个设备上登录
redisUtils.setStr(UserCacheKeys.liveAppTokenKey(appUser.getId()), token, UserCacheKeys.APP_TOKEN_EXPIRE_MINUTES);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment