java - springdoc-openapi-ui OAuth 2.0 授权代码流与 PKCE
问题描述
我正在使用带有 springdoc-openapi-ui-1.4.3 的招摇
@SecurityRequirement(name = "security_auth")
public class ProductController {}
设置安全架构
@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 {}
安全配置
@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();
}
}
有依赖关系
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"
配置设置
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 流客户端密码不应该存在
解决方案
您的属性语法
使用 pkcewithauthorizationcodegrant
是不正确的:
这是 PKCE 的正确属性:
springdoc.swagger-ui.oauth.use-pkce-with-authorization-code-grant=true
要填写客户 ID,只需使用:
springdoc.swagger-ui.oauth.client-id=yourSPAClientId
对于您对可以隐藏的现有秘密文件的评论。这看起来像是对 swagger-ui 的增强。
您应该提交对 swagger-ui 项目的增强:
推荐阅读
- r - 如何在 R 中制作具有有限起始值的 nls 公式
- html - 如何防止项目高度扩展扩展同一行中的其他项目 - CSS grid/flex?
- c++ - 这个 c++ 语法是什么意思?`内联常量颜色 (&EnumValuesColor())[3] {...}`
- excel - 基于影响整个表 VBA 的一列对数据进行排序
- python - Pandas:基于多个条件的条件自连接
- python - ModuleNotFoundError:Visual Studio 代码中没有名为“cx_Oracle”的模块
- python - 如何注册一个 64 位 python COM 服务器
- python - 实施中的 Keras 在线学习问题
- php - SSH 脚本手动运行时运行良好,但在远程使用 PHP 执行时无法正常运行
- java - 双向链表在开头插入节点