Spring AOP实现接口验签
因项目需要与外部对接,为保证接口的安全性需要使用aop进行方法的验签;
在调用方法的时候,校验外部传入的参数进行验证,
验证通过就执行被调用的方法,验证失败返回错误信息;
不是所有的方法都需要进行验签,所有使用了注解,只对注解的方法才进行验签;
创建ApiAuth注解(Annotation)
@Documented
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface ApiAuth {
String value() default "";
}
如果需要针对class进行验证@Target(ElementType.METHOD) 就需要改成@Target({ElementType.TYPE});
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface ApiAuthForClass {
String id() default "";
}
创建切面类ApiAspect
@Aspect
@Component
public class ApiAspect {
}
spring aop中有这@Around @Before @After 三个注解;
@Before 是在所拦截方法执行之前执行一段逻辑。
@After 是在所拦截方法执行之后执行一段逻辑。
@Around 是可以同时在所拦截方法的前后执行一段逻辑。
我们现在使用@Around,验签通过后执行方法;
@Aspect
@Component
public class ApiAspect {
//@Pointcut("execution(* com.example.demo..*.*(..))")
@Pointcut("@annotation(com.example.demo.common.ApiAuth)")
private void apiAspect() {
}
/**
* @param joinPoint
* @Around是可以同时在所拦截方法的前后执行一段逻辑
*/
@Around("apiAspect()")
public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
// 获取方法请求参数
Object[] objs = joinPoint.getArgs();
String[] argNames = ((MethodSignature) joinPoint.getSignature()).getParameterNames(); // 参数名
Map<String, Object> paramMap = new HashMap<String, Object>();
for (int i = 0; i < objs.length; i++) {
paramMap.put(argNames[i], objs[i]);
}
// 获取注解的值
ApiAuth apiAuth = ((MethodSignature) joinPoint.getSignature()).getMethod().getAnnotation(ApiAuth.class);
String s = apiAuth.value();
// 取得Header传递的基础数据AppKey\Signed\Timestamp
// 验证是否传递了AppKey
// 验证AppKey是否存在
// 判定Url的时间戳调用频率是否过高
// 验证Url的时间戳是否失
// 验证方法是否存在
// 验证方法是否授权操作
// 拦截当前请求的Host地址,并对其进行匹配
// 生成验签
Object result = joinPoint.proceed();
return result;
}
}
接口使用注解
@RestController
@RequestMapping("/api/aspect")
public class ApiAspectController {
@ApiAuth("B2887DFC-3624-4F1A-8F1D-050A4038E728")
@RequestMapping(value = "/api")
public void demo(String name, Integer age) {
}
}
Spring AOP实现接口验签的更多相关文章
- java安全入门篇之接口验签
文章大纲 一.加密与验签介绍二.接口验签实操三.项目源码下载 一.加密与验签介绍 大多数公共网络是不安全的,一切基于HTTP协议的请求/响应(Request or Response)都是可以被 ...
- springboot 2.x整合redis,spring aop实现接口缓存
pox.xml: <dependency> <groupId>org.springframework.boot</groupId> <artifactId&g ...
- 5.3 Spring5源码--Spring AOP使用接口方式实现
Spring 提供了很多的实现AOP的方式:Spring 接口方式,schema配置方式和注解. 本文重点介绍Spring使用接口方式实现AOP. 使用接口方式实现AOP以了解为目的. 更好地理解动态 ...
- 使用spring aop 记录接口日志
spring配置文件中增加启用aop的配置 <!-- 增加aop 自动代理配置 --> <aop:aspectj-autoproxy /> 切面类配置 package com. ...
- php--php调java接口验签
<?php namespace Fmall_cloud\Model; use Think\Model; class DealJavaModel extends Model { /** * @ti ...
- Spring AOP代理时 ClassCastException: $Proxy0 cannot be cast to (类型转换错误)
Spring AOP代理时 ClassCastException: $Proxy0 cannot be cast to (类型转换错误) 问题: 今天在用AfterReturningAdvice时,a ...
- Spring AOP(5)-- 注解
applicationContext.xml <?xml version="1.0" encoding="UTF-8"?><beans xml ...
- Spring AOP 学习(五)
1. 使用动态代理实现AOP package com.atguigu.spring.aop; import java.lang.reflect.InvocationHandler; import ja ...
- 尚硅谷spring aop详解
spring的aop实现我们采用AspectJ的方式来实现,不采用spring框架自带的aop aspect实现有基于注解的方式,有基于xml的方式,首先我们先讲基于注解的方式,再将基于xml的方式 ...
随机推荐
- TensorFlow-keras fit的callbacks参数,定值保存模型
from tensorflow.python.keras.preprocessing.image import load_img,img_to_array from tensorflow.python ...
- LeetCode466. Count The Repetitions
题目链接 传送门 题意 定义一个特殊的串, 现在给出串S1和S2的参数, 问: S2最多可以多少个连接起来扔是S1的子序列, 求出这个最大值 解题思路 注意s1与S1的区别, 可以去看题目描述, 预处 ...
- 来自BAT大厂前端工程师的自白-怎么才能学好前端
如果说理解学好web前端是先能找到一份工作,那么你应该这样做: 1.制定好一下系统的web前端学习规划,每天定量,学完什么知识点就掌握,能自己应用,而不是能看懂,写不出来东西. 2.不要自己一个人闷头 ...
- [Inno Setup] 字符串列表,当要处理一长串文件时很有用
https://wiki.freepascal.org/TStringList-TStrings_Tutorial TStringList-TStrings Tutorial │ Deutsch (d ...
- windows右键没有新建选项的解决办法
1 以管理员身份运行cmd 2 cmd /k reg add "HKEY_CLASSES_ROOT\Directory\Background\shellex\ContextMenuHandl ...
- SQL SERVER 性能优化二: 数据库初始值大小及增长方式设置
数据库增长方式主要有两种,按百分比自动增长和按固定大小自动增长,设置初始大小和增长方式需谨慎. 初始大小就是建库的大小,设小了,容易造成磁盘碎片,频繁增长也会影响IO响应.设大了,也不行,设大了,每次 ...
- Python2 与 Python3 的区别
python解释器默认编码(python2与python3的区别一) python2 解释器默认编码:ascii python3 解释器默认编码:utf-8 输入(python2与python3的区别 ...
- 基于tp3开发的cms-前台代码执行
内核 thinkphp PbootCMS-V1.2.1 ├─ apps 应用程序 │ ├─ admin 后台模块 │ ├─ api api模块 │ ├ ...
- Constant Palindrome Sum(贪心*RMQ)
传送门 怎么说呢,想了几个小时没做出来实在可惜. \(\color{Red}{首先肯定想到暴力嘛!但是x定值有那么多值可以取,怎么办呢?}\) 但是题目中有一个很关键的条件 \[a[i]>=1\ ...
- SpringBoot:整合Shiro
目录 1.Shiro简介 1.1.什么是Shiro? 1.2.有哪些功能 1.3.Shiro架构(外部) 1.4.Shiro架构(内部) 2.HelloWorld 3.Shiro整合Spring Bo ...