能够在指定拦截的Controller中方法执行以前,进行请求拦截,好比对一些须要受权验证的方法进行拦截判断cookie及权限。redis
@Aspect @Component数据库
@Pointcut("execution(public * com.xxx.controller.*.*(..))" + "&& !execution(public * com.xxx.controller.WelcomeController.*(..))") public void verify() { } @Before("verify()") public void doVerify(JoinPoint joinPoint) { ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); HttpServletRequest request = attributes.getRequest(); // 用户身份认证 UserVO user = UserAuth.getUserInfoFromToken(request, redisClient, authService); // 同步用户信息到 threadlocal ThreadLocalUtil.set(CommonConstant.USERID, user.getId()); ThreadLocalUtil.set(CommonConstant.USERNAME, user.getName()); // 用户鉴权 RequestPermissions permissions = UserPermission.getAnnotation(joinPoint); if (null != permissions) { UserPermission.checkUserPermissionAllow(request, authService, user, permissions); } }
好比在程序中控制读写分离,能够定义一个@Slave注解,在执行指定service的方法以前判断是否存在@Slave注解。cookie
@Before("@annotation(test)")// 拦截被TestAnnotation注解的方法;若是你须要拦截指定package指定规则名称的方法,能够使用表达式execution(...),具体百度一下资料一大堆 public void beforeTest(JoinPoint point, TestAnnotation test) throws Throwable { System.out.println("beforeTest:" + test.name()); } @After("@annotation(test)") public void afterTest(JoinPoint point, TestAnnotation test) { System.out.println("afterTest:" + test.name()); } @Around("@annotation(slave)") public Object proceed(ProceedingJoinPoint proceedingJoinPoint, Slave slave) throws Throwable { try { logger.info("set database connection to slave"); DatabaseContextHolder.setDatabaseType(DatabaseType.SLAVE); return proceedingJoinPoint.proceed(); } finally { DatabaseContextHolder.clearDbType(); logger.info("restore database connection"); } }
@Aspect @Component public class DataSourceAspect { @Before("execution(* com.xxx.firstboot.dao.*.*(..))") public void setDataSourceKey(JoinPoint point){ //链接点所属的类实例是ShopDao if(point.getTarget() instanceof ShopDao){ DatabaseContextHolder.setDatabaseType(DatabaseType.mytestdb2); }else{//链接点所属的类实例是UserDao(固然,这一步也能够不写,由于defaultTargertDataSource就是该类所用的mytestdb) DatabaseContextHolder.setDatabaseType(DatabaseType.mytestdb); } } }