因项目需要与外部对接,为保证接口的安全性需要使用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实现接口验签的更多相关文章

  1. java安全入门篇之接口验签

    文章大纲 一.加密与验签介绍二.接口验签实操三.项目源码下载   一.加密与验签介绍   大多数公共网络是不安全的,一切基于HTTP协议的请求/响应(Request or Response)都是可以被 ...

  2. springboot 2.x整合redis,spring aop实现接口缓存

    pox.xml: <dependency> <groupId>org.springframework.boot</groupId> <artifactId&g ...

  3. 5.3 Spring5源码--Spring AOP使用接口方式实现

    Spring 提供了很多的实现AOP的方式:Spring 接口方式,schema配置方式和注解. 本文重点介绍Spring使用接口方式实现AOP. 使用接口方式实现AOP以了解为目的. 更好地理解动态 ...

  4. 使用spring aop 记录接口日志

    spring配置文件中增加启用aop的配置 <!-- 增加aop 自动代理配置 --> <aop:aspectj-autoproxy /> 切面类配置 package com. ...

  5. php--php调java接口验签

    <?php namespace Fmall_cloud\Model; use Think\Model; class DealJavaModel extends Model { /** * @ti ...

  6. Spring AOP代理时 ClassCastException: $Proxy0 cannot be cast to (类型转换错误)

    Spring AOP代理时 ClassCastException: $Proxy0 cannot be cast to (类型转换错误) 问题: 今天在用AfterReturningAdvice时,a ...

  7. Spring AOP(5)-- 注解

    applicationContext.xml <?xml version="1.0" encoding="UTF-8"?><beans xml ...

  8. Spring AOP 学习(五)

    1. 使用动态代理实现AOP package com.atguigu.spring.aop; import java.lang.reflect.InvocationHandler; import ja ...

  9. 尚硅谷spring aop详解

    spring的aop实现我们采用AspectJ的方式来实现,不采用spring框架自带的aop aspect实现有基于注解的方式,有基于xml的方式,首先我们先讲基于注解的方式,再将基于xml的方式 ...

随机推荐

  1. Spark SQL源码解析(三)Analysis阶段分析

    Spark SQL原理解析前言: Spark SQL源码剖析(一)SQL解析框架Catalyst流程概述 Spark SQL源码解析(二)Antlr4解析Sql并生成树 Analysis阶段概述 首先 ...

  2. (第七篇)系统编码、自启动配置、HOSTNAME、系统启动、定时任务、进程管理、硬盘及其分区

    linux查看系统编码和修改系统编码的方法 查看支持的字符编码 使用locale命令, 如: root@ubuntu:/etc# locale 然后修改/etc/locale.conf,如改成中文编码 ...

  3. [http 1.1] M-POST

    http://www.brainbell.com/tutors/XML/XML_Book_B/Sending_Messages_Using_M_POST.htm You can restrict me ...

  4. mac OS 查看开机/关机/重启记录

    last 查看最近的开关机.登录用户等记录 以及操作时间节点. last | grep reboot 查看重启记录 last | grep shutdown 查看关机记录

  5. Angular 7开发环境配置

    目录 前言 一.搭建项目  1.安装Angular CLI  2.创建项目  3.集成Element Angular 二.设置路由  1.创建路由模块  2.导入.导出RouterModule类  3 ...

  6. mybatis源码学习(四):动态SQL的解析

    之前的一片文章中我们已经了解了MappedStatement中有一个SqlSource字段,而SqlSource又有一个getBoundSql方法来获得BoundSql对象.而BoundSql中的sq ...

  7. 【阅读笔记】Ranking Relevance in Yahoo Search (三)—— query rewriting

    5. QUERY REWRITING 作用: query rewriting is the task of altering a given query so that it will get bet ...

  8. 数学--数论--HDU-2698 Maximum Multiple(规律)

    Given an integer nn, Chiaki would like to find three positive integers xx, yy and zzsuch that: n=x+y ...

  9. 关于RMQ问题的四种解法

    什么是RMQ问题:     RMQ (Range Minimum/Maximum Query):对于长度为n的数组A,回答若干询问RMQ(A,i,j)(i,j<=n-1),返回数组A中下标在i, ...

  10. 一只简单的网络爬虫(基于linux C/C++)————Url处理以及使用libevent进行DNS解析

    Url处理 爬虫里使用了两个数据结构来管理Url 下面的这个数据结构用来维护原始的Url,同时有一个原始Url的队列 //维护url原始字符串 typedef struct Surl { char * ...