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 > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how to add header information to a request in SpringCloud Gateway". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to add header information to a request in SpringCloud Gateway".
Import java.io.UnsupportedEncodingException;import java.net.URLEncoder;import org.springframework.cloud.gateway.filter.GatewayFilterChain;import org.springframework.cloud.gateway.filter.GlobalFilter;import org.springframework.http.server.reactive.ServerHttpRequest;import org.springframework.security.core.context.ReactiveSecurityContextHolder;import org.springframework.security.core.context.SecurityContext;import org.springframework.stereotype.Component;import org.springframework.util.StringUtils;import org.springframework.web.server.ServerWebExchange;import reactor.core.publisher.Mono @ Componentpublic class WebFluxUserRequestInfoFilter implements GlobalFilter {private static final String ORG_CODE = "11000001"; private static final String CHANNEL_CODE = "WEBQHZX001"; private static final String HEADER_USER_INFO_ENCODE = "X-User-Info-Encode"; @ Override public Mono filter (ServerWebExchange exchange, GatewayFilterChain chain) {return ReactiveSecurityContextHolder.getContext () .map (SecurityContext::getAuthentication) .map (authentication-> {UserRequestInfo userInfo = new UserRequestInfo (); Object details = authentication.getDetails () If (details instanceof JWTPlayload) {JWTPlayload jwtPlayload = (JWTPlayload) details; String userId = jwtPlayload.getSub (); userInfo.setUserId (userId); userInfo.setUserName (jwtPlayload.getCname ());} return userInfo;}) .defaultIfEmpty (new UserRequestInfo ()) .flatMap (userInfo-> {UserRequestInfoHolder.setInstance (userInfo)) String userInfoJson; try {String userInfoEncode = exchange.getRequest (). GetHeaders (). GetFirst (HEADER_USER_INFO_ENCODE); if (! StringUtils.isEmpty (userInfoEncode) & & "false" .equals userInfoEncode) {userInfoJson = JsonUtils.object2Json (userInfo) } else {userInfoJson = URLEncoder.encode (JsonUtils.object2Json (userInfo), GlobalConstant.CHARSET);}} catch (UnsupportedEncodingException e) {throw new RuntimeException ("URLEncoder.encode UserRequestInfo failure") } ServerHttpRequest newRequest = exchange.getRequest () .mutate () .header (HeaderDefinition.USER_INFO, userInfoJson) .build (); return chain.filter (exchange.mutate () .request (newRequest) .build ();}) }} Thank you for your reading. The above is the content of "how to add header information to a request in SpringCloud Gateway". After the study of this article, I believe you have a deeper understanding of how to add header information to a request in SpringCloud Gateway, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.