我们在spring cloud中使用zuul作为API网关。现在我们想从zuul中提取访问令牌以进一步实现。请提供我们想要实现的建议。谢谢
答案 0 :(得分: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;
}
}
您需要在@Bean
@EnableZuulProxy
声明
@Bean
public TestFilter testFilter() {
return new TestFilter();
}
希望这会有所帮助。!!!