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的方式 ...
随机推荐
- 一篇文章带你编写10种语言HelloWorld
0,编程语言排行榜 计算机编程语言众多,世界上大概有600 多种编程语言,但是流行的也就几十种.我们来看下编程语言排行榜,下面介绍两种语言排行榜. Ⅰ TIOBE 指数 该指数每月更新一次,它监控了近 ...
- JNI与NDK简析(一)
1 JNI 简介 在Android Framework中,需要提供一种媒介或 桥梁,将Java层(上层)与C/C++层(下层)有机的联系起来,使得他们互相协调完成某些任务.而充当这种媒介的就是Java ...
- js 之 for循环
js之 for循环 普通for 循环 语法 for ([initialization]; [condition]; [final-expression]) statement initializati ...
- php private学习笔记
类的权限修饰符,放在属性/方法的前面.用来说明属性/方法的权限特点. 三种权限修饰符 private 私有的 public 公共 protected 保护的 privata 的属性.方法只能在 ...
- Vue Cli 报错:You are using the runtime-only build of Vue where the template compiler is not availabl
报错原因: 这里引用的是vue.runtime.esm.js,造成的不能正常运行. vue-cli 2.x 解决方法: 在webpack.base.conf.js配置文件中多加了一段代码,将 vue/ ...
- oracle查询当前系统时间前10天的数据
select * from eo_c_order t where t.create_time>systimestamp-interval'1'day; 转载于:https://www.cnblo ...
- 使用mysqldump自动备份数据库脚本
每天利用计划任务在凌晨1点自动执行,备份zabbix的数据库至本地的/backup/mysql_backup目录 #!/bin/sh DUMP=/usr/bin/mysqldump OUT_DIR=/ ...
- linux和windows互传文件、用户配置文件和密码配置文件、用户组管理、用户管理...
linux和windows互传文件 第一种:在linux主机上下载lrzsz软件包 1.yum install lrzsz 2.通过rz命令上传window的文件到linux主机上 用过sz 文件名下 ...
- Frame Relay Voice Traffic Shaping and Frament
本文全称应该是:Frame Relay Voice-Adaptive Traffic Shaping and Fragmentation,标题限制字数,没办法了 帧中继的流量整型向来是个头疼的地方 ...
- 网络流--最大流--POJ 1459 Power Network
#include<cstdio> #include<cstring> #include<algorithm> #include<queue> #incl ...