Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use SpringBoot's interceptor to get the content of header

Shulou Source: shulou.com Published: 2022-06-01 12:53:32 09月25日 Update

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!

Tags: Interceptor content information method article path code user or details filter learning help encumbrance middle entrance entrance dynamic variable Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei vpn Xiaomi Shulou Technology MariaDB