使用Spring WebFlux设置Swagger UI



我目前正在为我正在进行的一个项目设置Swagger UI界面,我遇到了各种问题。

我的项目使用Spring安全性来使用承载令牌身份验证来保护API调用,因此我需要提供一种启用输入对话框的方法,以便用户可以输入他们的承载令牌。我已经尝试了OpenAPI文档中提到的关于这一点的一切,但似乎没有什么能正确地呈现对话框。

其次,该项目进行CSRF检查,即使我的应用程序属性包括springdoc.swagger-ui.csrf.enabled=true,检查也总是失败。我有一条死胡同,我不知道如何解决这两个问题。作为参考,我的安全配置如下:

@Bean
public SecurityWebFilterChain securityFilterChain(ServerHttpSecurity security) {
if (securityProperties.isEnabled()) {
return security
.securityMatcher(new NegatedServerWebExchangeMatcher(ServerWebExchangeMatchers.pathMatchers(securityProperties.getIgnoredPaths())))
.exceptionHandling()
.accessDeniedHandler(accessDeniedHandler)
.authenticationEntryPoint(entryPoint)
.and()
.cors()
.and()
.authorizeExchange(spec -> spec.anyExchange().authenticated())
.oauth2ResourceServer(ServerHttpSecurity.OAuth2ResourceServerSpec::jwt)
.build();
}
return security
.securityMatcher(new PathPatternParserServerWebExchangeMatcher("/**"))
.authorizeExchange(spec -> spec.anyExchange().permitAll())
.csrf()
.disable()
.build();
}

我们通过将其添加到每个application.yaml:来修复我们的多提供商(API的OAuth2 Keycapture和Swagger UI的基本Auth(Webflux安全配置

springdoc:
api-docs:
enabled: true
swagger-ui:
oauth:
client-id: dev
client-secret: 123
scopes: [openid]
csrf:
enabled: false

这里的重点是csrf.enabled: false

我们的钥匙斗篷安全配置:

// Keycloak-based JWT authorization for @RestControllers
@Order(1)
@EnableWebFluxSecurity
@EnableReactiveMethodSecurity
public class JwtSecurityConfig {
@Bean("jwt")
public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
http.authorizeExchange()
.pathMatchers("/api/**")
.authenticated()
.and()
.csrf()
.disable()
.oauth2ResourceServer()
.jwt()
.jwtAuthenticationConverter(grantedAuthoritiesExtractor());
return http.build();
}
private Converter<Jwt, ? extends Mono<? extends AbstractAuthenticationToken>>
grantedAuthoritiesExtractor() {
JwtAuthenticationConverter jwtAuthenticationConverter = new JwtAuthenticationConverter();
jwtAuthenticationConverter.setJwtGrantedAuthoritiesConverter(new GrantedAuthoritiesExtractor());
return new ReactiveJwtAuthenticationConverterAdapter(jwtAuthenticationConverter);
}
}

最新更新