使用AOP+自定义注解完成spring boot的接口权限校验
记使用AOP+自定义注解完成接口的权限校验,代码如下:
pom文件添加所需依赖:
1 <dependency>
2 <groupId>org.aspectj</groupId>
3 <artifactId>aspectjrt</artifactId>
4 <version>1.8.9</version>
5 </dependency>
6 <dependency>
7 <groupId>org.springframework.boot</groupId>
8 <artifactId>spring-boot-starter-aop</artifactId>
9 </dependency>
先自定义注解@MyAnnotation,注解中可以设置所需参数:
1 package com.itcq.aop;
2
3 import java.lang.annotation.*;
4
5 //定义注解可以使用的范围
6 @Target({ElementType.TYPE, ElementType.METHOD})
7 @Retention(RetentionPolicy.RUNTIME)
8 @Documented
9 public @interface MyAnnotation {
10
11 String name();
12 }
定义解析注解的MyAnnotationService,完成接口权限校验的逻辑,这里我是获取接口请求时header中的user_name参数,进行校验:
1 package com.itcq.aop;
2
3 import lombok.extern.slf4j.Slf4j;
4 import org.aspectj.lang.ProceedingJoinPoint;
5 import org.aspectj.lang.Signature;
6 import org.aspectj.lang.annotation.Around;
7 import org.aspectj.lang.annotation.Aspect;
8 import org.aspectj.lang.annotation.Pointcut;
9 import org.springframework.stereotype.Component;
10 import org.springframework.web.context.request.RequestContextHolder;
11 import org.springframework.web.context.request.ServletRequestAttributes;
12
13 import javax.servlet.http.HttpServletRequest;
14
15 @Aspect
16 @Component
17 @Slf4j
18 public class MyAnnotationService {
19
20 //切入点表达式决定了用注解方式的方法切还是针对某个路径下的所有类和方法进行切,方法必须是返回void类型
21 @Pointcut("@annotation(com.itcq.aop.MyAnnotation)")
22 private void roleCheckCut() {};
23
24 //定义了切面的处理逻辑。即方法上加了@MyAnnotation注解,将会进行权限校验
25 @Around("roleCheckCut()")
26 public Object operateAuth(ProceedingJoinPoint pjp) throws Throwable {
27
28 //打印日志
29 Signature signature = pjp.getSignature();
30 String className = pjp.getTarget().getClass().getSimpleName();
31 String methodName = signature.getName();
32 log.info("className:{},methodName:{}", className, methodName);
33
34 //获取接口请求时header中的user_name参数,进行校验
35 HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
36 String userName = request.getHeader("user_name");
37 //这里可以Apollo配置可以放行的角色
38 if (!"hwy".equals(userName)) {
39 throw new Exception(userName+"权限校验不通过");
40 }
41 return pjp.proceed();
42 }
43 }
最后在controller层中编写测试方法,利用postman测试接口:
1 package com.itcq.controller;
2
3 import com.itcq.aop.MyAnnotation;
4 import com.itcq.service.TestService;
5 import org.springframework.beans.factory.annotation.Autowired;
6 import org.springframework.web.bind.annotation.GetMapping;
7 import org.springframework.web.bind.annotation.RequestHeader;
8 import org.springframework.web.bind.annotation.RequestParam;
9 import org.springframework.web.bind.annotation.RestController;
10
11 @RestController
12 public class TestController {
13
14 @Autowired
15 private TestService testService;
16
17 @GetMapping("/test")
18 @MyAnnotation(name = "HWY")
19 public String testMethod(@RequestHeader(name = "user_name") String userName,
20 @RequestParam(name = "user_age") Integer userAge) {
21
22 return testService.testMethod(userName, userAge);
23 }
24 }
两种不同请求参数的测试结果如下:
参数正确时的返回结果:

参数不正确时的返回结果,接口报错,控制台输出:


可以发现使用AOP+自定义注解的形式完成了接口的权限校验,当然这只算是比较初级的应用,AOP+自定义注解还有很大的探索空间。
使用AOP+自定义注解完成spring boot的接口权限校验的更多相关文章
- redis分布式锁-spring boot aop+自定义注解实现分布式锁
接这这一篇redis分布式锁-java实现末尾,实现aop+自定义注解 实现分布式锁 1.为什么需要 声明式的分布式锁 编程式分布式锁每次实现都要单独实现,但业务量大功能复杂时,使用编程式分布式锁无疑 ...
- 利用Spring AOP自定义注解解决日志和签名校验
转载:http://www.cnblogs.com/shipengzhi/articles/2716004.html 一.需解决的问题 部分API有签名参数(signature),Passport首先 ...
- spring AOP自定义注解方式实现日志管理
今天继续实现AOP,到这里我个人认为是最灵活,可扩展的方式了,就拿日志管理来说,用Spring AOP 自定义注解形式实现日志管理.废话不多说,直接开始!!! 关于配置我还是的再说一遍. 在appli ...
- spring AOP自定义注解 实现日志管理
今天继续实现AOP,到这里我个人认为是最灵活,可扩展的方式了,就拿日志管理来说,用Spring AOP 自定义注解形式实现日志管理.废话不多说,直接开始!!! 关于配置我还是的再说一遍. 在appli ...
- (转)利用Spring AOP自定义注解解决日志和签名校验
一.需解决的问题 部分API有签名参数(signature),Passport首先对签名进行校验,校验通过才会执行实现方法. 第一种实现方式(Origin):在需要签名校验的接口里写校验的代码,例如: ...
- SpringBoot系列(十三)统一日志处理,logback+slf4j AOP+自定义注解,走起!
往期精彩推荐 SpringBoot系列(一)idea新建Springboot项目 SpringBoot系列(二)入门知识 springBoot系列(三)配置文件详解 SpringBoot系列(四)we ...
- AOP自定义注解鉴权
刚出来工作那会或者在学校的时候,经常听到说AOP(面向对象编程,熟称切面)的用途是日志.鉴权等.但是那会不会,后面学会了,又没有写博客记录,今天写给大伙,希望能帮到大家 一.学习目标:利用AOP+自定 ...
- ssm+redis整合(通过aop自定义注解方式)
此方案借助aop自定义注解来创建redis缓存机制. 1.创建自定义注解类 package com.tp.soft.common.util; import java.lang.annotation.E ...
- spring mvc注解和spring boot注解
1 spring mvc和spring boot之间的关系 spring boot包含spring mvc.所以,spring mvc的注解在spring boot总都是可以用的吗? spring b ...
随机推荐
- 如何从二维平面n个点中寻找距离最近两个点?
如何理解分治算法 什么是分治算法?简单来说就是"分而治之",也就是将原问题划分成n个规模较小的,并且结构与原问题相似的子问题,然后去递归地解决这些子问题,最后再合并其结果,就得到原 ...
- 为什么大部分的Android开发成为不了架构师
小团队一般 10 人左右,其中常常是技术最牛的人做架构师(或TL).所以,架构师在广大码农中的占比大概平均不到 10%.而架构师也可以分为初级.中级.高级三档,江湖上真正高水平的软件架构师就更少了. ...
- lerna 常用命令
lerna 介绍 lerna 处理机构 固定模式(fixed) 所有包是统一的版本号,每次升级,所有包版本统一更新,不管这个包内容改变与否 具体体现在,lerna 的配置文件 lerna.json 中 ...
- 关于java新特性lambda表达式的理解即使用
Lambda 表达式的使用 1.举例: (o1,o2) -> Integer.compare(o1,o2); 2.格式: -> : lambda操作符 或 箭头操作符 ->左边 : ...
- JSON.stringify()的用法
**JSON.stringify() 方法用于将 JavaScript 值转换为 JSON 字符串,而我们一般只是用了第一个参数,没有在意过第二个以及第三个参数的妙用** **1.最常用的方式:** ...
- Mysql数据库优化(1)
1.尽量不要留null select id from t where num is null,可以,但尽量不要留null,null也占空间:使用not null填充数据库,像varchar(100)这 ...
- 嵌入式Linux可用的防火墙——iptables:实现ip白名单、mac地址白名单
iptables是linux系统下的一个功能强大的模块,不仅可以用作防火墙,还可以实现NAT等众多路由功能.iptables的容器有很清晰的层次关系: 1. iptables是表的容器,iptable ...
- Asp.Net Core Razor页面中使用echarts展示图形
Asp.Net Core Razor页面中使用echarts展示图形 要在Razor页面中使用echarts显示图形,主要问题点在于如何将数据传递给js文件. 1,下载安装echarts库文件 首先引 ...
- Linux 分区扩容(根分区扩容,SWAP 分区扩容,挂载新分区为目录)
请访问原文链接:https://sysin.org/blog/linux-partition-expansion/,查看最新版.原创作品,转载请保留出处. 作者:gc(at)sysin.org,主页: ...
- HTML <form> 标签的 method 属性
定义和用法 method 属性规定如何发送表单数据(表单数据发送到 action 属性所规定的页面). 表单数据可以作为 URL 变量(method="get")或者 HTTP p ...