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的方式 ...
随机推荐
- 关于phpstorm、idea、gogland等等ide全家桶设置
2017-08-29 16:30:55 Preferences => IDE Settings => Editor => Code Completion => Autopopu ...
- 如何快速地恢复你的win10
win10清单 这份List不会介绍如何安装系统,而是当面对一个新系统,如何最快的搭建,或者说恢复到一个生产力环境. 必备习惯 备份软件安装包和常用内容上云是高效恢复的两点关键. 备份软件安装包 我的 ...
- 手机照片的exif里有方向属性
<?php $image = imagecreatefromstring(file_get_contents($_FILES['image_upload']['tmp_name'])); $ex ...
- redis的5种数据类型
卸载服务:redis-server --service-uninstall 开启服务:redis-server --service-start 停止服务:redis-server --service- ...
- Python3 注释和运算符
Python3 注释 确保对模块, 函数, 方法和行内注释使用正确的风格 Python中的注释有单行注释和多行注释: Python中单行注释以 # 开头,例如:: # 这是一个注释 print(&qu ...
- 深入理解TCP建立和关闭连接
建立连接: 理解:窗口和滑动窗口TCP的流量控制TCP使用窗口机制进行流量控制什么是窗口?连接建立时,各端分配一块缓冲区用来存储接收的数据,并将缓冲区的尺寸发送给另一端 接收方发送的确认信息中包含了自 ...
- 【杂谈】从实现角度看ChannelFuture
JDK中的Future特性 在介绍Netty的ChannelFuture之前,我们先来看看JDK中的Future是如何实现的.总的来说就是任务提交的时候会使用装饰器模式,将任务包装成一个FutureT ...
- shell基础知识DAY2
1.管道符(|):把一个命令的输出,把输出的内容传递给管道符后面命令的输入.如:ls -l | grep "^[^d]".2.jobs作业控制,后台运行bg PID,前台运行fg ...
- ASP.NET MVC使用log4net
本文介绍如何在ASP.NET MVC中使用log4net来记录日志文件. 参考链接:https://www.codeproject.com/Articles/823247/How-to-use-Apa ...
- 整理高度塌陷与BFC
当面试官问道你高度塌陷时,人们第一想到的方法一定是 .clearfix::after { content: ''; display: block; clear: both; visibility: h ...