首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >springdoc-openapi-UIPKCE2.0授权代码流

springdoc-openapi-UIPKCE2.0授权代码流
EN

Stack Overflow用户
提问于 2020-07-17 17:05:17
回答 2查看 6K关注 0票数 1

我正在使用springdoc-openapi-ui-1.4.3的swagger

代码语言:javascript
复制
@SecurityRequirement(name = "security_auth")
public class ProductController {}

设置安全架构

代码语言:javascript
复制
@SecurityScheme(name = "security_auth", type = SecuritySchemeType.OAUTH2,
        flows = @OAuthFlows(authorizationCode = @OAuthFlow(
                authorizationUrl = "${springdoc.oAuthFlow.authorizationUrl}"
                , tokenUrl = "${springdoc.oAuthFlow.tokenUrl}",scopes = {
                @OAuthScope(name = "IdentityPortal.API", description = "IdentityPortal.API")})))
public class OpenApiConfig {}

安全配置

代码语言:javascript
复制
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {// @formatter:off
        http
                .authorizeRequests()
                .antMatchers("/v3/api-docs/**", "/swagger-ui/**", "/swagger-ui.html")
                .permitAll()
                .antMatchers(HttpMethod.GET, "/user/info", "/api/foos/**")
                .hasAuthority("SCOPE_read")
                .antMatchers(HttpMethod.POST, "/api/foos")
                .hasAuthority("SCOPE_write")
                .anyRequest()
                .authenticated()
                .and()
                .oauth2ResourceServer()
                .jwt();
    }
}

使用依赖项

代码语言:javascript
复制
implementation 'org.springframework.boot:spring-boot-starter-oauth2-resource-server'
implementation 'org.springdoc:springdoc-openapi-ui:1.4.3'
implementation 'org.springdoc:springdoc-openapi-security:1.4.3'
implementation "org.springframework.boot:spring-boot-starter-security"

配置设置

代码语言:javascript
复制
spring:
  profiles:
    active: dev

####### resource server configuration properties
  security:
    oauth2:
      resourceserver:
        jwt:
          issuer-uri: https://localhost:5001
          jwk-set-uri: https://localhost:5001/connect/token
springdoc:
  swagger-ui:
    oauth:
      clientId: Local
      usepkcewithauthorizationcodegrant: true
  oAuthFlow:
    authorizationUrl: https://localhost:5001
    tokenUrl: https://localhost:5001/connect/token

在swagger UI中,clientId为空,客户端密钥存在,因为授权码+ PKCE流客户端密钥不应存在。

EN

回答 2

Stack Overflow用户

发布于 2020-08-02 21:30:43

您的属性语法

使用带授权码授权的kce.

不正确:

以下是PKCE的正确属性:

代码语言:javascript
复制
springdoc.swagger-ui.oauth.use-pkce-with-authorization-code-grant=true

要填充客户端id,只需使用:

代码语言:javascript
复制
springdoc.swagger-ui.oauth.client-id=yourSPAClientId

你对现有机密文件的评论是可以隐藏的。这看起来像是swagger-ui的增强。

您应该提交关于swagger-ui项目的增强:

票数 3
EN

Stack Overflow用户

发布于 2021-05-28 16:10:59

你问这个问题已经有一段时间了,但我会回答其他人的信息。主要的问题是UI的误导性实现。您必须在配置中使用授权码流,因为缺少带有PKCE的授权码。因此,您必须使用授权码(因为您需要提供授权和令牌url),并在yaml中放置一个虚拟秘密。下面是一个例子。

代码语言:javascript
复制
@SecurityScheme(name = "security_auth", type = SecuritySchemeType.OAUTH2,
        flows = @OAuthFlows(authorizationCode = @OAuthFlow(
                authorizationUrl = "${springdoc.oAuthFlow.authorizationUrl}"
                , tokenUrl = "${springdoc.oAuthFlow.tokenUrl}")))
public class OpenApiConfig {}

如果你想使用PKCE而不是纯隐式的设置正确的属性(如@brianbro所指出的)和一个虚拟秘密,如下所示:

代码语言:javascript
复制
springdoc.swagger-ui.oauth.use-pkce-with-authorization-code-grant=true
springdoc.swagger-ui.oauth.clent-secret=justFillerBecausePKCEisUsed

最重要的是,如果您想预先填充client_id use配置:

代码语言:javascript
复制
springdoc.swagger-ui.oauth.client-id=YourClientId
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62950730

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档