【问题标题】:Get AccessToken from spring cloud zuul API Gateway从 spring cloud zuul API Gateway 获取 AccessToken
【发布时间】:2016-12-10 10:14:33
【问题描述】:

我们在 Spring Cloud 中使用 zuul 作为 API 网关。现在我们想从 zuul 中提取访问令牌以进一步实施。请提供我们希望如何实施的建议。谢谢

【问题讨论】:

  • 您好 Grinish,感谢您的回复,我们已经实现了身份验证,但我们需要访问令牌以在我们的应用程序中进行用户管理。我们想提取访问令牌并将其存储在 redis 会话中以供进一步实施。请提供建议。谢谢。
  • 如果我正确理解了您的问题,您一定有某种授权标头,您可以在 zuul 中读取该标头并将访问令牌从那里传递给下游服务。要阅读它并将其发送到下游,您需要使用 pre zuul 过滤器。
  • 嗨 Grinish,您能指导我如何从 zuul 中的授权标头中检索。

标签: wso2is spring-cloud microservices oauth2 netflix-zuul


【解决方案1】:

要阅读授权标头,您需要在 ZUUL 中创建一个过滤器,我的想法是您需要一个预过滤器,您可以根据需要更改它。这是您需要的。

public class TestFilter extends ZuulFilter {

@Override
public boolean shouldFilter() {

    return true;
}

@Override
public Object run() {

    final RequestContext ctx = RequestContext.getCurrentContext();
    final HttpServletRequest request = ctx.getRequest();
 //Here is the authorization header being read.
    final String xAuth = request.getHeader("Authorization");
 //Use the below method to add anything to the request header to read downstream. if needed.
    ctx.addZuulRequestHeader("abc", "abc"); 

    return null;
}

@Override
public String filterType() {

    return "pre";
}

@Override
public int filterOrder() {

    return 1;
}

}

您将需要在您拥有@EnableZuulProxy 的类中为Filter 声明@Bean

@Bean
public TestFilter testFilter() {
    return new TestFilter();
}

希望这会有所帮助。!!!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-04-15
    • 2020-12-13
    • 2020-07-19
    • 2020-06-01
    • 2018-05-26
    • 2017-07-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多