mirror of https://gitee.com/maxjhandsome/pig
修改ttl 失效问题
This commit is contained in:
parent
bf5dd6f350
commit
40f41a0fe3
|
@ -17,14 +17,9 @@
|
|||
|
||||
<h2 align="center">Supporting pig group</h2>
|
||||
|
||||
### 前端解决方案
|
||||
https://gitee.com/smallweigit/avue
|
||||
### 项目官网
|
||||
|
||||
### 视频教程
|
||||
https://www.bilibili.com/video/av20229859/
|
||||
|
||||
### 使用文档
|
||||
[pig开发文档V1.0](https://www.kancloud.cn/lengleng/pig-guide/550709)
|
||||
文档、视频:https://pig4cloud.com/
|
||||
|
||||
|
||||
### 分支介绍
|
||||
|
|
|
@ -2,16 +2,12 @@ package com.github.pig.common.bean.aop;
|
|||
|
||||
import com.github.pig.common.constant.SecurityConstants;
|
||||
import com.github.pig.common.util.UserUtils;
|
||||
import com.github.pig.common.vo.UserVO;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import com.xiaoleilu.hutool.util.StrUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.aspectj.lang.ProceedingJoinPoint;
|
||||
import org.aspectj.lang.annotation.Around;
|
||||
import org.aspectj.lang.annotation.Aspect;
|
||||
import org.aspectj.lang.annotation.Pointcut;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.cache.CacheManager;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.context.request.RequestContextHolder;
|
||||
import org.springframework.web.context.request.ServletRequestAttributes;
|
||||
|
@ -25,13 +21,10 @@ import java.util.Arrays;
|
|||
* Controller 增强
|
||||
*/
|
||||
|
||||
@Slf4j
|
||||
@Aspect
|
||||
@Component
|
||||
public class ControllerAop {
|
||||
private static final Logger logger = LoggerFactory.getLogger(ControllerAop.class);
|
||||
@Autowired
|
||||
private CacheManager cacheManager;
|
||||
|
||||
@Pointcut("execution(public com.github.pig.common.util.R *(..))")
|
||||
public void pointCutR() {
|
||||
}
|
||||
|
@ -69,39 +62,28 @@ public class ControllerAop {
|
|||
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
|
||||
HttpServletRequest request = attributes.getRequest();
|
||||
|
||||
String token = UserUtils.getToken(request);
|
||||
UserVO userVo = null;
|
||||
if (StringUtils.isNotEmpty(token)) {
|
||||
userVo = cacheManager.getCache(SecurityConstants.TOKEN_USER_DETAIL).get(token, UserVO.class);
|
||||
}
|
||||
String username;
|
||||
if (userVo == null) {
|
||||
username = UserUtils.getUserName(request);
|
||||
if (StringUtils.isNotEmpty(username)) {
|
||||
String username = request.getHeader(SecurityConstants.USER_HEADER);
|
||||
if (StrUtil.isNotBlank(username)){
|
||||
log.info("Controller AOP get username:{}", username);
|
||||
UserUtils.setUser(username);
|
||||
}
|
||||
} else {
|
||||
username = userVo.getUsername();
|
||||
UserUtils.setUser(username);
|
||||
}
|
||||
logger.info("Controller AOP get username:{}", username);
|
||||
|
||||
logger.info("URL : " + request.getRequestURL().toString());
|
||||
logger.info("HTTP_METHOD : " + request.getMethod());
|
||||
logger.info("IP : " + request.getRemoteAddr());
|
||||
logger.info("CLASS_METHOD : " + pjp.getSignature().getDeclaringTypeName() + "." + pjp.getSignature().getName());
|
||||
logger.info("ARGS : " + Arrays.toString(pjp.getArgs()));
|
||||
log.info("URL : " + request.getRequestURL().toString());
|
||||
log.info("HTTP_METHOD : " + request.getMethod());
|
||||
log.info("IP : " + request.getRemoteAddr());
|
||||
log.info("CLASS_METHOD : " + pjp.getSignature().getDeclaringTypeName() + "." + pjp.getSignature().getName());
|
||||
log.info("ARGS : " + Arrays.toString(pjp.getArgs()));
|
||||
|
||||
Object result;
|
||||
|
||||
try {
|
||||
result = pjp.proceed();
|
||||
logger.info(pjp.getSignature() + "use time:" + (System.currentTimeMillis() - startTime));
|
||||
log.info(pjp.getSignature() + "use time:" + (System.currentTimeMillis() - startTime));
|
||||
} catch (Throwable e) {
|
||||
logger.error("异常信息:", e);
|
||||
log.error("异常信息:", e);
|
||||
throw new RuntimeException(e);
|
||||
} finally {
|
||||
if (StringUtils.isNotEmpty(username)) {
|
||||
if (StrUtil.isNotEmpty(username)) {
|
||||
UserUtils.clearAllUserInfo();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -56,6 +56,8 @@ public class TokenArgumentResolver implements HandlerMethodArgumentResolver {
|
|||
if (StrUtil.isBlank(username) || StrUtil.isBlank(roles)) {
|
||||
log.warn("resolveArgument error username or role is empty");
|
||||
return null;
|
||||
} else {
|
||||
log.info("resolveArgument username :{} roles:{}", username, roles);
|
||||
}
|
||||
UserVO userVO = new UserVO();
|
||||
userVO.setUsername(username);
|
||||
|
|
|
@ -24,55 +24,6 @@ public class UserUtils {
|
|||
private static final ThreadLocal<String> THREAD_LOCAL_USER = new TransmittableThreadLocal<>();
|
||||
private static final String KEY_USER = "user";
|
||||
|
||||
|
||||
/**
|
||||
* 根据用户请求中的token 获取用户名
|
||||
*
|
||||
* @param request Request
|
||||
* @return “”、username
|
||||
*/
|
||||
public static String getUserName(HttpServletRequest request) {
|
||||
String username = "";
|
||||
String authorization = request.getHeader(CommonConstant.REQ_HEADER);
|
||||
if (StringUtils.isEmpty(authorization)) {
|
||||
return username;
|
||||
}
|
||||
String token = StringUtils.substringAfter(authorization, CommonConstant.TOKEN_SPLIT);
|
||||
if (StringUtils.isEmpty(token)) {
|
||||
return username;
|
||||
}
|
||||
String key = Base64.getEncoder().encodeToString(CommonConstant.SIGN_KEY.getBytes());
|
||||
try {
|
||||
Claims claims = Jwts.parser().setSigningKey(key).parseClaimsJws(token).getBody();
|
||||
username = claims.get("user_name").toString();
|
||||
} catch (Exception ex) {
|
||||
log.error("用户名解析异常,token:{},key:{}", token, key);
|
||||
}
|
||||
return username;
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过token 获取用户名
|
||||
*
|
||||
* @param authorization token
|
||||
* @return 用户名
|
||||
*/
|
||||
public static String getUserName(String authorization) {
|
||||
String username = "";
|
||||
String token = StringUtils.substringAfter(authorization, CommonConstant.TOKEN_SPLIT);
|
||||
if (StringUtils.isEmpty(token)) {
|
||||
return username;
|
||||
}
|
||||
String key = Base64.getEncoder().encodeToString(CommonConstant.SIGN_KEY.getBytes());
|
||||
try {
|
||||
Claims claims = Jwts.parser().setSigningKey(key).parseClaimsJws(token).getBody();
|
||||
username = claims.get("user_name").toString();
|
||||
} catch (Exception ex) {
|
||||
log.error("用户名解析异常,token:{},key:{}", token, key);
|
||||
}
|
||||
return username;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据请求heard中的token获取用户角色
|
||||
*
|
||||
|
@ -105,7 +56,6 @@ public class UserUtils {
|
|||
*/
|
||||
public static void setUser(String username) {
|
||||
THREAD_LOCAL_USER.set(username);
|
||||
|
||||
MDC.put(KEY_USER, username);
|
||||
}
|
||||
|
||||
|
|
|
@ -14,5 +14,5 @@ public class LogVO implements Serializable {
|
|||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private SysLog sysLog;
|
||||
private String token;
|
||||
private String username;
|
||||
}
|
||||
|
|
|
@ -3,19 +3,20 @@ package com.github.pig.gateway.service.impl;
|
|||
import com.github.pig.common.constant.CommonConstant;
|
||||
import com.github.pig.common.constant.MqQueueConstant;
|
||||
import com.github.pig.common.entity.SysLog;
|
||||
import com.github.pig.common.util.UserUtils;
|
||||
import com.github.pig.common.vo.LogVO;
|
||||
import com.github.pig.gateway.service.LogSendService;
|
||||
import com.netflix.zuul.context.RequestContext;
|
||||
import com.xiaoleilu.hutool.http.HttpUtil;
|
||||
import com.xiaoleilu.hutool.io.IoUtil;
|
||||
import com.xiaoleilu.hutool.util.StrUtil;
|
||||
import com.xiaoleilu.hutool.util.URLUtil;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.apache.http.HttpStatus;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.amqp.core.AmqpTemplate;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.core.context.SecurityContextHolder;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
@ -55,7 +56,6 @@ public class LogSendServiceImpl implements LogSendService {
|
|||
log.setMethod(method);
|
||||
log.setUserAgent(request.getHeader("user-agent"));
|
||||
log.setParams(HttpUtil.toParams(request.getParameterMap()));
|
||||
log.setCreateBy(UserUtils.getUserName(request));
|
||||
Long startTime = (Long) requestContext.get("startTime");
|
||||
log.setTime(System.currentTimeMillis() - startTime);
|
||||
if (requestContext.get(SERVICE_ID) != null) {
|
||||
|
@ -98,10 +98,12 @@ public class LogSendServiceImpl implements LogSendService {
|
|||
}
|
||||
|
||||
//保存发往MQ(只保存授权)
|
||||
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
||||
if (authentication != null && StrUtil.isNotBlank(authentication.getName())) {
|
||||
LogVO logVo = new LogVO();
|
||||
log.setCreateBy(authentication.getName());
|
||||
logVo.setSysLog(log);
|
||||
if (StringUtils.isNotEmpty(request.getHeader(CommonConstant.REQ_HEADER))) {
|
||||
logVo.setToken(request.getHeader(CommonConstant.REQ_HEADER));
|
||||
logVo.setUsername(authentication.getName());
|
||||
rabbitTemplate.convertAndSend(MqQueueConstant.LOG_QUEUE, logVo);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -26,9 +26,8 @@ public class LogReceiveListener {
|
|||
@RabbitHandler
|
||||
public void receive(LogVO logVo) {
|
||||
SysLog sysLog = logVo.getSysLog();
|
||||
String username = UserUtils.getUserName(logVo.getToken());
|
||||
MDC.put(KEY_USER, username);
|
||||
sysLog.setCreateBy(username);
|
||||
MDC.put(KEY_USER, logVo.getUsername());
|
||||
sysLog.setCreateBy(logVo.getUsername());
|
||||
sysLogService.insert(sysLog);
|
||||
MDC.remove(KEY_USER);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue