使用Spring AOP来进行权限验证

时间:2022-04-19 12:00:38

使用Spring AOP前需要先引入相应的包

<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.8.10</version>
</dependency> <dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.10</version>
</dependency>

然后创建切面类

 package com.homework.filter;

 import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component; /**
* Created by ytt on 2017/1/11.
*/ @Aspect
@Component
public class LoginCheckAspect { @Around(value = "@annotation(org.springframework.web.bind.annotation.RequestMapping)")
public Object checkUser(ProceedingJoinPoint pjp) throws Throwable { //获取参数
Object[] args = pjp.getArgs();
System.out.println("在进入方法之前");
//放行
Object proceed = pjp.proceed();
System.out.println("执行完方法之后");
return proceed;
} @Before("execution(* com.homework.controller..*.*(..))")
public void before(){
System.out.println("在方法执行之前执行");
}
}

这样就能完成拦截了