From 40f41a0fe3b90694e8a895928975b167bc53c76d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=86=B7=E5=86=B7?= Date: Thu, 12 Apr 2018 17:12:13 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9ttl=20=E5=A4=B1=E6=95=88?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 9 +--- .../pig/common/bean/aop/ControllerAop.java | 46 ++++++----------- .../bean/resolver/TokenArgumentResolver.java | 2 + .../com/github/pig/common/util/UserUtils.java | 50 ------------------- .../java/com/github/pig/common/vo/LogVO.java | 2 +- .../service/impl/LogSendServiceImpl.java | 16 +++--- .../common/listener/LogReceiveListener.java | 5 +- 7 files changed, 30 insertions(+), 100 deletions(-) diff --git a/README.md b/README.md index 1728efef..17d142cb 100644 --- a/README.md +++ b/README.md @@ -17,14 +17,9 @@

Supporting pig group

-### 前端解决方案 -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/ ### 分支介绍 diff --git a/pig-common/src/main/java/com/github/pig/common/bean/aop/ControllerAop.java b/pig-common/src/main/java/com/github/pig/common/bean/aop/ControllerAop.java index 22b17509..67d02c5a 100644 --- a/pig-common/src/main/java/com/github/pig/common/bean/aop/ControllerAop.java +++ b/pig-common/src/main/java/com/github/pig/common/bean/aop/ControllerAop.java @@ -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)) { - UserUtils.setUser(username); - } - } else { - username = userVo.getUsername(); + String username = request.getHeader(SecurityConstants.USER_HEADER); + if (StrUtil.isNotBlank(username)){ + log.info("Controller AOP get username:{}", username); 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(); } } diff --git a/pig-common/src/main/java/com/github/pig/common/bean/resolver/TokenArgumentResolver.java b/pig-common/src/main/java/com/github/pig/common/bean/resolver/TokenArgumentResolver.java index 66325316..b9938a1d 100644 --- a/pig-common/src/main/java/com/github/pig/common/bean/resolver/TokenArgumentResolver.java +++ b/pig-common/src/main/java/com/github/pig/common/bean/resolver/TokenArgumentResolver.java @@ -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); diff --git a/pig-common/src/main/java/com/github/pig/common/util/UserUtils.java b/pig-common/src/main/java/com/github/pig/common/util/UserUtils.java index c6d546e1..bd45e83d 100644 --- a/pig-common/src/main/java/com/github/pig/common/util/UserUtils.java +++ b/pig-common/src/main/java/com/github/pig/common/util/UserUtils.java @@ -24,55 +24,6 @@ public class UserUtils { private static final ThreadLocal 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); } diff --git a/pig-common/src/main/java/com/github/pig/common/vo/LogVO.java b/pig-common/src/main/java/com/github/pig/common/vo/LogVO.java index 98401f02..973be989 100644 --- a/pig-common/src/main/java/com/github/pig/common/vo/LogVO.java +++ b/pig-common/src/main/java/com/github/pig/common/vo/LogVO.java @@ -14,5 +14,5 @@ public class LogVO implements Serializable { private static final long serialVersionUID = 1L; private SysLog sysLog; - private String token; + private String username; } diff --git a/pig-gateway/src/main/java/com/github/pig/gateway/service/impl/LogSendServiceImpl.java b/pig-gateway/src/main/java/com/github/pig/gateway/service/impl/LogSendServiceImpl.java index 76fa4908..0128316c 100644 --- a/pig-gateway/src/main/java/com/github/pig/gateway/service/impl/LogSendServiceImpl.java +++ b/pig-gateway/src/main/java/com/github/pig/gateway/service/impl/LogSendServiceImpl.java @@ -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(只保存授权) - LogVO logVo = new LogVO(); - logVo.setSysLog(log); - if (StringUtils.isNotEmpty(request.getHeader(CommonConstant.REQ_HEADER))) { - logVo.setToken(request.getHeader(CommonConstant.REQ_HEADER)); + Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); + if (authentication != null && StrUtil.isNotBlank(authentication.getName())) { + LogVO logVo = new LogVO(); + log.setCreateBy(authentication.getName()); + logVo.setSysLog(log); + logVo.setUsername(authentication.getName()); rabbitTemplate.convertAndSend(MqQueueConstant.LOG_QUEUE, logVo); } } diff --git a/pig-modules/pig-upms-service/src/main/java/com/github/pig/admin/common/listener/LogReceiveListener.java b/pig-modules/pig-upms-service/src/main/java/com/github/pig/admin/common/listener/LogReceiveListener.java index 9f5ba803..312c8e93 100644 --- a/pig-modules/pig-upms-service/src/main/java/com/github/pig/admin/common/listener/LogReceiveListener.java +++ b/pig-modules/pig-upms-service/src/main/java/com/github/pig/admin/common/listener/LogReceiveListener.java @@ -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); }