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

先创建一个注解

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 获取 HttpServletRequest
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) requestAttributes;
        HttpServletRequest httpServletRequest = servletRequestAttributes.getRequest();

        //从header中获取token
        String 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 {
    //获取token
    HttpServletRequest httpServletRequest = getHttpServletRequest();
    String token = httpServletRequest.getHeader("x-token");
    //todo token有效期校验

    //获取请求里提交的role
    String 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

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值