SpringBoot 中注解方式的拦截过滤
使用场景
公司运行的App 登陆-验证码短信接口,遭到大量的恶意攻击。处于安全的考虑,需要客户端api目前的一些接口加上验证签名的功能,以提高安全性。
现行的App之前也有过签名的秘钥在,后来出于性能考虑,验签功能并没有用上。所以并不是所有的接口都需要验签,只需要要在需要的接口及时加入验签功能即可。
实现步骤
我们运行的api项目,是基于Spring Cloud的一个项目,所以都是基于Spring Boot 的,版本是1.5.3
1.先定义一个注解,我们只需要对需要验签的接口加上注解即可。
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@Documented
public @interface Signature {
String value() default "";
}
2.再写一个扩展org.springframework.web.servlet.handler.HandlerInterceptorAdapter的拦截器 SignatureInterceptor
这里我们只需要重写前置拦截的方法即可
public class SignatureInterceptor extends HandlerInterceptorAdapter {
private final static Logger logger = LoggerFactory.getLogger(SignatureInterceptor.class);
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
Object handler) throws Exception {
if (handler instanceof HandlerMethod) {
HandlerMethod hm = (HandlerMethod) handler;
Signature signature = hm.getMethodAnnotation(Signature.class);
if (signature == null) {
return true;
}
//验证签名的方法
ApiError result = checkSigature(request, response);
if (result == null) {
return true;
}
SimpleResponse simResponse = new SimpleResponse(result, request.getRequestURI());
String strResponseJson = JsonUtil.toJson(simResponse);
response.setContentType("application/json;charset=UTF-8");
try (OutputStream out = response.getOutputStream()) {
out.write(strResponseJson.getBytes("UTF-8"));
out.flush();
}
request.setAttribute(Constants.RESPONSE_BODY_STRING, strResponseJson);
return false;
}
return super.preHandle(request, response, handler);
}
3. 把这个拦截器加入到配置类中
@Configuration
public class WebMvcConfiguration extends WebMvcConfigurerAdapter{
@Bean
@Autowired
public ServletRegistrationBean dispatcherRegistration(DispatcherServlet dispatcherServlet){
ServletRegistrationBean dispatcherRegistration = new ServletRegistrationBean(dispatcherServlet);
dispatcherRegistration.addUrlMappings("*.do");
dispatcherRegistration.addUrlMappings("*.htm");
dispatcherRegistration.addUrlMappings("/*");
dispatcherRegistration.setLoadOnStartup(1);
return dispatcherRegistration;
}
/**
* 通过 @Bean 注入这个 拦截器
* @return
*/
@Bean
public HandlerInterceptor signatureInterceptor(){
return new SignatureInterceptor();
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
// signatureInterceptor 定义 拦截的URL 的类型
registry.addInterceptor(signatureInterceptor()).addPathPatterns("/**")
.excludePathPatterns("/dss/**")
.excludePathPatterns("/mappings","/trace","/info","/metrics","/health","/env","/refresh","/configprops")
.excludePathPatterns("/archaius","/proxy.stream","/hystrix","/hystrix/**","/hystrix.stream")
.excludePathPatterns("/heapdump","/dump")
.excludePathPatterns("/error","/loggers","/loggers/**");
}
@Override
public void configureHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers) {
AppExceptionResolver appExceptionResolver = new AppExceptionResolver();
appExceptionResolver.setOrder(1);
exceptionResolvers.add(appExceptionResolver);
}
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
MMHFastjsonHttpMessageConverter messageConverter = new MMHFastjsonHttpMessageConverter();
messageConverter.setSupportedMediaTypes(Lists.newArrayList(MediaType.APPLICATION_JSON_UTF8));
messageConverter.setDefaultCharset(Charset.forName("UTF-8"));
converters.add(messageConverter);
}
@Override
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
configurer.favorPathExtension(false);
}
}
4. 在需要验签的接口前加入注解@Signature
@RequestMapping(value = {"/V2/sms/vcode/sendSmsVcodeForLoginOrReg.htm","/s/V2/sms/vcode/sendSmsVcodeForLoginOrReg.htm"})
@Signature
public void sendSmsVcodeForLoginOrRegV2() {
responseSuccessJson(response);
}
通过以上4部,再启动项目的时候,拦截器节开始了它的拦截功能,会针对添加了
@Signature的接口,运行前置拦截功能,验签通过才执行接口本来的业务逻辑。
源码分析
本段代码的核心是HandlerInterceptorAdapter这个类,拦截适配器 它提供了4个方法:
- preHandle 预处理,该方法将在请求处理之前进行调用
- postHandle 后置处理, 该方法将在请求处理之后,DispatcherServlet进行视图返回渲染之前进行调用
- afterCompletion 在请求后处理,并在DispatcherServlet进行视图返回渲染之后调用
可以参考看https://blog.csdn.net/qq_35246620/article/details/68487904?1491374806898
SpringBoot 中注解方式的拦截过滤的更多相关文章
- Springboot中注解@Configuration源码分析
Springboot中注解@Configuration和@Component的区别 1.先说结论,@Configuration注解上面有@Component注解,所以@Component有的功能@Co ...
- springboot中使用过滤器、拦截器、监听器
监听器:listener是servlet规范中定义的一种特殊类.用于监听servletContext.HttpSession和servletRequest等域对象的创建和销毁事件.监听域对象的属性发生 ...
- SpringMVC的controller方法中注解方式传List参数使用@RequestBody
在SpringMVC控制器方法中使用注解方式传List类型的参数时,要使用@RequestBody注解而不是@RequestParam注解: //创建文件夹 @RequestMapping(value ...
- 在ssh框架中注解方式需要注意的几个问题
1.注解方式的时候 Spring 2.5 引入了 @Autowired 注释,它可以对类成员变量.方法及构造函数进行标注,完成自动装配的工作. 通过 @Autowired的使用来消除 set ,get ...
- Springboot中静态资源和拦截器处理(踩了坑)
背景: 在项目中我使用了自定义的Filter 这时候过滤了很多路径,当然对静态资源我是直接放过去的,但是,还是出现了静态资源没办法访问到springboot默认的文件夹中得文件 说下默认映射的文件夹有 ...
- SpringBoot使用注解方式整合Redis
1.首先导入使用Maven导入jar包 <dependency> <groupId>org.springframework.boot</groupId> <a ...
- SpringBoot | 问题 | 注解方式下无法发现Bean
在排除注解的问题后,考虑扫描类的位置, [SpringBoot项目的Bean装配默认规则是根据Application类所在的包位置从上往下扫描! “Application类”是指SpringBoot项 ...
- Spring中注解方式实现IOC和AOP
1.IOC注解 1.1 IOC和DI的注解 IOC: @Component:实现Bean组件的定义 @Repository:用于标注DAO类,功能与@Component作用相当 @Service:用 ...
- SpringBoot系列-整合Mybatis(注解方式)
目录 一.常用注解说明 二.实战 三.测试 四.注意事项 上一篇文章<SpringBoot系列-整合Mybatis(XML配置方式)>介绍了XML配置方式整合的过程,本文介绍下Spring ...
随机推荐
- Codeforces1141E(E题)Superhero Battle
A superhero fights with a monster. The battle consists of rounds, each of which lasts exactly nn min ...
- Django之form表单常用字段与插件
from django.shortcuts import render,HttpResponse from django import forms from app01 import models f ...
- ORA-12519,TNS:no appropriate service handler found的问题 超过连接数
http://www.2cto.com/database/201205/133542.html ORA-12519,TNS:no appropriate service handler found的问 ...
- Python 字符串内置函数(四)
# 4.类型判断# isalnum()函数检测字符串是否只由字母和数字组成.s = "this2009"; # 字符中没有空格print(s.isalnum()) # 结果:Tru ...
- UVALive3720
题目大意:见刘汝佳<算法竞赛入门经典——训练指南>P173. 解题思路: 问题可以转化为求共有多少条过点阵中的点的斜线.其中必定包含左斜线和右斜线,由于点阵式对称的,所以我们只需求出左右斜 ...
- java远程执行linux服务器上的shell脚本
业务场景:需要从服务器A中新增的文件同步至本地服务器,服务器A中内存有限,需同步成功之后清除文件. Java调用远程shell脚本,需要和远程服务器建立ssh链接,再调用指定的shell脚本. 1.创 ...
- Ef core 如何设置主键
在正题之前,先说明几个问题. (1)写 sql 不好吗,为什么要引入 ORM ? 总的来说由于需求的复杂性增加,引入了面向对象编程,进而有了 ORM ,ORM 使得开发人员以对象的方式表达业务逻辑.对 ...
- 如何使用JPA的@Formula注解?
背景描述 我们经常会在项目中用到一些数据字典,在存储和传输时使用Code,在前端展示时使用Name,这样做的好处是便于系统维护,比如项目中用到了"医院"这个名称,如果后期需求发生变 ...
- centos7 在docker下安装mongodb
第一步:安装 1.1 查找(查看)mongo相应的版本 [root@localhost ~]# docker search mongo INDEX NAME DESCRIPTION STARS OFF ...
- 关于Backus-Naur Form巴克斯诺尔范式和扩展巴克斯范式的知识点和相关词语中英文对照
巴克斯诺尔范式的相关词语中英文对照和知识点 syntax 语法 强调的是编程语言的组形式,例如一个句子中会包含表达式.陈述还有各种单元等等 semantics 语义 强调的是这个编程语言的实际含义,例 ...