【问题标题】:Restrict access to Swagger UI限制对 Swagger UI 的访问
【发布时间】:2021-04-07 00:10:59
【问题描述】:

我有使用 spring-boot 的招摇 UI。我的 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);

}

【问题讨论】:

  • 通常值得包括您当前如何定义安全性,以便人们可以提供适合您设置的解决方案。有很多方法可以做到这一点,但了解更多关于您的实施会有所帮助。
  • 更新了我的问题

标签: spring security spring-boot swagger-ui swagger-2.0


【解决方案1】:

一个不知道更多关于你的配置的建议来自这个 SO question。

https://stackoverflow.com/a/24920752/1499549

您可以在此处添加更新的问题详细信息示例:

@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 页面,而不是作为 .json 文件从该 UI 页面加载的 API 规范。

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

【讨论】:

    【解决方案2】:

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

    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

    【讨论】:

      猜你喜欢
      • 2017-02-20
      • 1970-01-01
      • 2018-01-27
      • 2018-06-08
      • 2017-05-01
      • 2016-01-15
      • 2013-03-17
      • 2021-04-24
      • 2020-07-20
      相关资源
      最近更新 更多