利用aop注解来实现对访问的环绕拦截处理

7 篇文章 0 订阅
6 篇文章 0 订阅
import com.xxx.LogAop;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.ResponseEntity;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;


/**
 * 记录日志的切面
 * 目的:记录访问日志信息
 * @author lff
 */
@Aspect   //定义一个切面
@Configuration
public class LogRecordAspect {

    // 可以定义切点Pointcut 自己使用注解来标记处理就不需要再定义切点了
//    @Pointcut("execution(* com.xxx.controller.*Controller.*(..))")
//    public void excudeService() {
//    }
    @Autowired
    private LogInfoService logInfoService;
    @Autowired
    private JsonUtils jsonUtils;
    //拦截 环绕
    @Around(" @annotation(logAop)")
    public Object doAround(ProceedingJoinPoint pjp, LogAop logAop) throws Throwable {
        //
   
        RequestAttributes ra = RequestContextHolder.getRequestAttributes();
        ServletRequestAttributes sra = (ServletRequestAttributes) ra;
        HttpServletRequest request = sra.getRequest();

        //访问ip
        String ip = IpUtils.getIpAddr(request);
        //拦截的接口
        String uri = request.getRequestURI();
        //入参
        String parameter = this.getParameter(request);
        //从请求头获取信息
        String token = request.getHeader("token");
        
    }
    
}
package com.xxx.annotation;xxx

import org.springframework.core.annotation.AliasFor;

import java.lang.annotation.*;

/**
 * 自定义注解类
 * 目的:需要记录日志的接口
 */
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@Documented
public @interface LogAop {
    //保存重复提交标记 默认为需要保存 必须要传入接口id参数,否则不拦截
    @AliasFor("value")
    String name() default "未定义接口名字";
    @AliasFor("name")
    String value() default "未定义接口名字";
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值