這篇文章主要介紹了SpringBoot+SpringCloud實(shí)現(xiàn)登錄用戶信息在微服務(wù)之間的傳遞,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
在邵東等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強(qiáng)發(fā)展的系統(tǒng)性、市場(chǎng)前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供網(wǎng)站設(shè)計(jì)、網(wǎng)站制作 網(wǎng)站設(shè)計(jì)制作定制制作,公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),成都品牌網(wǎng)站建設(shè),全網(wǎng)營銷推廣,外貿(mào)營銷網(wǎng)站建設(shè),邵東網(wǎng)站建設(shè)費(fèi)用合理。
實(shí)現(xiàn)思路:
1:準(zhǔn)備一個(gè)ThreadLocal變量,供線程之間共享。
2:每個(gè)微服務(wù)對(duì)所有過來的Feign調(diào)用進(jìn)行過濾,然后從請(qǐng)求頭中獲取User用戶信息,并存在ThreadLocal變量中。
3:每個(gè)微服務(wù)在使用FeignClient調(diào)用別的微服務(wù)時(shí),先從ThreadLocal里面取出user信息,并放在request的請(qǐng)求頭中。
4:封裝為一個(gè)注解,在啟動(dòng)類上標(biāo)記即可。
代碼樣例:
1:ThreadLocal工具類 :UserInfoContext
package com.test.domi.common.system; import com.test.domi.dao.UserInfo; public class UserInfoContext { private static ThreadLocaluserInfo = new ThreadLocal (); 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:準(zhǔn)備承載用戶信息的userInfo實(shí)體類(代碼略)
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) { //從應(yīng)用上下文中取出user信息,放入Feign的請(qǐng)求頭中 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("用戶信息設(shè)置錯(cuò)誤",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:編寫注解實(shí)現(xiàn)類: 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 在業(yè)務(wù)端通過注解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:在啟動(dòng)類上標(biāo)記注解即可使用(無侵入)
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); } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。