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. 渗透技巧基于Swagger-UI的XSS

    目录 免责声明: 漏洞简述: 漏洞实现 POC 漏洞利用 如何大规模找到 Swagger UI Google FOFA XRAY 修复 免责声明:   本文章仅供学习和研究使用,严禁使用该文章内容对互 ...

  2. GAMES101课程 作业6 源代码概览

    GAMES101课程 作业6 源代码概览 Written by PiscesAlpaca(双鱼座羊驼) 一.概述 本篇将从main函数为出发点,按照各cpp文件中函数的调用顺序和层级嵌套关系,简单分析 ...

  3. 使用vite + vue3 + ant-design-vue + vue-router + vuex 创建一个后台管理应用

    使用vite + vue3 + ant-design-vue + vue-router + vuex 创建一个管理应用的记录 使用vite 创建项目 我创建的node 版本是 v16.17.1 使用N ...

  4. Training: Encodings I

    原题链接:http://www.wechall.net/challenge/training/encodings1/index.php 根据题目信息貌似是让我们用这个JPK来解码,我们先点击JPK去下 ...

  5. 【Datawhale】动手学数据分析

    动手学数据分析 第一章:数据载入及初步观察 载入数据 任务一:导入numpy和pandas import numpy as np import pandas as pd 任务二:载入数据 train_ ...

  6. mongorestore target dump invalid CreateFile dump The system cannot find

    问题 使用 mongorestore 指定 dump 文件夹,恢复数据报错. mongorestore target 'dump' invalid: CreateFile dump: The syst ...

  7. Linux禁止摄像头自动曝光(手动调节曝光)

    前言 很多摄像头具有自动曝光的功能,例如在较暗的调节下,提高曝光率,在较亮的调节下降低曝光.下面简单介绍在linux平台俩种方式来修改自动曝光. 软件调节(图形化界面) 安装qv4l2 sudo ap ...

  8. oracle 内置函数(二)字符函数

    主要函数: 大小写转换函数 获取子字符串函数(字符串截取) 获取字符串长度函数 字符串连接函数 去除子字符串函数 字符替换函数 字符串出现次数 字符串按照特定符号拆分多行 一.大小写转换 1.uppe ...

  9. 《浅谈亚 log 数据结构在 OI 中的应用》阅读随笔

    这篇又长长长了! \(8435\to 8375\to 9729\) 早就馋这篇了!终于学了( 压位 Trie 确实很好写啊 但是总感觉使用范围不是很广的样子 似乎是见的题少 原文里都在用 \(\log ...

  10. 除了 filter 还有什么置灰网站的方式?

    大家都知道,当一些重大事件发生的时候,我们的网站,可能需要置灰,像是这样: 当然,通常而言,全站置灰是非常简单的事情,大部分前端同学都知道,仅仅需要使用一行 CSS,就能实现全站置灰的方式. 像是这样 ...