swagger-ui不显示问题定位

1. 现象1

正常情况是 group会显示default/v2/api-docs

不知道是什么原因, 一个app可以展示,但另一个app不展示,配置也基本相同

1.1 定位过程

正常的app访问时的结果:

异常app访问的结果:

原因:

因为自定义了application/json响应结果的统一封装,导致swagger的响应被修改了

页面显示时会调用3个ajax请求, 结果必须满足swagger自己的格式

1.2 修改方案:

结果封装部分要跳过swagger的请求



2.现象2

no mapping for swagger-ui.html

2.1解决方案:

注意spring mvc的配置, spring.mvc.static-path-pattern 使用默认值

升级spingdoc + oauth2的认证流程

    public OpenAPI apiInfo() {
        return new OpenAPI()
                .info(new Info().title("XXX API Service")
                        .description(SOME_SWAGGER_DESCRIPTION)
                        .version(appVersion)
                        .license(new License().name("Apache 2.0").url("http://springdoc.org"))
                )
                // 手动输入token
//                .components(new Components()
//                        .addSecuritySchemes(SECURITY_SCHEME_NAME,
//                                new SecurityScheme()
//                                        .type(SecurityScheme.Type.HTTP)
//                                        .scheme("bearer")
//                                        .bearerFormat("JWT")))
                // 自动获取token
                .components(new Components()
                        .addSecuritySchemes(SECURITY_SCHEME_NAME,
                                new SecurityScheme()
                                        .type(SecurityScheme.Type.OAUTH2)
                                        .description("OAuth2 flow")
                                        .flows(new OAuthFlows()
                                                // 1. name + password
                                                .password(new OAuthFlow()
                                                        .tokenUrl(tokenInfoUrl)
                                                        .scopes(new Scopes().addString("Identity:SCIM", "")))
                                                // 2. BOT授权码模式
                                                .authorizationCode(new OAuthFlow()
                                                        .authorizationUrl(authorizationUrl)
                                                        .tokenUrl(tokenInfoUrl)
                                                        .scopes(new Scopes().addString("Identity:SCIM", "")))
                                                // 3. implicit
                                                .implicit(new OAuthFlow()
                                                        // 只需要授权地址,不需要 tokenUrl
                                                        .authorizationUrl(authorizationUrl)
                                                        .scopes(new Scopes()
                                                                .addString("Identity:SCIM", "required")))
                                        )))
                .addSecurityItem(new SecurityRequirement().addList(SECURITY_SCHEME_NAME))
                ;
    }

implicit模式不需要tokenUrl,也不需要在swagger-ui里输入密码,跳转Tab后用户自己处理。
redirectUrl自动生成,本地开发是 http://localhost:8080/swagger-ui/oauth2-redirect.html
提前在认证服务器(如 Keycloak、Auth0)中手动注册本地地址
image

posted @ 2019-09-30 13:12  funny_coding  阅读(5775)  评论(0)    收藏  举报
build beautiful things, share happiness