java - 在 Spring 中如何审计端点通信?
问题描述
我的意图是通过我的应用程序的端点审核数据交换。我想检索通过端点交换的 json 并将其存储在数据库中。为了存储 json 数据,我将使用 Javes ( https://javers.org/ ),但我仍然需要弄清楚如何获取端点数据。
解决方案
您可以编写拦截器来对流经您的应用程序的请求和响应进行额外处理。如果你正在使用spring
它很容易实现。这是一个例子:
@Component
public class RestInterceptor implements ClientHttpRequestInterceptor {
@Override
public ClientHttpResponse intercept(HttpRequest request, byte[] body,
ClientHttpRequestExecution execution) throws IOException {
traceRequest(request, body);
ClientHttpResponse response = execution.execute(request, body);
traceResponse(response);
return response;
}
private void traceRequest(HttpRequest request, byte[] body) throws IOException {
//track request here
}
private void traceResponse(ClientHttpResponse response) throws IOException {
//track response here
}
}
现在用你的休息模板注册它
restTemplate.setInterceptors(Collections.singletonList(new RestInterceptor ()));
推荐阅读
- python - 不断收到此错误“AF_INET 地址必须是元组,而不是套接字”。不知道如何解决它是什么意思
- jenkins - jenkins 中是否有一个插件来限制字符串参数的字符输入?
- r - 关于 R for 循环和 if 语句的一个实际问题
- python - 如何修复`警告:错误的管道:没有元素“疯狂”`
- architecture - 寻找下载大文件的架构
- c++ - 如何在虚拟机上使用 Visual Studio 设置内核调试器
- c++ - 在 C++ 中绘制 Windows 10 壁纸
- azure - azure-arm winrm 使用打包程序超时错误
- python - Context Locals 和 The Request Context 如何协同工作?
- google-sheets - 如何发布谷歌表格插件