Java aop 实战 Token鉴权

自定义注解

Authorization.java

package cn.zdfy.blogsystem.authorization.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * 在Controller的方法上使用此注解,该方法在映射时会检查用户鉴权
 */
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Authorization {
}

创建aop拦截

package cn.zdfy.blogsystem.authorization.handler;

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.aspectj.lang.reflect.MethodSignature;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
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 java.lang.annotation.Annotation;
import java.lang.reflect.Method;

import javax.servlet.http.HttpServletRequest;

import cn.zdfy.blogsystem.authorization.annotation.Authorization;
import cn.zdfy.blogsystem.config.ResultStatus;
import cn.zdfy.blogsystem.model.ResultModel;

@Aspect
@Component
public class AuthHandle {
    @Pointcut(value = "execution(public * cn.zdfy.blogsystem.controller..*.*(..))")
    public void start() {

    }

    @Around("start()")
    public ResponseEntity<ResultModel> access(ProceedingJoinPoint joinPoint) {
        MethodSignature joinPointObject = (MethodSignature) joinPoint.getSignature();
        //获得请求的方法 
        Method method = joinPointObject.getMethod();
        if (hasAnnotationOnMethod(method, Authorization.class)) {
            String token = (String) getParams("token");
            System.out.println("token ====================== " + token);
            if (token.equals("123456")) {
                System.out.println("token is ok");
            } else {
                return new ResponseEntity<>(ResultModel.error(ResultStatus.TOKEN_ERROR), HttpStatus.OK);
            }
        }
        ResponseEntity obj = null;
        try {
            obj = (ResponseEntity) joinPoint.proceed();
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        return obj;
    }

    /**
     * 获取参数
     *
     * @param key
     * @return
     */
    private Object getParams(String key) {
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) requestAttributes;
        HttpServletRequest request = servletRequestAttributes.getRequest();
        return request.getParameter(key);
    }

    /**
     * 判断某方法上是否含有某注解
     *
     * @param method
     * @param annotationClazz
     * @return
     */
    private boolean hasAnnotationOnMethod(Method method, Class annotationClazz) {
        //使用反射获取注解信息
        Annotation a = method.getAnnotation(annotationClazz);
        if (a == null) {
            return false;
        }
        return true;
    }
}

这里token check简单做个字符串比较说明一下 问题

看一下测试结果

token 正确的
这里写图片描述
token 错误的
这里写图片描述

总结 使用aop可以帮我有效节约 大部分冗余代码 岂不是美滋滋

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值