SpringCloud解決feign調(diào)用token丟失問題解決辦法
背景討論
feign請求
在微服務(wù)環(huán)境中,完成一個(gè)http請求,經(jīng)常需要調(diào)用其他好幾個(gè)服務(wù)才可以完成其功能,這種情況非常普遍,無法避免。那么就需要服務(wù)之間的通過feignClient發(fā)起請求,獲取需要的 資源。
認(rèn)證和鑒權(quán)
一般而言,微服務(wù)項(xiàng)目部署環(huán)境中,各個(gè)微服務(wù)都是運(yùn)行在內(nèi)網(wǎng)環(huán)境,網(wǎng)關(guān)服務(wù)負(fù)責(zé)請求的路由,對外通過nginx暴露給請求者。
這種情況下,似乎網(wǎng)關(guān)這里做一個(gè)認(rèn)證,就可以確保請求者是合法的,至于微服務(wù)調(diào)用微服務(wù),反正都是自己人,而且是內(nèi)網(wǎng),無所謂是否驗(yàn)證身份了。
我有一個(gè)朋友,他們公司的項(xiàng)目確實(shí)就是這樣做的,正經(jīng)的商業(yè)項(xiàng)目。
講道理,只要框架提供了這樣的功能,那么就有存在的意義,但是,如果涉及權(quán)限的校驗(yàn),微服務(wù)之間的feign調(diào)用就需要知道身份了,即需要做鑒權(quán)。
token
無論是JWT、還是OAUTH2、還是shiro,大家比較公認(rèn)的認(rèn)證、鑒權(quán)方案,就是在請求頭中放一堆東西,然后服務(wù)提供者通過解析這些東西完成認(rèn)證和鑒權(quán),這些東西俗稱token。
在feign調(diào)用中需要解決的就是token傳遞的問題,只有請求發(fā)起者將正確的token傳遞給服務(wù)提供者,服務(wù)提供者才能完成認(rèn)證&鑒權(quán),進(jìn)而返回需要的資源。
問題描述
在feign調(diào)用中可能會遇到如下問題:
- 同步調(diào)用中,token丟失,這種可以通過創(chuàng)建一個(gè)攔截器,將token做透傳來解決
- 異步調(diào)用中,token丟失,這種就無法直接透傳了,因?yàn)樽泳€程并沒有token,這種需要先將token從父線程傳遞到子線程,再進(jìn)行透傳
解決方案
token透傳
編寫一個(gè)攔截器,在feign請求前,將http請求攜帶的token傳遞給restTemplate。
具體實(shí)現(xiàn)方式為:
創(chuàng)建一個(gè)Component實(shí)現(xiàn)com.nghsmart.ar.context.RequestAttributeContext中的RequestInterceptor接口
重寫apply方法
通過RequestContextHolder對象獲取到RequestAttributes
通過RequestAttributes對象獲取到HttpServletRequest
通過HttpServletRequest對象獲取到請求頭
在請求頭中把token拿出來
將token塞進(jìn)restTemplate創(chuàng)建的http請求頭中
示例代碼:
BizFeignRequestInterceptor
import com.nghsmart.ar.context.RequestAttributeContext;
import com.nghsmart.common.core.utils.ServletUtils;
import com.nghsmart.common.core.utils.StringUtils;
import com.nghsmart.common.core.utils.ip.IpUtils;
import com.nghsmart.common.security.constant.FeignRequestHeader;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.AbstractRequestAttributes;
import org.springframework.web.context.request.FacesRequestAttributes;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;
@Slf4j
@Order(1)
@Component
public class BizFeignRequestInterceptor implements RequestInterceptor {
@Override
public void apply(RequestTemplate requestTemplate) {
RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
if (null! = attributes) {
ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) attributes;
String token = servletRequestAttributes.getRequest().getHeader("token");
requestTemplate.header("token",token);
}
}
}token異步線程傳遞
上述添加BizFeignRequestInterceptor只能解決同步調(diào)用環(huán)境下的token傳遞問題,當(dāng)是異步線程環(huán)境下就GG了。
通過在主線程中主動將RequestAttribute傳遞到子線程中可以解決一部分異步線程中token傳遞的問題,示例代碼如下:
RequestContextHolder.setRequestAttributes(RequestContextHolder.getRequestAttributes(), true);
但是這種方式有弊端,當(dāng)主線程先于子線程結(jié)束的時(shí)候,子線程將獲取不到RequestAttribute,原因是Tomcat會在http請求結(jié)束的時(shí)候清空數(shù)據(jù)。
我們可以創(chuàng)建一個(gè)InheritableThreadLocal用來保存RequestAttribute,這樣就可以完美解決問題了。
實(shí)現(xiàn)思路為:
創(chuàng)建一個(gè) RequestAttributeContext,其中維護(hù)一個(gè)InheritableThreadLocal對象,用來存RequestAttributes
創(chuàng)建一個(gè)RequestAttributeInterceptor,實(shí)現(xiàn)HandlerInterceptor, WebMvcConfigurer接口,用來在請求開始前把 RequestAttributes 存放到 RequestAttributeContext 中
修改 BizFeignRequestInterceptor ,當(dāng)無法獲取到 RequestAttributes 的時(shí)候,就從 RequestAttributeContext 中獲取
透傳邏輯不變
相關(guān)示例代碼如下:
RequestAttributeContext
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.context.request.RequestAttributes;
@Slf4j
public class RequestAttributeContext {
private static final ThreadLocal<RequestAttributes> context = new InheritableThreadLocal<>();
public static void setAttribute(RequestAttributes attributes) {
if (null == attributes) {
log.debug("RequestAttributes is null");
}
context.set(attributes);
}
public static RequestAttributes getAttribute() {
return context.get();
}
public static void removeAttribute() {
context.remove();
}
}RequestAttributeInterceptor
import com.alibaba.fastjson.JSON;
import com.nghsmart.ar.context.RequestAttributeContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@Slf4j
@Configuration
public class RequestAttributeInterceptor implements HandlerInterceptor, WebMvcConfigurer {
/**
* 重寫 WebMvcConfigurer 的 addInterceptors,將 RequestAttributeInterceptor 添加到攔截器列表
*
* @param registry
*/
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(this).addPathPatterns("/**").excludePathPatterns("/swagger-resources/**", "/v2/api-docs/**");
}
/**
* 重寫 HandlerInterceptor 的 preHandle,在請求開始處理前,將 RequestAttribute 存入 RequestAttributeContext
*
* @param request current HTTP request
* @param response current HTTP response
* @param handler chosen handler to execute, for type and/or instance evaluation
* @return
* @throws Exception
*/
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
RequestAttributeContext.setAttribute(requestAttributes);
return true;
}
}BizFeignRequestInterceptor
import com.nghsmart.ar.context.RequestAttributeContext;
import com.nghsmart.common.core.utils.ServletUtils;
import com.nghsmart.common.core.utils.StringUtils;
import com.nghsmart.common.core.utils.ip.IpUtils;
import com.nghsmart.common.security.constant.FeignRequestHeader;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.AbstractRequestAttributes;
import org.springframework.web.context.request.FacesRequestAttributes;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;
@Slf4j
@Order(1)
@Component
public class BizFeignRequestInterceptor implements RequestInterceptor {
@Override
public void apply(RequestTemplate requestTemplate) {
RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
if (null! = attributes) {
ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) attributes;
String token = servletRequestAttributes.getRequest().getHeader("token");
requestTemplate.header("token",token);
}else {
RequestAttributes requestAttributes = RequestAttributeContext.getAttribute();
if (null != requestAttributes) {
RequestContextHolder.setRequestAttributes(requestAttributes);
} else {
log.debug("requestAttributes is null");
}
ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) requestAttributes;
String token = servletRequestAttributes.getRequest().getHeader("token");
requestTemplate.header("token",token);
}
}
}到此這篇關(guān)于SpringCloud解決feign調(diào)用token丟失問題解決辦法的文章就介紹到這了,更多相關(guān)SpringCloud中feign調(diào)用token丟失內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java設(shè)計(jì)模式之適配器模式(Adapter模式)介紹
這篇文章主要介紹了Java設(shè)計(jì)模式之適配器模式(Adapter模式)介紹,本文講解了為何使用適配器模式、如何使用適配器模式等內(nèi)容,需要的朋友可以參考下2015-03-03
Java批量從svn導(dǎo)出多個(gè)項(xiàng)目代碼實(shí)例
這篇文章主要介紹了java批量從svn導(dǎo)出多個(gè)項(xiàng)目代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03
Java語法糖之個(gè)數(shù)可變的形參的實(shí)現(xiàn)
這篇文章主要介紹了Java語法糖之個(gè)數(shù)可變的形參的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06
詳解Java中的增強(qiáng) for 循環(huán) foreach
foreach 是 Java 中的一種語法糖,幾乎每一種語言都有一些這樣的語法糖來方便程序員進(jìn)行開發(fā),編譯期間以特定的字節(jié)碼或特定的方式來對這些語法進(jìn)行處理。能夠提高性能,并減少代碼出錯(cuò)的幾率。2017-05-05
SpringSecurity動態(tài)加載用戶角色權(quán)限實(shí)現(xiàn)登錄及鑒權(quán)功能
這篇文章主要介紹了SpringSecurity動態(tài)加載用戶角色權(quán)限實(shí)現(xiàn)登錄及鑒權(quán)功能,很多朋友感覺這個(gè)功能很難,今天小編通過實(shí)例代碼給大家講解,需要的朋友可以參考下2019-11-11

