如何修复springfox中的“无法解析全局安全范围定义”?

时间:2019-02-12 10:43:44

标签: spring spring-boot swagger springfox

我正在使用springfox从spring控制器生成swagger文档。造访http://127.0.0.1:8080/mycontextroot/swagger-ui.html时,我得到了一个醒目的UI,有效!

但是当我尝试打开通过yaml通过json生成的相同的http://127.0.0.1:8080/mycontextroot/v2/api-docs(或https://editor.swagger.io/)文件时,我得到了错误提示:

enter image description here

昂首阔步:

---
swagger: '2.0'
info:
    description: Api Documentation
    version: '1.0'
    title: Api Documentation
    termsOfService: urn:tos
    contact: {}
    license:
        name: Apache 2.0
        url: http://www.apache.org/licenses/LICENSE-2.0
host: 127.0.0.1:8080
basePath: "/"
paths:
    "/mycontextroot/blogs":
        get:
            summary: blogs
            operationId: blogsUsingGET
            produces:
                - "*/*"
            responses:
                '200':
                    description: OK
                    schema:
                        "$ref": "#/definitions/Blogs"
                '401':
                    description: Unauthorized
                '403':
                    description: Forbidden
                '404':
                    description: Not Found
            security:
                - xauth:
                      - global
            deprecated: false
securityDefinitions:
    xauth:
        type: apiKey
        name: my-auth-header
        in: header
definitions:
    Blog:
        type: object
        properties:
            title:
                type: string
        title: Blog
    Blogs:
        type: object
        properties:
            blogs:
                type: array
                items:
                    "$ref": "#/definitions/Blog"
        title: Blogs

2 个答案:

答案 0 :(得分:4)

我有同样的问题。无效是由以下原因引起的:

(defun char-white-p (c)
  ;; Is a character white?  The fallback for this is horrid, since
  ;; tab &c are not a standard characters.  There must be a portability
  ;; library with a function which does this.
  #+LispWorks (lw:whitespace-char-p c)
  #+CCL (ccl:whitespacep c)             ;?
  #-(or LispWorks CCL)
  (member char (load-time-value
                (mapcan (lambda (n)
                          (let ((c (name-char n)))
                            (and c (list c))))
                        '("Space" "Newline" "Page" "Tab" "Return" "Linefeed"
                          ;; and I am not sure about the following, but, well
                          "Backspace" "Rubout")))))

(defun read-and-parse (filename &key (as-strings nil))
  "Parse a file into a list of words, splitting on whitespace.

By default the words are returned as lists of characters.  If
AS-STRINGS is T then they are coerced to strings"
  (with-open-file (s filename)
    (loop for maybe-word = (loop with collecting = nil
                                 for c = (read-char s nil)
                                 ;; carry on until we hit EOF, or we
                                 ;; hit whitespace while collecting a
                                 ;; word
                                 until (or (not c) ;EOF
                                           (and collecting (char-white-p c)))
                                 ;; if we're not collecting and we see
                                 ;; a non-white character, then we're
                                 ;; now collecting
                                 when (and (not collecting) (not (char-white-p c)))
                                 do (setf collecting t)
                                 when collecting
                                 collect c)
          while (not (null maybe-word))
          collect (if as-strings
                      (coerce maybe-word 'string)
                    maybe-word))))

必须是:

security:
    - xauth:
        - global

如果您是用Java生成摇摇欲坠的,请申请:

security:
    - xauth: []

答案 1 :(得分:0)

来自@ igor-lopatka的答案是正确的,但让我们尝试详细说明:

  1. 没有空的AuthorizationScope列表只能在OAuth的情况下使用 scopes身份验证是oAuth世界
  2. 的实体
  3. 对于其他身份验证架构,它应该为空

请参见示例,我的应用程序支持两种模式BasicAuthOAuth

    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                // skip irrelevant configuration
                .securitySchemes(Arrays.asList(basicAuth(), oAuth2()))
                .securityContexts(singletonList(securityContext()))
                // skip irrelevant configuration
                .build()
    }

    private BasicAuth basicAuth() {
        return new BasicAuth("basicAuth");
    }

    private OAuth oAuth2() {
        return new OAuth("oAuth2", Arrays.asList(oAuth2AuthorizationScopes()), singletonList(new ResourceOwnerPasswordCredentialsGrant("https://example.com/oauth/token"))));
    }

    private AuthorizationScope[] oAuth2AuthorizationScopes() {
        return new AuthorizationScope[]{
               new AuthorizationScope("read", "read access"),
               new AuthorizationScope("write", "write access")
        };
    }

    private SecurityContext securityContext() {
        return SecurityContext.builder()
                .securityReferences(defaultAuth())
                .forPaths(regex("/api/.*"))
                .build();
    }

    private List<SecurityReference> defaultAuth() {
        return Arrays.asList(
                new SecurityReference("basicAuth", new AuthorizationScope[]{}),
                new SecurityReference("oAuth2", oAuth2AuthorizationScopes())
        );
    }