首页>代码>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;
    }
}
最近下载更多
微信网友_7134912998903808  LV15 2024年9月13日
taoshen95  LV16 2024年5月15日
lyws1986  LV17 2023年10月29日
王雲飞  LV7 2023年8月24日
ewan007  LV30 2023年7月3日
139465  LV12 2023年4月3日
aaaahao  LV13 2022年10月28日
tnrthr  LV6 2022年9月22日
laishaofe  LV7 2022年8月30日
1234mama  LV19 2022年6月7日
最近浏览更多
haitianjiaoyu0  LV8 5月27日
15719908287  LV10 2024年11月1日
微信网友_7134912998903808  LV15 2024年9月12日
dapeng0011  LV15 2024年7月13日
taoshen95  LV16 2024年5月15日
简约时尚  LV13 2024年1月16日
lyws1986  LV17 2023年10月29日
王雲飞  LV7 2023年8月24日
ewan007  LV30 2023年7月3日
zzh081401 2023年6月30日
暂无贡献等级
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友