程序员刘杨

———没有写不出的程序,只有不努力的程序员。

2019年11月24日   码农之路   1,264 次浏览

祝大家新年快乐,有任何问题可与我联系:点击这里给我发消息

今天把zuul部署好了,在使用代理后的swagger测试接口时,发现提交时项目路径出现重复,比如原来的接口地址为http://{ip}/{project_name}/api/user/add,然后现在变成了http://{ip}/{project_name}/{project_name}/api/user/add,找了N久的资料都没有解决(估计没人像我这样用)。

然后通过跟踪swagger源码找到了解决方法,下面来看看swagger源码(我的版本为springfox-swagger2-2.8.0):

1、首先我们知道swagger-ui是通过/api-docs这个接口来拿到数据的,里面就有个basePath参数,对应的是springfox.documentation.swagger2.web.Swagger2Controller类的getDocumentation方法,注意看倒数第4、5行。

  public ResponseEntity<Json> getDocumentation(
      @RequestParam(value = "group", required = false) String swaggerGroup,
      HttpServletRequest servletRequest) {

    String groupName = Optional.fromNullable(swaggerGroup).or(Docket.DEFAULT_GROUP_NAME);
    Documentation documentation = documentationCache.documentationByGroup(groupName);
    if (documentation == null) {
      return new ResponseEntity<Json>(HttpStatus.NOT_FOUND);
    }
    Swagger swagger = mapper.mapDocumentation(documentation);
    UriComponents uriComponents = componentsFrom(servletRequest, swagger.getBasePath());
    swagger.basePath(Strings.isNullOrEmpty(uriComponents.getPath()) ? "/" : uriComponents.getPath());
    if (isNullOrEmpty(swagger.getHost())) {
      swagger.host(hostName(uriComponents));
    }
    return new ResponseEntity<Json>(jsonSerializer.toJson(swagger), HttpStatus.OK);
  }

2、继续跟踪componentsFrom方法(注意标记行):

public class HostNameProvider {

  public HostNameProvider() {
    throw new UnsupportedOperationException();
  }

  static UriComponents componentsFrom(
      HttpServletRequest request,
      String basePath) {

    ServletUriComponentsBuilder builder = fromServletMapping(request, basePath);

    UriComponents components = UriComponentsBuilder.fromHttpRequest(
        new ServletServerHttpRequest(request))
        .build();

    String host = components.getHost();
    if (!hasText(host)) {
      return builder.build();
    }

    builder.host(host);
    builder.port(components.getPort());

    return builder.build();
  }

  private static ServletUriComponentsBuilder fromServletMapping(
      HttpServletRequest request,
      String basePath) {

    ServletUriComponentsBuilder builder = fromContextPath(request);

    builder.replacePath(prependForwardedPrefix(request, basePath));
    if (hasText(new UrlPathHelper().getPathWithinServletMapping(request))) {
      builder.path(request.getServletPath());
    }

    return builder;
  }

  private static String prependForwardedPrefix(
      HttpServletRequest request,
      String path) {

    String prefix = request.getHeader("X-Forwarded-Prefix");
    if (prefix != null) {
      return prefix + path;
    } else {
      return path;
    }
  }
}

3、最终跟踪到了prependForwardedPrefix方法,这里X-Forwarded-Prefix引起了我的注意,然后网上找到这个参数的用法是解决swagger在经过zuul转发后服务取不到请求路径的问题。这里我猜想是因为老版本zuul有这个问题,而swagger为了解决这个问题进行了处理,但是新版本的zuul解决了这个问题,而我用的swagger却还是老的版本,这样就重复处理了,于是设置下zuul不转发X-Forwarded-Prefix:

zuul:
  add-host-header: true
  sensitive-headers: "X-Forwarded-Prefix"
  ignoredHeaders: "X-Forwarded-Prefix"

4、问题解决,等有时间试下升级下swagger看是不是新版本的已经解决了这个问题。

赞 赏
申明:除非注明,本站文章均为原创,转载请以链接形式标明本文地址。 如有问题,请于一周内与本站联系,本站将在第一时间对相关内容进行处理。
本文地址: http://www.yyjjssnn.cn/articles/858.html

>>> Hello World <<<

这篇内容是否帮助到你了呢?

如果你有任何疑问或有建议留给其他朋友,都可以给我留言。

:wink: :twisted: :surprised: :smile: :smile9: :smile8: :smile7: :smile6: :smile5: :smile56: :smile55: :smile54: :smile53: :smile52: :smile51: :smile50: :smile4: :smile49: :smile48: :smile47: :smile46: :smile45: :smile44: :smile43: :smile42: :smile41: :smile40: :smile3: :smile39: :smile38: :smile37: :smile36: :smile35: :smile34: :smile33: :smile32: :smile31: :smile30: :smile2: :smile29: :smile28: :smile27: :smile26: :smile25: :smile24: :smile23: :smile22: :smile21: :smile20: :smile1: :smile19: :smile18: :smile17: :smile16: :smile15: :smile14: :smile13: :smile12: :smile11: :smile10: :smile0: :sad: :rolleyes1: :redface: :razz: :question: :neutral: :mrgreen: :mad: :lol: :idea: :exclaim: :evil: :eek: :cry: :cool: :confused: :biggrin: :arrow:

友情链接: 程序员刘杨 刘杨
Copyright 2003~2018 保留所有权利 | 网站地图
备案号:湘ICP备14001005号-2

湘公网安备 43011102001322号