基于SpringAOP的权限管理

不废话,上代码

1.自定义注解

@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface MyAnnotation {
String value() default “”;
}
2.在方法上使用注解

@RequestMapping("/index/{str}")
@MyAnnotation
public String test(@PathVariable("str") String str){
    //testService.test();
    System.out.println("ok:"+str);
    return "ok";
}

3.定义切面

@Component
@Aspect
@Configuration
public class HttpAspect {

/**
 * 定义切点,切点为对应controller
 */
@Pointcut("execution(public * com.example.demo.controller.TestController.*(*))")
//使用指定路径的方式调用
public void myPoint() {
}

@Pointcut("@annotation(com.example.demo.config.aspect.MyAnnotation)")
//使用注解方式来调用
public void ByAnnotation() {
}

/*
//可以利用bifore ,执行权限判定逻辑
@Before(“myPoint()”)
public void before(JoinPoint joinPoint) {
System.out.println(“this is before”);
}
/
/
大家可以自己试试用第一种方法,
@Around(“myPoint()”)
public void doAround(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println(“this is aroud”);
Object obeject = joinPoint.proceed();
Object[] args = joinPoint.getArgs();
// return obeject;
// 注意,如果在Around中不调用joinPoint.proceed()则@Before,@After都不会生效,
}*/

@Around("ByAnnotation()")
public Object doAroundByAnnotation(ProceedingJoinPoint joinPoint) throws Throwable {
    System.out.println("this is aroud");
 
    //这里获取参数,执行自己的业务
    Object[] args = joinPoint.getArgs();
    //这里如果没有,将没有返回
    Object obeject = joinPoint.proceed();
    return obeject;

}

}
使用:哪里要记录接口参数,就在方法上使用自定义的注解,即可记录参数

注意点

1.aop执行顺序

2.@Pointcut 里拦截哪些类,哪些方法

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值