- Map
headerMap = new HashMap<>(); - Enumeration
headerNames = request.getHeaderNames(); -
- while (headerNames.hasMoreElements()) {
- String headerName = headerNames.nextElement();
- Enumeration
headers = request.getHeaders(headerName); - while (headers.hasMoreElements()) {
- String headerValue = headers.nextElement();
- headerMap.put(headerName, headerValue);
- }
-
- }
implements HandlerInterceptor 重写方法
- import jakarta.servlet.http.HttpServletRequest;
- import jakarta.servlet.http.HttpServletResponse;
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.web.servlet.HandlerInterceptor;
- import org.springframework.web.servlet.ModelAndView;
-
- import java.lang.reflect.Field;
- import java.util.Enumeration;
-
- @Slf4j
- public class MyInterceptor implements HandlerInterceptor {
-
- @Override
- public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
- MyHeader myHeader = new MyHeader();
- MyHeaderEnum[] myHeaderEnums = MyHeaderEnum.values();
- for (MyHeaderEnum myHeaderEnum : myHeaderEnums){
- String headerValue = request.getHeader(myHeaderEnum.getVal());
- setMyHeaderFiled(myHeader,myHeaderEnum,headerValue);
- }
- log.info("myHeader:{}",myHeader);
- SecurityUtils.setMyHeader(myHeader);
- return true;
- }
-
- private void setMyHeaderFiled(MyHeader myHeader, MyHeaderEnum myHeaderEnum, String headerValue) throws Exception {
- String filedName = myHeaderEnum.getVal();
- Class> clazz = MyHeader.class;
- Field field = clazz.getDeclaredField(filedName);
- field.setAccessible(true);
- field.set(myHeader,headerValue);
-
- }
-
- private String getRequestHeaders(HttpServletRequest request){
- Enumeration
headerNames = request.getHeaderNames(); - StringBuilder headers = new StringBuilder();
- while (headerNames.hasMoreElements()){
- String headerName = headerNames.nextElement();
- String headerValue = request.getHeader(headerName);
- headers.append(headerName).append(": ").append(headerValue).append(", ");
- }
- if (headers.length() > 0){
- headers.setLength(headers.length() - 2);
- }
- return headers.toString();
- }
- @Override
- public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
- HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
- }
-
- @Override
- public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
- SecurityUtils.clearMyHeader();
- }
- }
在配置类中注入拦截器
WebConfig
- @Configuration
- public class WebConfig implements WebMvcConfigurer {
-
- @Override
- public void addInterceptors(InterceptorRegistry registry) {
- registry.addInterceptor(clientRequestInterceptor());
- }
-
- @Bean
- @Scope(value = "request", proxyMode = ScopedProxyMode.TARGET_CLASS)
- public MyHeader myHeader(){
- return new MyHeader();
- }
-
- @Bean
- public MyInterceptor clientRequestInterceptor(){
- return new MyInterceptor();
- }
- }
请求头的枚举类、实体类
- @Getter
- @AllArgsConstructor
- public enum MyHeaderEnum {
- USERID("userId"),
- ORGID("orgId");
-
- private final String val;
- }
存进线程池
-
- public class SecurityUtils {
- private static final ThreadLocal
myHeaderThreadLocal = new ThreadLocal<>(); -
- public static MyHeader getMyHeader(){
- return myHeaderThreadLocal.get();
- }
-
- public static void setMyHeader(MyHeader myHeader){
- myHeaderThreadLocal.set(myHeader);
- }
-
- public static void clearMyHeader(){
- myHeaderThreadLocal.remove();
- }
- }