PageProcessAspect.java 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. package com.java110.service.aop;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.java110.common.constant.CommonConstant;
  4. import com.java110.common.constant.ResponseConstant;
  5. import com.java110.common.exception.FilterException;
  6. import com.java110.core.context.IPageData;
  7. import com.java110.core.factory.PageDataFactory;
  8. import com.java110.common.util.Assert;
  9. import com.java110.core.factory.GenerateCodeFactory;
  10. import com.java110.common.util.StringUtil;
  11. import com.java110.core.context.PageData;
  12. import org.aspectj.lang.JoinPoint;
  13. import org.aspectj.lang.ProceedingJoinPoint;
  14. import org.aspectj.lang.annotation.*;
  15. import org.slf4j.Logger;
  16. import org.slf4j.LoggerFactory;
  17. import org.springframework.stereotype.Component;
  18. import org.springframework.web.context.request.RequestContextHolder;
  19. import org.springframework.web.context.request.ServletRequestAttributes;
  20. import javax.servlet.http.Cookie;
  21. import javax.servlet.http.HttpServletRequest;
  22. import javax.servlet.http.HttpServletResponse;
  23. import java.io.BufferedReader;
  24. import java.io.IOException;
  25. import java.io.InputStream;
  26. import java.io.InputStreamReader;
  27. import java.util.Map;
  28. /**
  29. * 数据初始化
  30. * Created by wuxw on 2018/5/2.
  31. */
  32. @Aspect
  33. @Component
  34. public class PageProcessAspect {
  35. private static Logger logger = LoggerFactory.getLogger(PageProcessAspect.class);
  36. @Pointcut("execution(public * com.java110..*.*Controller.*(..)) || execution(public * com.java110..*.*Rest.*(..))")
  37. public void dataProcess() {
  38. }
  39. /**
  40. * 初始化数据
  41. *
  42. * @param joinPoint
  43. * @throws Throwable
  44. */
  45. @Before("dataProcess()")
  46. public void deBefore(JoinPoint joinPoint) throws Throwable {
  47. // 接收到请求,记录请求内容
  48. ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
  49. HttpServletRequest request = attributes.getRequest();
  50. IPageData pd = null;
  51. String reqData = "";
  52. String userId = "";
  53. String sessionId = request.getSession().getId();
  54. if ("POST,PUT".contains(request.getMethod())) {
  55. InputStream in = request.getInputStream();
  56. BufferedReader reader = new BufferedReader(new InputStreamReader(in));
  57. //reader.
  58. StringBuffer sb = new StringBuffer();
  59. String str = "";
  60. while ((str = reader.readLine()) != null) {
  61. sb.append(str);
  62. }
  63. reqData = sb.toString();
  64. }
  65. //对 get情况下的参数进行封装
  66. else {
  67. Map<String, String[]> params = request.getParameterMap();
  68. if (params != null && !params.isEmpty()) {
  69. JSONObject paramObj = new JSONObject();
  70. for (String key : params.keySet()) {
  71. if (params.get(key).length > 0) {
  72. String value = "";
  73. for (int paramIndex = 0; paramIndex < params.get(key).length; paramIndex++) {
  74. value = params.get(key)[paramIndex] + ",";
  75. }
  76. value = value.endsWith(",") ? value.substring(0, value.length() - 1) : value;
  77. paramObj.put(key, value);
  78. }
  79. continue;
  80. }
  81. reqData = paramObj.toJSONString();
  82. }
  83. }
  84. // 获取 userId
  85. if (request.getAttribute("claims") != null && request.getAttribute("claims") instanceof Map) {
  86. Map<String, String> userInfo = (Map<String, String>) request.getAttribute("claims");
  87. if (userInfo.containsKey(CommonConstant.LOGIN_USER_ID)) {
  88. userId = userInfo.get(CommonConstant.LOGIN_USER_ID);
  89. }
  90. }
  91. // 获取组件名称 和方法名称
  92. String url = request.getRequestURL() != null ? request.getRequestURL().toString() : "";
  93. String componentCode = "";
  94. String componentMethod = "";
  95. if (url.contains("callComponent")) { //组件处理
  96. String[] urls = url.split("/");
  97. if (urls.length == 6) {
  98. componentCode = urls[4];
  99. componentMethod = urls[5];
  100. }
  101. } else if (url.contains("flow")) { //流程处理
  102. String[] urls = url.split("/");
  103. if (urls.length == 5) {
  104. componentCode = urls[4];
  105. }
  106. }
  107. pd = PageData.newInstance().builder(userId, this.getToken(request), reqData, componentCode, componentMethod, url, sessionId);
  108. request.setAttribute(CommonConstant.CONTEXT_PAGE_DATA, pd);
  109. }
  110. @AfterReturning(returning = "ret", pointcut = "dataProcess()")
  111. public void doAfterReturning(Object ret) throws Throwable {
  112. // 处理完请求,返回内容
  113. }
  114. //后置异常通知
  115. @AfterThrowing("dataProcess()")
  116. public void throwException(JoinPoint jp) {
  117. }
  118. //后置最终通知,final增强,不管是抛出异常或者正常退出都会执行
  119. @After("dataProcess()")
  120. public void after(JoinPoint jp) throws IOException {
  121. // 接收到请求,记录请求内容
  122. ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
  123. HttpServletRequest request = attributes.getRequest();
  124. PageData pd = request.getAttribute(CommonConstant.CONTEXT_PAGE_DATA) != null ? (PageData) request.getAttribute(CommonConstant.CONTEXT_PAGE_DATA) : null;
  125. //保存日志处理
  126. if (pd == null) {
  127. return;
  128. }
  129. //写cookies信息
  130. writeCookieInfo(pd, attributes);
  131. }
  132. //环绕通知,环绕增强,相当于MethodInterceptor
  133. @Around("dataProcess()")
  134. public Object around(ProceedingJoinPoint pjp) {
  135. try {
  136. Object o = pjp.proceed();
  137. return o;
  138. } catch (Throwable e) {
  139. e.printStackTrace();
  140. return null;
  141. }
  142. }
  143. /**
  144. * 获取TOKEN
  145. *
  146. * @param request
  147. * @return
  148. */
  149. private String getToken(HttpServletRequest request) throws FilterException {
  150. String token = "";
  151. if (request.getCookies() == null || request.getCookies().length == 0) {
  152. return token;
  153. }
  154. for (Cookie cookie : request.getCookies()) {
  155. if (CommonConstant.COOKIE_AUTH_TOKEN.equals(cookie.getName())) {
  156. token = cookie.getValue();
  157. }
  158. }
  159. return token;
  160. }
  161. /**
  162. * 写cookie 信息
  163. *
  164. * @param pd 页面封装信息
  165. * @param attributes
  166. * @throws IOException
  167. */
  168. private void writeCookieInfo(IPageData pd, ServletRequestAttributes attributes) throws IOException {
  169. // 这里目前只写到组件级别,如果需要 写成方法级别
  170. if (!StringUtil.isNullOrNone(pd.getToken()) && "login".equals(pd.getComponentCode())) {
  171. HttpServletResponse response = attributes.getResponse();
  172. Cookie cookie = new Cookie(CommonConstant.COOKIE_AUTH_TOKEN, pd.getToken());
  173. cookie.setHttpOnly(true);
  174. cookie.setPath("/");
  175. response.addCookie(cookie);
  176. response.flushBuffer();
  177. }
  178. }
  179. }