先创建一个注解

package com.fchan.hashmapstudy.aspect;public @interface CheckIng {}

aop配置类

在需要aop切入的方法中加上这个注解就可以了

package com.fchan.hashmapstudy.aspect;import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;import javax.servlet.http.HttpServletRequest;@Aspect
@Component
public class CheckIngAspect {//注解的包名全路径@Around("@annotation(com.fchan.hashmapstudy.aspect.CheckIng)")public Object checkIng(ProceedingJoinPoint point) throws Throwable {//通过 RequestContextHolder 获取 HttpServletRequestRequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) requestAttributes;HttpServletRequest httpServletRequest = servletRequestAttributes.getRequest();//从header中获取tokenString token = httpServletRequest.getHeader("x-toekn");//校验toekn,如果校验成功就执行方法,否则就抛异常出去,这里只是为了说明aop配合注解使用,就不多写了return point.proceed();}}

通过RequestContextHolder静态类获取HttpServletRequest

//通过 RequestContextHolder 获取 HttpServletRequest
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) requestAttributes;
HttpServletRequest httpServletRequest = servletRequestAttributes.getRequest();

通过在自定义注解内传入参数,配合aop做用户角色校验

定义一个可以传入自定义参数的注解,然后在aop中配置拦截

自定义注解中要注意参数的类型是有限制的。

  1. A primitive type—基本类型(java的八种基本类型:byte、short、int、long、float、double、char、boolean)

  2. String

  3. Class

  4. An enum type

  5. An annotation type

  6. An array type :类型为以上任一类型的数组

除了以上标示,其他类型编译都会出错: invalid type of annotation member。

package com.fchan.hashmapstudy.aspect;import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;@Retention(RetentionPolicy.RUNTIME)
// RetentionPolicy.RUNTIME 表示CheckUserRole这个注解在运行期可以用反射取到
public @interface CheckUserRole {String value();
}

aop中拦截使用该注解的方法,并且获取注解中配置的参数进行校验

private HttpServletRequest getHttpServletRequest() {RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) requestAttributes;return servletRequestAttributes.getRequest();
}@Around("@annotation(com.fchan.hashmapstudy.aspect.CheckUserRole)")
public Object checkUserRole(ProceedingJoinPoint point) throws Throwable {//获取tokenHttpServletRequest httpServletRequest = getHttpServletRequest();String token = httpServletRequest.getHeader("x-token");//todo token有效期校验//获取请求里提交的roleString requestRole = httpServletRequest.getHeader("role");//获取方法上的注解里的role值,比对看看是否相等,相等则符合要求MethodSignature signature = ((MethodSignature) point.getSignature());Method method = signature.getMethod();// 这里想通过反射获取 CheckUserRole 注解,需要在注解定义的地方加上 @Retention(RetentionPolicy.RUNTIME)CheckUserRole annotation = method.getAnnotation(CheckUserRole.class);String annotationRole = annotation.value();if(!Objects.equals(requestRole, annotationRole)){throw new SecurityException("用户无权访问");}return point.proceed();
}

使用该注解时传入的自定义参数

关于@Retention(RetentionPolicy.RUNTIME)相关的自定义注解可以参考
https://www.cnblogs.com/olmlo/p/3566778.html

aop配合自定义的注解使用,使用RequestContextHolder静态类获取HttpServletRequest相关推荐

  1. 【日志记录】基于AOP实现自定义日志注解,并支持动态设置注解内容

    前言 平时在java项目开发过程中,涉及到记录操作日志的场景很多,有时候大家习惯把操作日志的生成代码写到业务代码中,这样造成了日志和业务代码的耦合性比较高.可维护性也不强,易读性更差,更多的时候是使用 ...

  2. SpringBoot基于AOP实现自定义非空验证的注解

    为了避免对大量参数进行过多的非空校验,我们可以自定义一个非空验证的注解,因为spring自带的@RequestParam并不能对参数进行非空 准备工作 首先需要创建一个spring boot项目,并引 ...

  3. 如何使用 AOP 和自定义注解?

    作者 | 一个程序员的成长 责编 | 胡巍巍 记得今年年初刚开始面试的时候,被问的最多的就是你知道Spring的两大核心嘛?那你说说什么是AOP,什么是IOC?我相信你可能也被问了很多次了. 到底是什 ...

  4. 自定义注解实现权限校验含义_厉害了!老大利用AOP实现自定义注解,半小时完成我三天工作量...

    前面我们已经介绍了AOP适用的一些业务场景以及简单的代码实现,当时我们的切点是用execution表达式来配置的,这种方式有一些局限性在里面: 灵活性不高,一个表达式只能切到某种同类型的方法 个性化不 ...

  5. spring aop拦截自定义注解的切入点表达式

    @within(com.cxh.study.aop.controller.UserAccessAnnotation) 表示拦截含有com.cxh.study.aop.controller.UserAc ...

  6. 使用@Constraint配合自定义注解开发

    前言 通常我们在开发的过程中,需要对前端传入的数据进行校验,尽管这一步已经在前端进行了一次校验,虽然现在已经有了很多校验的注解,@NotNull.@NotBlank.@URL等一系列注解帮助我们进行校 ...

  7. 自定义的注解校验器的实现

    首先先学习一下注解,注解为我们在代码中添加信息提供了一种形式化的方法,使得我们在稍后的某个时刻可以方便地使用这些数据. 在日常的编码中我们一直都在使用注解,只是没有特别关注过,Java中内置了三种注解 ...

  8. slf4j注解log报错_SpringBoot自定义日志注解,用于数据库记录操作日志,你用过吗?...

    大家好,我是程序员7歌! 今天我将为大家讲解如何通过自定义注解记录接口访问日志.一般的开发中,有两种方式可以记录日志信息,第一种:把接口日志信息保存到日志文件中,第二种:把接口操作日志保存到数据库中, ...

  9. 记一次AOP+反射动态修改注解值成功后注解没有生效

    记一次AOP+反射动态修改注解值成功后注解没有生效 最近重新看了一下反射,突发奇想,在运行的时候在不同的方法上放入不同的注解值,然后获取到注解值进行修改.于是拿了hirbernate的@Validat ...

最新文章

  1. 使用中文输入法时对键盘事件的处理
  2. 台式计算机m9870t,JBT9870_水力测功器最新标准规范(14页)-原创力文档
  3. 关于Servlet的原理以及常用类
  4. Mybatis中Mapper动态代理方式
  5. java定义一个方法,向控制台输出一个整数的阶乘
  6. 符号说明表怎么做_电气新手搞不定电气识图怎么办?别慌!8套电气识图教程,秒上手...
  7. log4net 使用手记
  8. 让我们用 SQL 开发一个图形数据库吧
  9. python中confIgparser模块学习
  10. mysql多值存储过程_mysql使用存储过程回来多个值
  11. hdoj--5621--KK's Point(简单数学)
  12. Java之Maven配置教程
  13. 2016hctf writeup
  14. React脚手架项目示例
  15. 【SpringBoot】62、SpringBoot中接入xxl-job实现分布式任务调度
  16. Cassandra Secondary Index 介绍
  17. C语言实现数字全排列
  18. 动手打造N合1操作系统安装光盘
  19. 【CLP】Conic Linear Programming Duality
  20. 最大开源代码sourceforge 简介 及视音频方面常用的开源代码

热门文章

  1. PHP 替换原则,里氏替换原则是什么?
  2. 微软免费杀毒软件Morro开始测试 征求定名
  3. numpy.where和numpy.piecewise的用法
  4. matlab实现 分段线性插值算法 piecewise linear interpolation
  5. 每日一题-9/6-消失的数字
  6. 原生Android系统的第一次开机google验证的解决
  7. 2.27√ 2.25的条件下作两点修改:表AB可能存在值相同的元素,但新生成的表C的元素值各不相同;利用A表空间存放表C
  8. 如何将csdn博客转移至halo博客之中
  9. 【Day3.2】坐在铁轨边午餐
  10. SQL Server2005安装教程