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

使用 Spring Rest 模板在服務上傳播 HTTP 標頭(JWT 令

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

問題描述

我有一個微服務架構,它們都由 Spring Security 和 JWT 令牌保護.

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

因此,當我調用我的第一個微服務時,我想獲取 JWT 令牌并使用這些憑據向另一個服務發送請求.

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

如何檢索令牌并再次發送到其他服務?

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

推薦答案

我已經完成了任務,創建了一個自定義過濾器

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 {}


}

然后,在我的配置中設置

Then, setting in my config

    @Bean
public FilterRegistrationBean getPeticionFilter() {

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

    return registration;
}

考慮到這一點,我創建了另一個具有 ThreadLocal 變量的類,以將 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);
}

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

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

相關文檔推薦

How to wrap text around components in a JTextPane?(如何在 JTextPane 中的組件周圍環繞文本?)
MyBatis, how to get the auto generated key of an insert? [MySql](MyBatis,如何獲取插入的自動生成密鑰?[MySql])
Inserting to Oracle Nested Table in Java(在 Java 中插入 Oracle 嵌套表)
Java: How to insert CLOB into oracle database(Java:如何將 CLOB 插入 oracle 數據庫)
Why does Spring-data-jdbc not save my Car object?(為什么 Spring-data-jdbc 不保存我的 Car 對象?)
Use threading to process file chunk by chunk(使用線程逐塊處理文件)
主站蜘蛛池模板: 亚洲天堂视频在线观看 | 国产精品一二三四区 | 91国产视频在线观看 | 国产理论在线观看 | 亚洲国产毛片 | 国产传媒在线 | 亚洲精品中文字幕乱码三区91 | 天堂中文在线视频 | 91亚洲国产成人精品性色 | 一级黄色片免费观看 | 日韩精品福利 | 一区二区三区国产精品 | 91在线视频免费观看 | 国产在线免费 | 日日干夜夜操 | 色吧五月天 | 日韩成人综合 | 免费视频久久久 | 四虎8848精品成人免费网站 | 黄色一级片视频 | 亚洲特级片 | 久久久久久亚洲精品 | 国产日韩欧美综合 | 国产专区在线播放 | 久久国产免费 | 看片地址 | 欧美福利在线 | 91激情网| 免费看片黄色 | 亚洲一区国产 | www.一区| 色综合婷婷 | 亚洲精品一二 | 国产视频一区二区在线观看 | 国产一区二区精品在线 | heyzo在线观看 | 亚洲精品国产精品国自产在线 | 懂色av一区二区夜夜嗨 | 国产精品成人一区二区三区 | 亚洲黄色av | 成人精品免费视频 |