如何在spring-boot应用程序中使用API​​网关拦截请求

时间:2017-05-10 14:37:08

标签: java spring-boot interceptor

我想做的是:

  • 我通过运行在8765端口的API-GATEWAY呼叫每个微服务
  • 对于路由,我已经在API网关中添加了zuell路由(调用正常运行)
  • 我希望对通过API网关的每个呼叫进行API密钥检查。 即如果API密钥为空或不正确,则不会调用其他微服务。

以下是我的代码:

public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
        LOG.info("#### Starting TokenValidateInterceptor.preHandle ####");

        LOG.info("apikeyAttribute-->" + request.getAttribute("apiKey"); //coming as a blank
        LOG.info("apikeyHeader-->" + request.getHeader("apiKey"));//coming as a blank

        if (StringUtils.isBlank(apiKey)) {

            if (!request.getRequestURI().endsWith("/")) {

                try {
                    response.sendError(401, ("Unauthorized: Access is denied due to invalid credentials."));
                } catch (IOException e) {
                    LOG.error("##### IOException occured at TokenValidateInterceptor.preHandle() #####", e);
                } catch (Exception e) {
                    LOG.error("##### Generic Exception occured at TokenValidateInterceptor.preHandle() #####,e");
                }
                //This block is getting executed but still its calling the other microservice
                return false;
            } else {

                return true;
            }
        }

我在这里面临两个问题   -

  • 尽管我使用postman
  • 提供API密钥,但它仍为null
  • 正如我所说,我正在做这个验证检查。 API密钥仍然是空白,仍称为相应的微服务

请参阅附件图片, The way I am calling my gateway

0 个答案:

没有答案