一般在项目开发中会根据登录人员的权限大小对接口也会设置权限,那么对接口权限是怎么实现的呢,大多数都是用自定义权限注解,只需要在接口上加上一个注解就可以实现对接口的权限拦截,是否对该接口有权调用

接下来我们用一个简单的案例测试一下如何实现自定义权限注解

1、首先,创建一个类,命名随意,这里为MyPermission 

package com.study.permission;
import java.lang.annotation.*;

@Documented //作用域
@Inherited //可继承
@Target(ElementType.METHOD)//标明自定义注解可作用的地方,指方法
@Retention(RetentionPolicy.RUNTIME) //存活阶段,RUNRIME:存在运行期,还有jvm,class文件级别
public @interface MyPermission {
    String username() default "name";
    //是否需要数据权限,默认为true
     boolean required() default true;
}

在此解释一下,以上类中出现的注解

@Target注解

Target注解的作用是:描述注解的使用范围
Target注解对象范围:注解可以用于修饰 packages、types(类、接口、枚举、注解类)、类成员(方法、构造方法、成员变量、枚举值)、方法参数和本地变量(如循环变量、catch参数),它的取值范围定义在ElementType 枚举中,详情可查源码

@Retention注解

Reteniton注解的作用是:描述注解保留的时间范围(被描述的注解在它所修饰的类中可以被保留到何时)

Reteniton注解用来限定那些被它所注解的注解类在注解到其他类上以后,可被保留到何时,一共有三种策略(SOURCE,CLASS,RUNTIME),定义在RetentionPolicy枚举中

@Documented注解
Documented注解的作用是:描述在使用 javadoc 工具为类生成帮助文档时是否要保留其注解信息,有兴趣的看官自己去研究研究

@Inherited注解
Inherited注解的作用是:使被它修饰的注解具有继承性(如果某个类使用了被@Inherited修饰的注解,则其子类将自动具有该注解),有兴趣的看官自己去研究研究

 2、创建一个类MyPermissionAspect并继承Ordered

package com.study.permission;

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.core.Ordered;
import org.springframework.stereotype.Component;
import com.alibaba.fastjson.JSONObject;

import java.lang.reflect.Method;

@Component
@Aspect
public class MyPermissionAspect implements Ordered {
    @Pointcut("execution(* com.study.controller..*(..))")
    public void permissionTest() {

    }
    @Around("permissionTest()")
    public Object doPermission(ProceedingJoinPoint joinPoint) throws Throwable {
        MethodSignature signature = (MethodSignature)joinPoint.getSignature();
        Method method = signature.getMethod();
        MyPermission myPermission = method.getAnnotation(MyPermission.class);
        if(myPermission == null){
            return joinPoint.proceed();
        }

        //判断是否需要数据权限
        boolean required = myPermission.required();
        if (!required) {
            return joinPoint.proceed();
        }

        Object[] args = joinPoint.getArgs();
        if(null == args || args.length == 0){
            return "参数为空";
        }

        JSONObject json = JSONObject.parseObject(String.valueOf(args[0]));
        String username = json.getString(myPermission.username());
        if(!"admin".equals(username)){
            return "权限验证未通过";
        }

        return joinPoint.proceed();
    }

    @Override
    public int getOrder() {
        return 0;
    }
}

3、测试demo,在controller层写测试方法

package com.study.controller;

import com.study.permission.MyPermission;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MainController {

    @RequestMapping("test")
    @MyPermission
    public String permissionTest(@RequestBody String name){
        System.out.println("已通过权限");
        return "success";
    }
    @RequestMapping("test02")
    public String permissionTest02(@RequestBody String name){
        System.out.println("未加权限");
        return "success";
    }
}

在test方法上加权限注解@MyPermission,用postman测试后,传入参数name进行判断拦截,如果是admin则返回success,如果非admin,则显示权限验证未通过

 加上注解,但传的参数为非admin时,显示不通过

测试方法二test02,不加权限注解时,接口应该返回success

 写完了,大致内容就是这样紫了,但是一般情况下进行权限验证时候会携带token的,通过request获取token,然后根据token去查询判断当前用户的信息,再进行逻辑判断,这里全部省略了。。。

12-21 00:56