In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly shows you "how to use SpringBoot interceptor to get header content", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "how to use SpringBoot interceptor to get header content" this article.
Analysis.
Since dynamic acquisition is required, there are only two ways: either to get it from the request header each time the downstream request comes, or to define a unified interceptor to get it automatically.
Realize
Then let's make it happen first.
The first is relatively simple: get the request header directly by using springboot, and from the entrance of the controller method: @ RequestHeader (value = "xxxx", required = false) String appUser.
The code is as follows:
@ RequestMapping (name = "get user details", value = "/ getUserDetail", method=GET) public String getUserDetail (@ RequestHeader (value = "app-user", required = false) String appUser, @ RequestParam (name = "search") String search) {log.info ("- > get the request header is:" + appUser); return "SUCCESS";}
Although this method is available, it is too low b, and it is too cumbersome to write it if there are dozens of interfaces.
Let's use the interceptor.
The details are as follows:
First, define an interceptor, which needs to implement HandlerInterceptor, which contains preHandler and afterCompletion corresponding to before and after the call is completed, respectively.
Package com.xxxx.cc.admin.interceptor; import com.alibaba.fastjson.JSONObject; import com.ddmc.cc.admin.support.ThreadLocalUtils; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang.StringUtils; import org.springframework.stereotype.Component; import org.springframework.web.servlet.HandlerInterceptor; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse / * Copyright (C), 2017-2021 * Author: sjugg* Date: 2021-5-19 10 FileName 39 * FileName: AuthInterceptor* Description: transaction front interceptor * / @ Component @ Slf4j public class AuthInfoInterceptor implements HandlerInterceptor {@ Override public boolean preHandle (HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {String userInfo = request.getHeader ("app-user") Log.info ("> intercepts api-related request headers heard info here:" + ThreadLocalUtils.get ("app-user")); return "SUCCESS";}
After the method is executed, the corresponding threadlocal variable is also cleared.
The above is all the content of the article "how to use SpringBoot's interceptor to get the content of header". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.