首页>代码>Spring boot 整合JWT + Security实现用户登录授权认证>/jpa-jwt-demo/src/main/java/com/gbq/jpa/jwt/demo/comment/LogRecordAspect.java
package com.gbq.jpa.jwt.demo.comment; import com.alibaba.fastjson.JSONObject; import com.gbq.jpa.jwt.demo.util.BusinessException; 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.core.annotation.Order; import org.springframework.stereotype.Component; import org.springframework.web.context.request.RequestAttributes; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest; import java.util.HashMap; /** * 切面 * * @ClassName: LogRecordAspect * @author gbq */ @Aspect @Component @Order(1) public class LogRecordAspect { private static final Logger logger = LoggerFactory.getLogger(com.gbq.jpa.jwt.demo.comment.LogRecordAspect.class); @Pointcut("execution(* com.gbq.jpa.jwt.demo.controller..*(..))") public void excudeService() { } @SuppressWarnings("unchecked") @Around(value = "excudeService()") public Object doAround(ProceedingJoinPoint pjp) throws Throwable { RequestAttributes ra = RequestContextHolder.getRequestAttributes(); ServletRequestAttributes sra = (ServletRequestAttributes) ra; HttpServletRequest request = sra.getRequest(); String url = request.getRequestURI(); String method = request.getMethod(); String uri = request.getRequestURI(); String queryString = request.getQueryString(); logger.info("接收, url: {}, method: {}, uri: {}, params: {}", url, method, uri, queryString == null ? "" : queryString); HashMap<String, Object> result = new HashMap<>(); try { Object object = pjp.proceed(); if (object instanceof String || object instanceof ModelAndView) { return object; } else if (object instanceof HashMap) { HashMap<String, Object> data = (HashMap<String, Object>) object; result.put("result", data); } else if (object instanceof JSONObject) { JSONObject jsonObject = (JSONObject) object; result.put("result", jsonObject); } result.put("status", 0); } catch (BusinessException exception) { result = new HashMap<>(); result.put("errorMsg", exception.getErrorMessage()); result.put("result", null); result.put("status", -1); exception.printStackTrace(); logger.error("error, message: {}, errorMessage: {}, exception: {}", exception.getMessage()); } catch (Exception e) { result = new HashMap<String, Object>(); result.put("errorMsg", "系统错误"); result.put("status", -1); result.put("result", null); e.printStackTrace(); logger.error(e.getMessage()); } logger.info("响应, url: {}, result: {}", url, result); return result; } }
最近下载更多