久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

使用 Spring Rest 模板在服務(wù)上傳播 HTTP 標(biāo)頭(JWT 令

Propagate HTTP header (JWT Token) over services using spring rest template(使用 Spring Rest 模板在服務(wù)上傳播 HTTP 標(biāo)頭(JWT 令牌))
本文介紹了使用 Spring Rest 模板在服務(wù)上傳播 HTTP 標(biāo)頭(JWT 令牌)的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(wèn)題描述

我有一個(gè)微服務(wù)架構(gòu),它們都由 Spring Security 和 JWT 令牌保護(hù).

I have a microservice architecture, both of them securized by spring security an JWT tokens.

因此,當(dāng)我調(diào)用我的第一個(gè)微服務(wù)時(shí),我想獲取 JWT 令牌并使用這些憑據(jù)向另一個(gè)服務(wù)發(fā)送請(qǐng)求.

So, when I call my first microservice, I want to take the JWT token and send a request to another service using those credentials.

如何檢索令牌并再次發(fā)送到其他服務(wù)?

How can I retrieve the token and sent again to the other service?

推薦答案

我已經(jīng)完成了任務(wù),創(chuàng)建了一個(gè)自定義過(guò)濾器

I've accomplished the task, creating a custom Filter

public class RequestFilter implements Filter{



    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {

        HttpServletRequest httpServletRequest = (HttpServletRequest) request;
        String token = httpServletRequest.getHeader(RequestContext.REQUEST_HEADER_NAME);

        if (token == null || "".equals(token)) {
            throw new IllegalArgumentException("Can't retrieve JWT Token");
        }

        RequestContext.getContext().setToken(token);
        chain.doFilter(request, response);

    }

    @Override
    public void destroy() { }

    @Override
    public void init(FilterConfig arg0) throws ServletException {}


}

然后,在我的配置中設(shè)置

Then, setting in my config

    @Bean
public FilterRegistrationBean getPeticionFilter() {

    FilterRegistrationBean registration = new FilterRegistrationBean();
    registration.setFilter(new RequestFilter());
    registration.addUrlPatterns("/*");
    registration.setName("requestFilter");

    return registration;
}

考慮到這一點(diǎn),我創(chuàng)建了另一個(gè)具有 ThreadLocal 變量的類(lèi),以將 JWT 令牌從 Controller 傳遞到 Rest Templace 攔截器

With that in mind, I've create another class with a ThreadLocal variable to pass the JWT token from the Controller to the Rest Templace interceptor

public class RequestContext {

public static final String REQUEST_HEADER_NAME = "Authorization";

private static final ThreadLocal<RequestContext> CONTEXT = new ThreadLocal<>();

private String token;

public static RequestContext getContext() {
    RequestContext result = CONTEXT.get();

    if (result == null) {
        result = new RequestContext();
        CONTEXT.set(result);
    }

    return result;
}

public String getToken() {
    return token;
}

public void setToken(String token) {
    this.token = token;
}

}

public class RestTemplateInterceptor implements ClientHttpRequestInterceptor{

@Override
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {

    String token = RequestContext.getContext().getToken();

    request.getHeaders().add(RequestContext.REQUEST_HEADER_NAME, token);

    return execution.execute(request, body);

}

}

在配置中添加攔截器

  @PostConstruct
public void addInterceptors() {
    List<ClientHttpRequestInterceptor> interceptors = restTemplate.getInterceptors();
    interceptors.add(new RestTemplateInterceptor());
    restTemplate.setInterceptors(interceptors);
}

這篇關(guān)于使用 Spring Rest 模板在服務(wù)上傳播 HTTP 標(biāo)頭(JWT 令牌)的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

How to wrap text around components in a JTextPane?(如何在 JTextPane 中的組件周?chē)h(huán)繞文本?)
MyBatis, how to get the auto generated key of an insert? [MySql](MyBatis,如何獲取插入的自動(dòng)生成密鑰?[MySql])
Inserting to Oracle Nested Table in Java(在 Java 中插入 Oracle 嵌套表)
Java: How to insert CLOB into oracle database(Java:如何將 CLOB 插入 oracle 數(shù)據(jù)庫(kù))
Why does Spring-data-jdbc not save my Car object?(為什么 Spring-data-jdbc 不保存我的 Car 對(duì)象?)
Use threading to process file chunk by chunk(使用線(xiàn)程逐塊處理文件)
主站蜘蛛池模板: 欧美久久免费观看 | 天天视频一区二区三区 | 国产亚洲欧美另类一区二区三区 | 国产精品一区二区在线观看 | 日韩视频一区二区 | 精品视频久久久久久 | 成人免费网站 | 成人毛片网 | 久久久久无码国产精品一区 | 免费同性女女aaa免费网站 | 涩在线| 97精品国产97久久久久久免费 | 国产日韩欧美在线 | 91亚洲免费 | 日本亚洲一区 | 九九久久精品视频 | 成人影视网址 | 91精品在线播放 | 国产精品亚洲综合 | 久热精品视频 | 国产人成精品一区二区三 | 小视频你懂得 | 91大神xh98xh系列全部 | 精品视频一区二区三区 | 在线欧美激情 | 亚洲色图综合 | 蜜桃视频在线观看免费视频网站www | 精品国产一区二区三区日日嗨 | 国产日韩在线观看一区 | 亚洲日本一区二区三区四区 | 国产在线二区 | 一区二区三区四区国产精品 | 欧美在线视频网 | 国产精品国产a级 | 天堂男人av | 精品成人在线视频 | 91伦理片| 日韩欧美中文 | 麻豆久久久久久 | 九九精品在线 | 午夜伦理影院 |