您好,登錄后才能下訂單哦!
這篇文章主要介紹了SpringBoot+SpringCloud實現登錄用戶信息在微服務之間的傳遞,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
實現思路:
1:準備一個ThreadLocal變量,供線程之間共享。
2:每個微服務對所有過來的Feign調用進行過濾,然后從請求頭中獲取User用戶信息,并存在ThreadLocal變量中。
3:每個微服務在使用FeignClient調用別的微服務時,先從ThreadLocal里面取出user信息,并放在request的請求頭中。
4:封裝為一個注解,在啟動類上標記即可。
代碼樣例:
1:ThreadLocal工具類 :UserInfoContext
package com.test.domi.common.system; import com.test.domi.dao.UserInfo; public class UserInfoContext { private static ThreadLocal<UserInfo> userInfo = new ThreadLocal<UserInfo>(); public static String KEY_USERINFO_IN_HTTP_HEADER = "X-AUTO-FP-USERINFO"; public UserInfoContext() { } public static UserInfo getUser(){ return (UserInfo)userInfo.get(); } public static void setUser(UserInfo user){ userInfo.set(user); } }
2:準備承載用戶信息的userInfo實體類(代碼略)
3:編寫攔截器 : TransmitUserInfoFeighClientIntercepter
package com.test.domi.config; import com.alibaba.fastjson.JSON; import com.test.domi.common.system.UserInfoContext; import com.test.domi.dao.UserInfo; import feign.RequestInterceptor; import feign.RequestTemplate; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.UnsupportedEncodingException; import java.net.URLDecoder; public class TransmitUserInfoFeighClientIntercepter implements RequestInterceptor { private static final Logger log = LoggerFactory.getLogger(TransmitUserInfoFeighClientIntercepter.class); public TransmitUserInfoFeighClientIntercepter() { } @Override public void apply(RequestTemplate requestTemplate) { //從應用上下文中取出user信息,放入Feign的請求頭中 UserInfo user = UserInfoContext.getUser(); if (user != null) { try { String userJson = JSON.toJSONString(user); requestTemplate.header("KEY_USERINFO_IN_HTTP_HEADER",new String[]{URLDecoder.decode(userJson,"UTF-8")}); } catch (UnsupportedEncodingException e) { log.error("用戶信息設置錯誤",e); } } } }
4:編寫過濾器:TransmitUserInfoFilter
package com.test.domi.config; import com.alibaba.fastjson.JSON; import com.test.domi.common.system.UserInfoContext; import com.test.domi.dao.UserInfo; import org.apache.commons.lang.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.servlet.*; import javax.servlet.http.HttpServletRequest; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.net.URLDecoder; public class TransmitUserInfoFilter implements Filter { private static final Logger log = LoggerFactory.getLogger(TransmitUserInfoFeighClientIntercepter.class); public TransmitUserInfoFilter() { } @Override public void init(FilterConfig filterConfig) throws ServletException { } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { this.initUserInfo((HttpServletRequest)request); chain.doFilter(request,response); } private void initUserInfo(HttpServletRequest request){ String userJson = request.getHeader("KEY_USERINFO_IN_HTTP_HEADER"); if (StringUtils.isNotBlank(userJson)) { try { userJson = URLDecoder.decode(userJson,"UTF-8"); UserInfo userInfo = (UserInfo) JSON.parseObject(userJson,UserInfo.class); //將UserInfo放入上下文中 UserInfoContext.setUser(userInfo); } catch (UnsupportedEncodingException e) { log.error("init userInfo error",e); } } } @Override public void destroy() { } }
5:編寫注解實現類: EnableUserInfoTransmitterAutoConfiguration
package spring.cloud.common.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import spring.cloud.common.interceptor.TransmitUserInfoFeighClientInterceptor; import spring.cloud.common.filter.TransmitUserInfoFilter; //@Configuration 在業務端通過注解EanbleUserInfoTransmitter加載 public class EnableUserInfoTransmitterAutoConfiguration { public EnableUserInfoTransmitterAutoConfiguration() { } @Bean public TransmitUserInfoFeighClientInterceptor transmitUserInfo2FeighHttpHeader(){ System.out.println("-----TransmitUserInfoFeighClientInterceptor"); return new TransmitUserInfoFeighClientInterceptor(); } @Bean public TransmitUserInfoFilter transmitUserInfoFromHttpHeader(){ System.out.println("-----TransmitUserInfoFilter"); return new TransmitUserInfoFilter(); } }
6:編寫注解 EnableUserInfoTransmitter
package com.test.domi.annotation; import com.test.domi.config.EnableUserInfoTransmitterAutoConfiguration; import org.springframework.context.annotation.Import; import java.lang.annotation.*; @Documented @Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Import({EnableUserInfoTransmitterAutoConfiguration.class}) public @interface EnableUserInfoTransmitter { }
7:在啟動類上標記注解即可使用(無侵入)
package com.test.domi; import com.test.domi.annotation.EnableUserInfoTransmitter; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @EnableDiscoveryClient @SpringBootApplication @MapperScan("com.test.domi.dao") @EnableUserInfoTransmitter public class TestCommonClient { public static void main(String[] args){ SpringApplication.run(TestCommonClient.class,args); } }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。