因项目需要与外部对接,为保证接口的安全性需要使用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. Pytorch手写线性回归

    pytorch手写线性回归 import torch import matplotlib.pyplot as plt from matplotlib.animation import FuncAnim ...

  2. Python pandas库159个常用方法使用说明

    Pandas库专为数据分析而设计,它是使Python成为强大而高效的数据分析环境的重要因素. 一.Pandas数据结构 1.import pandas as pd import numpy as np ...

  3. PHP--关于上传文件大小的问题

    参考:https://www.cnblogs.com/jianqingwang/p/5863960.html https://blog.csdn.net/u013168253/article/deta ...

  4. [Qt]执行cmd命令

    要加 /c 参数 QProcess p; p.start("cmd", QStringList()<<"/c"<<"ping ...

  5. 防cc攻击利器之Httpgrard

    一.httpgrard介绍 HttpGuard是基于openresty,以lua脚本语言开发的防cc攻击软件.而openresty是集成了高性能web服务器Nginx,以及一系列的Nginx模块,这其 ...

  6. bootstrap-导航(垂直分组)

    1.运行效果如图所示 2.实现代码如下 <!DOCTYPE html> <html> <head>     <meta charset="utf-8 ...

  7. 商汤提出解偶检测中分类和定位分支的新方法TSD,COCO 51.2mAP | CVPR 2020

    目前很多研究表明目标检测中的分类分支和定位分支存在较大的偏差,论文从sibling head改造入手,跳出常规的优化方向,提出TSD方法解决混合任务带来的内在冲突,从主干的proposal中学习不同的 ...

  8. 06 ORM常用字段 关系字段 数据库优化查询

    一.Django ORM 常用字段和参数 1.常用字段 models中所有的字段类型其实本质就那几种,整形varchar什么的,都没有实际的约束作用,虽然在models中没有任何限制作用,但是还是要分 ...

  9. tomcat 在linux下启动时找不到JDK

    方案一.  修改bashrc (转载: https://www.cnblogs.com/hongzg1982/articles/2101792.html)  $ vim ~/.bashrc #加入JA ...

  10. 更安全的rm命令,保护重要数据

    更安全的rm命令,保护重要数据 网上流传的安全的rm,几乎都是提供一个rm的"垃圾"回收站,在服务器环境上来说,这实非良方. 我想,提供一个安全的rm去保护一些重要的文件或目录不被 ...