当前位置: 首页 > 面试题库 >

使用Spring Rest模板在服务上传播HTTP标头(JWT令牌)

郎成龙
2023-03-14
问题内容

我有一个微服务架构,它们都由Spring Security和JWT令牌保护。

因此,当我调用第一个微服务时,我想获取JWT令牌并使用这些凭据将请求发送到另一个服务。

如何获取令牌并再次发送给其他服务?


问题答案:

我已经完成了任务,创建了一个自定义过滤器

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


}

然后,在我的配置中进行设置

    @Bean
public FilterRegistrationBean getPeticionFilter() {

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

    return registration;
}

考虑到这一点,我创建了另一个带有ThreadLocal变量的类,以将JWT令牌从Controller传递到Rest Templace拦截器

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);
}


 类似资料:
  • 是否可以从应用程序中创建JWT令牌,并使用GCP服务帐户在应用程序中验证它?我如何在Python中做到这一点? 这两个应用程序都部署在GCP中。应用程序可以部署在GCF、Cloud Run、AppEngine、GKE甚至GCE中。 我花了一些时间阅读Google Cloud文档,但我没有找到如何处理服务间身份验证的“通用”答案。(很可能是因为它们对每个GCP产品都不相同) 那么,考虑到我们抛开了谷

  • 我尝试用angular2前端实现jwt令牌。当我尝试使用Postman接收带有post方法的令牌时,我接收到授权令牌,但在Angular中这样做返回空响应对象。这里是我使用的Angular服务的代码片段。 问题是,当我尝试记录时,令牌是空的,与响应相同。对于代码的后端部分,我遵循了jwt令牌的这个实现。

  • 我在springboot中用jwt令牌开发了一个oauth2服务器,我在注销时遇到了困难http://www.baeldung.com/spring-security-oauth-revoke-tokens 注销后,如果在头中提供令牌并点击/user,则它将提供所有用户信息,而应该抛出并错误地表示用户已注销

  • 可以使用Spring Security制作一个非常简单的JWT示例。 我想说明如何使用角色,而不必设置所有的Spring Security性。 基本上我想使用带有包含角色的jwt令牌的postman。我不想设置用户登录。可能吗?

  • 我在Tomcat服务器上部署了一个SpringBoot应用程序,它使用Spring接收请求并向其他服务发出其他HTTP REST请求。 在传入的请求中,有一个< code>CORRELATION_ID HTTP头,用于跟踪请求。我希望对其他服务的任何请求也有这个头,这样我就可以关联不同服务器上的日志。 我如何在不更改现有代码的情况下实现这一点? 是否有任何 ,我可以在引导时的某个位置设置,以便它拦

  • 经过一些研究,我发现了一个用于多部分文件上传的开放库。在我的情况下,我想上传一个图像使用PUT请求,其中的图像要么是从画廊或相机选择。以下是我正在使用的资源:1。https://github.com/gotev/android-upload-service2.https://www.simplifiedcoding.net/android-upload-image-to-server/#comme