• 打印请求头


    1. Map headerMap = new HashMap<>();
    2. Enumeration headerNames = request.getHeaderNames();
    3. while (headerNames.hasMoreElements()) {
    4. String headerName = headerNames.nextElement();
    5. Enumeration headers = request.getHeaders(headerName);
    6. while (headers.hasMoreElements()) {
    7. String headerValue = headers.nextElement();
    8. headerMap.put(headerName, headerValue);
    9. }
    10. }
    1. 请求头拦截器Interceptor
         implements HandlerInterceptor  重写方法
    1. import jakarta.servlet.http.HttpServletRequest;
    2. import jakarta.servlet.http.HttpServletResponse;
    3. import lombok.extern.slf4j.Slf4j;
    4. import org.springframework.web.servlet.HandlerInterceptor;
    5. import org.springframework.web.servlet.ModelAndView;
    6. import java.lang.reflect.Field;
    7. import java.util.Enumeration;
    8. @Slf4j
    9. public class MyInterceptor implements HandlerInterceptor {
    10. @Override
    11. public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
    12. MyHeader myHeader = new MyHeader();
    13. MyHeaderEnum[] myHeaderEnums = MyHeaderEnum.values();
    14. for (MyHeaderEnum myHeaderEnum : myHeaderEnums){
    15. String headerValue = request.getHeader(myHeaderEnum.getVal());
    16. setMyHeaderFiled(myHeader,myHeaderEnum,headerValue);
    17. }
    18. log.info("myHeader:{}",myHeader);
    19. SecurityUtils.setMyHeader(myHeader);
    20. return true;
    21. }
    22. private void setMyHeaderFiled(MyHeader myHeader, MyHeaderEnum myHeaderEnum, String headerValue) throws Exception {
    23. String filedName = myHeaderEnum.getVal();
    24. Class clazz = MyHeader.class;
    25. Field field = clazz.getDeclaredField(filedName);
    26. field.setAccessible(true);
    27. field.set(myHeader,headerValue);
    28. }
    29. private String getRequestHeaders(HttpServletRequest request){
    30. Enumeration headerNames = request.getHeaderNames();
    31. StringBuilder headers = new StringBuilder();
    32. while (headerNames.hasMoreElements()){
    33. String headerName = headerNames.nextElement();
    34. String headerValue = request.getHeader(headerName);
    35. headers.append(headerName).append(": ").append(headerValue).append(", ");
    36. }
    37. if (headers.length() > 0){
    38. headers.setLength(headers.length() - 2);
    39. }
    40. return headers.toString();
    41. }
    42. @Override
    43. public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
    44. HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
    45. }
    46. @Override
    47. public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
    48. SecurityUtils.clearMyHeader();
    49. }
    50. }

    在配置类中注入拦截器

    WebConfig
    1. @Configuration
    2. public class WebConfig implements WebMvcConfigurer {
    3. @Override
    4. public void addInterceptors(InterceptorRegistry registry) {
    5. registry.addInterceptor(clientRequestInterceptor());
    6. }
    7. @Bean
    8. @Scope(value = "request", proxyMode = ScopedProxyMode.TARGET_CLASS)
    9. public MyHeader myHeader(){
    10. return new MyHeader();
    11. }
    12. @Bean
    13. public MyInterceptor clientRequestInterceptor(){
    14. return new MyInterceptor();
    15. }
    16. }

    请求头的枚举类、实体类

    1. @Getter
    2. @AllArgsConstructor
    3. public enum MyHeaderEnum {
    4. USERID("userId"),
    5. ORGID("orgId");
    6. private final String val;
    7. }

    存进线程池

    1. public class SecurityUtils {
    2. private static final ThreadLocal myHeaderThreadLocal = new ThreadLocal<>();
    3. public static MyHeader getMyHeader(){
    4. return myHeaderThreadLocal.get();
    5. }
    6. public static void setMyHeader(MyHeader myHeader){
    7. myHeaderThreadLocal.set(myHeader);
    8. }
    9. public static void clearMyHeader(){
    10. myHeaderThreadLocal.remove();
    11. }
    12. }

  • 相关阅读:
    0047【Edabit ★☆☆☆☆☆】Minimal I: If Boolean Then Boolean
    编译器优化:何为SLP矢量化
    tlaplus-vscode插件使用记录
    java计算机毕业设计废品回收管理系统设计与实现源码+mysql数据库+系统+lw文档+部署
    Java基础-字符串
    JavaScript 变量提升的作用
    闭式旁流水处理器简介
    Vue关键知识点
    java的面向对象基础(1) —— 封装
    互联网应用开发实践:需求分析与数据库设计
  • 原文地址:https://blog.csdn.net/LUOZONGW/article/details/134008304