Commit 58ed5aa7 by 陈健智

单账号登录限制

parent 2782ec85
......@@ -2,11 +2,13 @@ package com.zhiwei.brandkbs2.auth;
import com.alibaba.fastjson.JSON;
import com.zhiwei.brandkbs2.common.GenericAttribute;
import com.zhiwei.brandkbs2.common.RedisKeyPrefix;
import com.zhiwei.brandkbs2.enmus.RoleEnum;
import com.zhiwei.brandkbs2.model.CommonCodeEnum;
import com.zhiwei.brandkbs2.model.ResponseResult;
import com.zhiwei.brandkbs2.pojo.UserInfo;
import com.zhiwei.brandkbs2.service.UserService;
import com.zhiwei.brandkbs2.util.RedisUtil;
import com.zhiwei.brandkbs2.util.Tools;
import com.zhiwei.middleware.auth.util.JwtUtil;
import org.apache.logging.log4j.LogManager;
......@@ -17,6 +19,7 @@ import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
......@@ -26,10 +29,12 @@ import org.springframework.web.context.request.ServletRequestAttributes;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.PrintWriter;
import java.lang.reflect.Method;
import java.util.Collections;
import java.util.Map;
import java.util.Objects;
/**
* @author sjj
......@@ -49,6 +54,9 @@ public class AuthAspect {
@Resource(name = "userServiceImpl")
private UserService UserService;
@Autowired
private RedisUtil redisUtil;
@Pointcut("execution(com.zhiwei.brandkbs2.model.ResponseResult com.zhiwei.brandkbs2.controller..*.*(..))")
// @Pointcut("within(com.zhiwei.brandkbs2.controller..*)")
public void auth() {
......@@ -82,6 +90,18 @@ public class AuthAspect {
log.error("token解析异常,uri:{},methodName:{},token:{}", uri, methodName, token);
} else {
String uid = tokenInfo.get(GenericAttribute.USER_ID).toString();
// session校验
HttpSession session = request.getSession();
String sessionId = session.getId();
String cacheSessionId = redisUtil.get(RedisKeyPrefix.userSessionKey(uid));
// log.info("userId:{},sessionId:{},cacheSessionId:{}", uid, sessionId, cacheSessionId);
if (null == cacheSessionId) {
redisUtil.set(RedisKeyPrefix.userSessionKey(uid), sessionId);
}
// 已登录状态
if (null != cacheSessionId && !Objects.equals(sessionId, cacheSessionId)){
return joinPoint.proceed();
}
UserInfo userInfo = UserService.queryUserInfo(uid, request.getHeader("pid"));
if (null == userInfo) {
userInfo = new UserInfo().setUserId(uid).setProjectId(request.getHeader("pid"));
......
......@@ -128,6 +128,8 @@ public class RedisKeyPrefix {
public static final String AI_SEARCH_QUESTION = "BRANDKBS:AI:SEARCH:QUESTION:";
public static final String USER_SESSION = "BRANDKBS:USER:SESSION:";
public static String projectWarnHotTopKeyAll(String projectId, String type) {
return RedisKeyPrefix.generateRedisKey(RedisKeyPrefix.PROJECT_WARN_HOT_TOP, projectId, Tools.concat(type, "*"));
}
......@@ -168,6 +170,10 @@ public class RedisKeyPrefix {
return RedisKeyPrefix.generateRedisKey(RedisKeyPrefix.HOT_SUPPLEMENT_WORD, projectId);
}
public static String userSessionKey(String userId) {
return RedisKeyPrefix.generateRedisKey(RedisKeyPrefix.USER_SESSION, userId);
}
private static String generateRedisKey(String... keys) {
Objects.requireNonNull(keys);
boolean contains = keys[0].endsWith(":");
......
......@@ -3,7 +3,9 @@ package com.zhiwei.brandkbs2.service.impl;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zhiwei.brandkbs2.auth.UserThreadLocal;
import com.zhiwei.brandkbs2.common.GenericAttribute;
import com.zhiwei.brandkbs2.common.GlobalPojo;
import com.zhiwei.brandkbs2.common.RedisKeyPrefix;
import com.zhiwei.brandkbs2.config.Constant;
import com.zhiwei.brandkbs2.dao.ProjectDao;
import com.zhiwei.brandkbs2.dao.UserDao;
......@@ -24,8 +26,10 @@ import com.zhiwei.brandkbs2.pojo.vo.PageVO;
import com.zhiwei.brandkbs2.service.UserCenterService;
import com.zhiwei.brandkbs2.service.UserService;
import com.zhiwei.brandkbs2.util.MongoUtil;
import com.zhiwei.brandkbs2.util.RedisUtil;
import com.zhiwei.brandkbs2.util.Tools;
import com.zhiwei.middleware.auth.pojo.CenterUser;
import com.zhiwei.middleware.auth.util.JwtUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
......@@ -38,8 +42,11 @@ import org.springframework.http.HttpEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Collectors;
......@@ -54,6 +61,9 @@ import java.util.stream.Collectors;
public class UserServiceImpl implements UserService {
public static final Logger log = LogManager.getLogger(UserServiceImpl.class);
@Value("${jwt.key}")
private String jwtKey;
@Resource(name = "userDao")
private UserDao userDao;
......@@ -87,6 +97,9 @@ public class UserServiceImpl implements UserService {
@Autowired
private RestTemplate restTemplate;
@Autowired
private RedisUtil redisUtil;
@Override
public UserInfo login() {
String uid = UserThreadLocal.getUserId();
......@@ -358,7 +371,12 @@ public class UserServiceImpl implements UserService {
@Override
public boolean checkUserRoles() {
User user = userDao.findOneById(UserThreadLocal.getUserId());
ServletRequestAttributes servletRequestAttributes = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes());
HttpServletRequest request = servletRequestAttributes.getRequest();
Map<String, Object> map = JwtUtil.unsign(request.getHeader(jwtKey), Map.class);
String userId = map.get(GenericAttribute.USER_ID).toString();
redisUtil.set(RedisKeyPrefix.userSessionKey(userId), request.getSession().getId());
User user = userDao.findOneById(userId);
if (null == user) {
return false;
}
......
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