refactor(log/httptrace-pro): 重构请求头及响应头信息获取

This commit is contained in:
2023-12-25 20:11:33 +08:00
parent e9f01d05c9
commit 5500e5d840
7 changed files with 32 additions and 19 deletions

View File

@@ -16,6 +16,7 @@
package top.charles7c.continew.starter.core.util;
import cn.hutool.core.map.MapUtil;
import cn.hutool.http.useragent.UserAgent;
import cn.hutool.http.useragent.UserAgentUtil;
import jakarta.servlet.http.HttpServletRequest;
@@ -26,7 +27,7 @@ import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import top.charles7c.continew.starter.core.constant.StringConstants;
import java.util.Objects;
import java.util.*;
/**
* Servlet 工具类
@@ -103,6 +104,21 @@ public class ServletUtils {
return userAgent.getOs().getName();
}
/**
* 获取响应所有的头header信息
*
* @param response 响应对象{@link HttpServletResponse}
* @return header值
*/
public static Map<String, String> getHeaderMap(HttpServletResponse response) {
final Collection<String> headerNames = response.getHeaderNames();
final Map<String, String> headerMap = MapUtil.newHashMap(headerNames.size(), true);
for (String name : headerNames) {
headerMap.put(name, response.getHeader(name));
}
return headerMap;
}
private static ServletRequestAttributes getServletRequestAttributes() {
return (ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes());
}