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

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

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去查询判断当前用户的信息,再进行逻辑判断,这里全部省略了。。。

最新文章

  1. EntityFramework 7 Migrations 迁移命令
  2. video 播放
  3. (转) C#多线程赛跑实例
  4. 我关于SecureCRT远程连接失败的问题解决办法
  5. WCF入门(五)---创建WCF服务
  6. 20140719中国互联网公司市值排名TOP20
  7. Debian上安装java
  8. Android实战之ListView复选框
  9. php java aes
  10. Ubuntu下vim打开文件时,提示请按ENTER或其它命令继续
  11. ocr智能图文识别 tess4j 图文,验证码识别
  12. Restful API学习Day4 - DRF版本控制和认证
  13. python之logging
  14. Mybatis入门及于hibernate的区别
  15. Ubuntu 下 Tomcat7 的安装和配置
  16. Django配置xadmin后台模板之坑(一)
  17. MOngoDB为现有数据添加或删除某一字段
  18. spring4 quartz2 集群动态任务
  19. C++实现String容器的基本功能
  20. servlet Servlet例子

热门文章

  1. 把本地的jar包导入到本地的maven仓库,Eclipse可以使用
  2. 轮询本机所有网卡的IP地址
  3. Java IO: 其他字符流(下)
  4. algorithm-question
  5. 吴裕雄--天生自然python学习笔记:Python3 OS 文件/目录方法
  6. idea快捷键-eclipse
  7. Elays'Blog
  8. iPhone X价格下跌!用户依旧冷眼相看为哪般?
  9. react 踩坑记
  10. “代码量统计脚本”