package com.xf.config;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Type;
import java.util.List; import javax.annotation.Priority; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.MethodParameter;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpInputMessage;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.servlet.mvc.method.annotation.RequestBodyAdvice; import com.xf.common.NoSecret;
import com.xf.service.CacheService;
import com.xf.tools.AesUtils;
import com.xf.tools.HttpServletRequestUtil;
import com.xf.tools.JWTUtils; import cn.hutool.core.io.IoUtil;
import lombok.val; @ControllerAdvice("com.xf.controller")
@Priority(1)
public class MyRequestBodyAdvice implements RequestBodyAdvice { @Autowired
CacheService cacheService; @Override
public boolean supports(MethodParameter methodParameter, Type targetType,
Class<? extends HttpMessageConverter<?>> converterType) {
return methodParameter.hasParameterAnnotation(RequestBody.class);
} @Override
public HttpInputMessage beforeBodyRead(HttpInputMessage inputMessage, MethodParameter parameter, Type targetType,
Class<? extends HttpMessageConverter<?>> converterType) throws IOException {
val request = HttpServletRequestUtil.getRequest();
if (request.getServletPath().contains("login"))
return inputMessage;
if (!parameter.getMethod().isAnnotationPresent(NoSecret.class)) {
return new HttpInputMessage() {
@Override
public InputStream getBody() throws IOException {
List<String> tokenList = inputMessage.getHeaders().get("token");
if (tokenList.isEmpty()) {
throw new RuntimeException("缺少必要参数");
}
String token = tokenList.get(0);
Integer accid = JWTUtils.getAccid(token);
String bodyStr = IoUtil.read(inputMessage.getBody(), "utf-8");
val req = HttpServletRequestUtil.getRequest();
// Console.log(HttpServletRequestUtil.getAllRequestInfo());
// Console.log(bodyStr);
val loginObj = cacheService.getLogin(accid);
val key = loginObj.getString("key");
val iv = loginObj.getString("iv");
req.setAttribute("key", key);
req.setAttribute("iv", iv);
try {
bodyStr = AesUtils.decrypt(bodyStr, key, iv);
} catch (Exception e) {
throw new RuntimeException("解密失败");
}
return IoUtil.toStream(bodyStr, "utf-8");
} @Override
public HttpHeaders getHeaders() {
return inputMessage.getHeaders();
}
};
}
return null;
} @Override
public Object afterBodyRead(Object body, HttpInputMessage inputMessage, MethodParameter parameter, Type targetType,
Class<? extends HttpMessageConverter<?>> converterType) {
return body;
} @Override
public Object handleEmptyBody(Object body, HttpInputMessage inputMessage, MethodParameter parameter,
Type targetType, Class<? extends HttpMessageConverter<?>> converterType) {
return body;
} }
package com.xf.config;

import java.util.HashMap;

import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice; import com.alibaba.fastjson.JSONObject;
import com.xf.common.R;
import com.xf.tools.AesUtils;
import com.xf.tools.HttpServletRequestUtil; import lombok.val; @ControllerAdvice("com.xf.controller")
public class MyResponseBodyAdvice implements ResponseBodyAdvice { @Override
public boolean supports(MethodParameter returnType, Class converterType) {
return true;
} @Override
public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType,
Class selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {
if (body instanceof R) {
R tem = (R) body;
// 前端非得强行有个data
if (!tem.containsKey("data"))
tem.put("data", new HashMap<String, Object>());
val req = HttpServletRequestUtil.getRequest();
if (req.getServletPath().contains("login"))
return body;
val key = req.getAttribute("key").toString();
val iv = req.getAttribute("iv").toString();
// return tem;
try {
return AesUtils.encrypt(JSONObject.toJSONString(tem), key, iv);
} catch (Exception e) {
return null;
}
}
return body;
}
}

@ControllerAdvice解密请求,加密响应的更多相关文章

  1. Python爬虫-02:HTTPS请求与响应,以及抓包工具Fiddler的使用

    目录 1. HTTP和HTTPS 1.1. HTTP的请求和响应流程:打开一个网页的过程 1.2. URL 2. 客户端HTTP请求 3. Fiddler抓包工具的使用 3.1. 工作原理 3.2. ...

  2. Websocket - Websocket原理(握手、解密、加密)、基于Python实现简单示例

    一.Websocket原理(握手.解密.加密) WebSocket协议是基于TCP的一种新的协议.WebSocket最初在HTML5规范中被引用为TCP连接,作为基于TCP的套接字API的占位符.它实 ...

  3. python爬虫(二)_HTTP的请求和响应

    HTTP和HTTPS HTTP(HyperText Transfer Protocol,超文本传输协议):是一种发布和接收HTML页面的方法 HTTPS(HyperText Transfer Prot ...

  4. CoolBlog开发笔记第5课:请求与响应

    教程目录 1.1 CoolBlog开发笔记第1课:项目分析 1.2 CoolBlog开发笔记第2课:搭建开发环境 1.3 CoolBlog开发笔记第3课:创建Django应用 1.4 CoolBlog ...

  5. Java Servlet (1) —— Filter过滤请求与响应

    Java Servlet (1) -- Filter过滤请求与响应 版本: Java EE 6 参考来源: Oracle:The Java EE 6 Tutorial: Filtering Reque ...

  6. 02-Http请求与响应全解

    什么是协议 约束双方规范的一个准则 什么是HTTP协议 HTTP,超文本传输协议(HyperText Transfer Protocol)是互联网上应用最为广泛的一种网络协议 所有的WWW文件都必须遵 ...

  7. HTTP和HTTPS的请求和响应

    HTTP协议(HyperText Transfer Protocol,超文本传输协议):是一种发布和接收 HTML页面的方法.HTTPS(Hypertext Transfer Protocol ove ...

  8. HTTPS的请求与响应

    HTTP和HTTPS HTTP协议(HyperText Transfer Protocol,超文本传输协议):是一种发布和接收 HTML页面的方法. HTTPS(Hypertext Transfer ...

  9. Flask(1):基本示例、配置文件、路由、请求和响应、模板渲染

    Flask的特点: - pip install flask - 短小精悍.可扩展性强的 web框架 注意:上下文管理机制 - 依赖 wsgi:werkzeug Flask的简单示例: from fla ...

  10. Python爬虫开发【第1篇】【HTTP与HTTPS请求与响应】

    一.HTTP.HTTPS介绍 HTTP协议(超文本传输协议):一种发布.接收HTML页面的方法 HTTPS协议:简单讲是HTTP安全版,在HTTP下加入SSL层 SSL(安全套接层),用于WEB的安全 ...

随机推荐

  1. Pwn学习随笔

    Pwn题做题流程 使用checksec检查ELF文件保护开启的状态 IDApro逆向分析程序漏洞(逻辑复杂的可以使用动态调试) 编写python的exp脚本进行攻击 (若攻击不成功)进行GDB动态调试 ...

  2. OpenCvSharp的安装和使用

    OpencvSharp是opencv的C#版本,使用习惯了opencv的人学起OpenCvSharp会很容易上手,看了网上很多的安装方式,最后我感觉还是自己去下载安装包的方式最简单,通过Nuget的方 ...

  3. Go语言核心36讲32

    你好,我是郝林,今天我们继续分享原子操作的内容. 我们接着上一篇文章的内容继续聊,上一篇我们提到了,sync/atomic包中的函数可以做的原子操作有:加法(add).比较并交换(compare an ...

  4. 你听说过OTA吗?

    以下内容为本人的学习笔记,如需要转载,请声明原文链接微信公众号「englyf」https://mp.weixin.qq.com/s/M660Sc4ey1SxCpRT6vcM9g what? 不知有多少 ...

  5. 8 STL-stack

    ​ 重新系统学习c++语言,并将学习过程中的知识在这里抄录.总结.沉淀.同时希望对刷到的朋友有所帮助,一起加油哦!  生命就像一朵花,要拼尽全力绽放!死磕自个儿,身心愉悦! 写在前面,本篇章主要介绍S ...

  6. uni 结合vuex 编写动态全局配置变量 this.baseurl

    在日常开发过程,相信大家有遇到过各种需求,而我,在这段事件便遇到了一个,需要通过用户界面配置动态接口,同时,因为是app小程序开发,所以接口中涉及到了http以及websocket两个类型的接口. 同 ...

  7. JDK中内嵌JS引擎介绍及使用

    原文: JDK中内嵌JS引擎介绍及使用 - Stars-One的杂货小窝 最近研究阅读这个APP,其主要功能就是通过一个个书源,从而实现移动端阅读的体验 比如说某些在线小说阅读网站,会加上相应的广告, ...

  8. 【Linux】通过Crontab和shell脚本实现定期备份和删除PG数据库表数据

    〇.参考资料 一.Crontab使用 1.查看状态 service crond status 2.新建crontab任务 crontab -e 输入字符串 * * * * * cd /home/big ...

  9. 【大数据课程】高途课程实践-Day01:Python编写Map Reduce函数实现各商品销售量展示(类似wordcount)

    〇.概述 1.工具 http://www.dooccn.com/python3/ 在线运行Python代码 2.步骤 (1)⽣成代码测试数据 (2)编写Mapper逻辑 (3)编写Reducer逻辑 ...

  10. 【Java SE进阶】Day12 函数式接口、函数式编程(Lambda表达式)

    一.函数式接口介绍 1.概念 仅有一个抽象方法的接口 适用于函数式编程(Lambda使用的接口) 语法糖:方便但原理不变,如for-each是Iterator的语法糖 Lambda≈匿名内部类的语法糖 ...