Feign統(tǒng)一設(shè)置header

調(diào)用遠(yuǎn)程服務(wù)時,服務(wù)提供方要求在header中傳遞權(quán)限驗證信息或者為方便定位問題,在header中透傳一個traceId實現(xiàn)調(diào)用鏈路的跟蹤。利用Feign Client,可以非常方便地統(tǒng)一設(shè)置

原理

Feign可以通過實現(xiàn)接口feign.RequestInterceptor,完成對feign.RequestTemplate的修改,比如添加header

自定義FeignInterceptor

  • 增加權(quán)限校驗信息
  • 增加requestId,方便服務(wù)方完成冪等處理
import com.google.common.base.Strings;
import com.tenmao.tenmao.starter.constants.TenmaoConstant;
import com.tenmao.tenmao.starter.mvc.config.AuthProperties;
import com.tenmao.tenmao.starter.util.Md5Util;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.MDC;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.UUID;

/**
 * @author tenmao
 * @since 2019/9/21
 */
@Slf4j
@Component
@ConditionalOnClass(RequestInterceptor.class)
@SuppressWarnings("unused")
public class FeignInterceptor implements RequestInterceptor {
    @Resource
    private AuthProperties authProperties;

    @Override
    public void apply(RequestTemplate requestTemplate) {
        String traceId = MDC.get(TenmaoConstant.TRACE_KEY);
        log.info("set traceId: url[{}], traceId[{}]", requestTemplate.url(), traceId);
        requestTemplate.header(TenmaoConstant.TRACE_KEY, traceId);

        if (!Strings.isNullOrEmpty(authProperties.getAppId()) && !Strings.isNullOrEmpty(authProperties.getAppKey())) {
            long timestamp = System.currentTimeMillis();
            String md5 = Md5Util.calcMD5(authProperties.getAppId() + authProperties.getAppKey() + timestamp);

            //對服務(wù)調(diào)用進(jìn)行簽名,當(dāng)前簽名方法比較簡單,以后可以支持更加復(fù)雜的簽名計算(比如讀取參數(shù)內(nèi)容,組合后再進(jìn)行簽名計算)
            requestTemplate.header("appId", authProperties.getAppId());
            requestTemplate.header("timestamp", Long.toString(timestamp));
            requestTemplate.header("sign", md5);

            //一些接口的調(diào)用需要實現(xiàn)冪等,比如消息發(fā)送,如果使用requestId就可以方便服務(wù)方實現(xiàn)冪等
            requestTemplate.header("requestId", UUID.randomUUID().toString().replaceAll("-", ""));
        }
    }
}

權(quán)限配置信息

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
 * @author tenmao
 * @since 2019/10/9
 */
@Data
@Slf4j
@Component
@ConfigurationProperties(prefix = "common.auth")
public class AuthProperties {
    /**
     * 應(yīng)用ID.
     * 由服務(wù)方統(tǒng)一分配,區(qū)分各個調(diào)用方.
     */
    private String appId;

    /**
     * 應(yīng)用的KEY.
     * 由服務(wù)方統(tǒng)一分配,不可泄露給第三方.
     */
    private String appKey;

    @PostConstruct
    private void init() {
        log.info("AuthProperties Info: {}", this);
    }
}

配置文件(application.yml)

common:
  auth:
    app-id: hello
    app-key: world

參考

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容