【问题标题】:Setting up Swagger UI with Spring WebFlux使用 Spring WebFlux 设置 Swagger UI
【发布时间】:2022-12-15 15:30:32
【问题描述】:

我目前正在为我正在从事的项目之一设置 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();
    }

【问题讨论】:

    标签: spring spring-boot spring-webflux swagger-ui springdoc-openapi-ui


    【解决方案1】:

    我们通过将此添加到每个application.yaml

    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);
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2020-04-13
      • 1970-01-01
      • 1970-01-01
      • 2019-04-24
      • 2018-06-27
      • 1970-01-01
      • 2018-06-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多