![](/img/trans.png)
[英]Generate webservice description with Swagger in Spring Webflux
[英]Setting up Swagger UI with Spring WebFlux
我目前正在為我正在從事的項目之一設置 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 Keycloak 和 Swagger UI 的基本身份驗證)Webflux 安全配置修復它:
springdoc:
api-docs:
enabled: true
swagger-ui:
oauth:
client-id: dev
client-secret: 123
scopes: [openid]
csrf:
enabled: false
這里的關鍵點是csrf.enabled: false
。
我們的 Keycloak 安全配置:
// 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);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.