【发布时间】:2017-10-26 06:52:38
【问题描述】:
这是我想要做的:
- 我有 API-GATEWAY(位于 spring-boot 中),我想在其中使用拦截器来验证请求。如果请求具有正确的凭据,它将调用其他服务(在 Node.js 中)
- API-GATEWAY 正在
8765上运行。我只使用这个8765调用其他服务 - 有 4 个 Node.js 服务,我希望每个服务调用都需要在 API-GATEWAY 的拦截器中进行身份验证,这是我使用
registry.addInterceptor(tokenValidateInterceptor()).addPathPatterns("/**");的原因 - spring-boot 和 node.js 服务都托管在 docker 容器中。
- 我在 API-GATEWAY 中使用 zuul 路由 来调用其他 node.js 服务。
- 调用其他服务的所有配置都可以正常工作,因为我可以通过 API-GATEWAY 访问其他服务。
对我来说,showstopper 是 API-GATEWAY 中的拦截器。我在这里观察到一个奇怪的场景,我想提一下
如果 Node.js 服务停止,拦截器工作正常。但如果 Node.js 服务正在运行,它甚至不会执行拦截器,但调用是通过 API-GATEWAY 和我正在从 Node.js 服务获得所需的响应。
这是我的代码片段:
@EnableEurekaClient
@SpringBootApplication
@EnableZuulProxy
@Configuration
public class Application extends WebMvcConfigurerAdapter {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Autowired
private TokenValidateInterceptor tokenValidateInterceptor;
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(tokenValidateInterceptor).addPathPatterns("/**");
}
拦截器
@Component
public class TokenValidateInterceptor extends HandlerInterceptorAdapter {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
LOG.info("#### Starting TokenValidateInterceptor.preHandle ####");
String apiKey = null;
try {
apiKey = request.getHeader("apikey");
LOG.info("The request come with apikey ======" + apiKey);
LOG.info("Actual apikey ======" + azureApikey);
}
建议更改或告诉我我是否执行错误。
【问题讨论】:
-
为什么不使用 Zuul 过滤器来拦截和验证请求。? github.com/Netflix/zuul/wiki/Writing-Filters
-
API-GATEWAY 在我的项目中执行更多操作,例如
AD-AUTHENTICTION等。所以我必须仅在 API-GATEWAY 中执行此操作 -
@PraneethRamesh :是否有任何指南可用于配置它以捕获一个标头并对其执行操作。
-
只需创建一个zuul Pre过滤器,您可以在其中获取HttpServletRequest的引用,该引用可用于获取您想要的标头。在过滤器内部执行认证操作。在它对我有用之前我已经这样做了。
标签: java node.js spring docker spring-boot