首页 > 解决方案 > SpringDoc Oauth2 需要隐式流随机数参数并且身份验证失败

问题描述

我可以启用授权按钮,但是当我单击授权按钮时,对于 oauth2 隐式流程,它会抱怨缺少 nonce 参数。

The 'nonce' parameter is required for authorize requests with either the 'id_token' or 'token' response types

这是我用来创建隐式流的代码

new OpenAPI()
    .addSecurityItem(new SecurityRequirement().addList(securitySchemeName))
    .components(new Components()
        .addSecuritySchemes(securitySchemeName,
            new SecurityScheme()
                .name(securitySchemeName)
                .type(SecurityScheme.Type.OAUTH2)
                .description("This API uses OAuth 2 with the implicit grant flow.")
                .flows(new OAuthFlows()
                          .implicit(new OAuthFlow()
                              .authorizationUrl(authorizeUri)
                              .scopes(new Scopes()
                                .addString("read", "read")))

在 Springfox 中添加 nonce 参数,您为 SecurityConfiguration 创建一个 bean 并使用 additionalQueryStringsParams() 方法。

SecurityConfiguration#additionalQueryStringParams()

您将在其中添加 nonce 作为键和值对中的随机字符串。我尝试使用 OAuthFlows 和 SecurityScheme 上的扩展,但它似乎仍然无法正常工作。

我不确定我错过了什么,感谢任何帮助。

标签: springdoc

解决方案


好的,我想通了,没有添加额外查询参数的功能。您只需自己将其硬编码到字符串中,然后将其添加到 url。

.authorizaitonUrl(authroizeUri + "?nonce=\"asdf\"")

推荐阅读