Aop+注解实现拦截

本文详细介绍了如何通过Spring AOP和注解来实现在用户阅读文章时自动增加阅读量的功能。首先,引入了相关依赖,然后定义了一个自定义注解`ArticleReadCount`,接着创建了一个切面类`ArticleAspect`,使用`@Before`注解的方法在访问文章前增加阅读数。最后,只需在访问文章的方法上添加`ArticleReadCount`注解,即可实现自动计数。
摘要由CSDN通过智能技术生成
  • 本文章介绍了一个通过注解形式实现AOP拦截完成文章阅读量增加的功能。首先在项目的pom文件中引入AOP所需的依赖代码:
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-aop -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-aop</artifactId>
			<version>5.2.1.RELEASE</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjrt -->
		<dependency>
			<groupId>org.aspectj</groupId>
			<artifactId>aspectjrt</artifactId>
			<version>1.9.4</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-aop -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-aop</artifactId>
			<version>2.2.1.RELEASE</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
		<dependency>
			<groupId>org.aspectj</groupId>
			<artifactId>aspectjweaver</artifactId>
			<version>1.9.4</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/cglib/cglib -->
		<dependency>
			<groupId>cglib</groupId>
			<artifactId>cglib</artifactId>
			<version>3.2.5</version>
		</dependency>
  • 创建一个注解用于方法的拦截:
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface ArticleReadCount {}
  • 开始写AOP拦截器代码,首先写一个拦截点,是通注解ArticleReadCount进行拦截的,然后我使用的是方法前拦截(Around、after等均可),获取到文章id后将阅读数增加。
        @Aspect
        @Component
        public class ArticleAspect {
            @Autowired
            private ArticleService articleService;
        
            /**
             * AOP拦截切入点,通过注解ArticleReadCount
             */
            @Pointcut("@annotation(ArticleReadCount)")
            private void articleRead(){};
        
            /**
             * 文章阅读数计算
             * @param joinPoint
             */
            @Before("articleRead()")
            public void ArticleReadCount(JoinPoint joinPoint) {
                Object[] args = joinPoint.getArgs();
                //获取文章id
                Long articleId = (Long)args[0];
                //文章阅读数+1
                articleService.addArticleReadCount(articleId);
            }
        }
  • 最后将注解加入到访问文章的方法上即可实现查看文章阅读量自动增加。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
首先,我们需要定义一个自定义注解 `@RequiresPermissions`,用于标识需要授权访问的方法,例如: ```java @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface RequiresPermissions { String[] value(); // 权限值 } ``` 然后,我们需要实现一个切面,用于拦截被 `@RequiresPermissions` 标识的方法,并进行权限校验,例如: ```java @Component @Aspect public class PermissionCheckAspect { @Autowired private AuthService authService; @Around("@annotation(requiresPermissions)") public Object checkPermission(ProceedingJoinPoint joinPoint, RequiresPermissions requiresPermissions) throws Throwable { // 获取当前用户 User user = authService.getCurrentUser(); if (user == null) { throw new UnauthorizedException("用户未登录"); } // 获取当前用户的权限列表 List<String> permissions = authService.getUserPermissions(user); // 校验权限 for (String permission : requiresPermissions.value()) { if (!permissions.contains(permission)) { throw new ForbiddenException("没有访问权限:" + permission); } } // 执行目标方法 return joinPoint.proceed(); } } ``` 在切面中,我们首先通过 `AuthService` 获取当前用户及其权限列表,然后校验当前用户是否拥有被 `@RequiresPermissions` 标识的方法所需的所有权限,如果没有则抛出 `ForbiddenException` 异常,如果有则继续执行目标方法。 最后,我们需要在 Spring 配置文件中启用 AOP 自动代理,并扫描切面所在的包,例如: ```xml <aop:aspectj-autoproxy /> <context:component-scan base-package="com.example.aspect" /> ``` 这样,我们就通过 Spring AOP 和自定义注解模拟实现了类似 Shiro 权限校验的功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值