限制对 Swagger 的访问 UI

Restrict access to Swagger UI

我很自信 UI 使用 spring-boot。我的 spring rest api 有一个无状态身份验证设置,它根据每个 api 路径的角色进行限制。

但是,我不确定如何将 <server_url>/swagger-ui.html 置于基本身份验证之后。

更新

我通过 WebSecurityConfig

配置了以下网络安全
@Override
protected void configure(HttpSecurity httpSecurity) throws Exception {
    httpSecurity
            .csrf().disable()
            .exceptionHandling().authenticationEntryPoint(unauthorizedHandler).and()
            .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and()
            .authorizeRequests()
            .antMatchers("/sysadmin/**").hasRole("SYSADMIN")
            .antMatchers("/admin/**").hasRole("ADMIN")
            .antMatchers("/siteadmin/**").hasRole("SITEADMIN")
            .antMatchers("/api/**").hasRole("USER")
            .anyRequest().permitAll();

    // Custom JWT based security filter
    httpSecurity
            .addFilterBefore(authenticationTokenFilterBean(), UsernamePasswordAuthenticationFilter.class);

}

在不了解您的配置的情况下的一个建议来自这个 SO 问题。

根据您更新的问题详情,这里是您可以添加内容的示例:

@Override
protected void configure(HttpSecurity httpSecurity) throws Exception {
    httpSecurity
            .csrf().disable()
            .exceptionHandling().authenticationEntryPoint(unauthorizedHandler).and()
            .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and()
            .authorizeRequests()
            .antMatchers("/sysadmin/**").hasRole("SYSADMIN")
            .antMatchers("/admin/**").hasRole("ADMIN")
            .antMatchers("/siteadmin/**").hasRole("SITEADMIN")
            .antMatchers("/api/**").hasRole("USER")
            // add the specific swagger page to the security
            .antMatchers("/swagger-ui.html").hasRole("USER")
            .anyRequest().permitAll();

    // Custom JWT based security filter
    httpSecurity
            .addFilterBefore(authenticationTokenFilterBean(), UsernamePasswordAuthenticationFilter.class);

}

问题是它只保护 Swagger UI 页面,而不保护从 UI 页面作为 .json 文件加载的 API 规范。

更好的方法是将 swagger 文件放在一个路径下,这样您就可以添加 antMatchers("/swagger/**").hasRole("USER")

回答有点晚了。我进行了一个小的 POC 来执行相同的操作。我正在使用 Keycloak 和 Spring 安全。下面是我的配置

http
                .antMatcher("/**").authorizeRequests()
                .antMatchers("/swagger-resources/**","/swagger-ui.html**","/swagger-ui/").hasRole("admin")
                .anyRequest().authenticated()
                .and()
                .exceptionHandling()
                .accessDeniedHandler(new AccessDeniedHandlerImpl())
                .defaultAuthenticationEntryPointFor(authenticationEntryPoint(), new CustomRequestMatcher(AUTH_LIST))
                .and()
                .httpBasic()
                .authenticationEntryPoint(restAuthenticationEntryPoint)
                .and()
                .csrf().disable()
                .logout()
                    .logoutUrl("/logout")
                    .invalidateHttpSession(true)
                    .clearAuthentication(true)
                    .addLogoutHandler(keycloakLogoutHandler());

我有一个工作示例here