JAR包
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.6.12</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.6.12</version>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>2.2</version>
</dependency>
</dependencies>
1.自定义注解切
1.创建切面Aspect 和通知Advice
package rrg.aoptest;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class TestAop {
@Pointcut("@annotation(Admin)")
public void test(){
}
//Advice spring aop中@Around @Before @After三个注解的区别@Before是在所拦截方法执行之前执行一段逻辑。@After 是在所拦截方法执行之后执行一段逻辑。@Around是可以同时在所拦截方法的前后执行一段逻辑。
@Before(value = "test()")
public String use(){
System.out.println("888888");
return "7777";
}
}
2.自定义admin注解
package rrg.aoptest;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
*
* @author Admin
*
*/
@Retention(value = RetentionPolicy.RUNTIME)
@Target(value = { ElementType.METHOD})
public @interface Admin {
}
3.实现切面效果
package rrg.aoptest;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class AopTestController {
@Admin
@RequestMapping("/aoptest")
public String add(){
return "6666";
}
}
2.execution 表达式切
1.创建切面Aspect 和通知Advice
package rrg.encryption;
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.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import rrg.dao.RedisDao;
import rrg.response.Msg;
@Aspect
@Component
public class ControMethodBeforeExecution {
@Autowired
private RedisDao redisDao;
//拦截rrg包或所有子包下的所有类种以uop开头的所有方法
@Pointcut("execution(* rrg..*.uop*(..))")
public void methodBeforeExecution(){
}
//拦截方法需要执行的代码
//spring aop中@Around @Before @After三个注解的区别@Before是在所拦截方法执行之前执行一段逻辑。@After 是在所拦截方法执行之后执行一段逻辑。
//@Around是可以同时在所拦截方法的前后执行一段逻辑。
@Around("methodBeforeExecution()")
public Msg cheakLogin(ProceedingJoinPoint jp) {
//获取拦截方法的参数 注意 方法的参数对应Object数组的下标
Object[] args = jp.getArgs();
//获取被拦截方法的第一个参数
int userId=(int)args[0];
//获取被拦截方法的第二个参数
String token=(String)args[1];
String localtoken=redisDao.get("userId_"+userId);
Msg msg=null;
if(token.equals(localtoken)) {
try {
msg= (Msg)jp.proceed();
} catch (Throwable e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else {
return Msg.fail().add("err_code", "令牌失效");
}
return msg;
}
}