Security中集成Swagger

2023-05-12 SpringSecurity

SpringBoot集成SpringSecurity之后,Swagger生成的json文件及页面在不携带token的情况下全部禁止访问,所以本篇文章将放开Swagger相关静态资源,如果您有其他静态资源需要放开,在此添加即可。

我的系统不单集成Swagger,还集成了knife4j相当于增强版Swagger,通过下面配置一并放开。

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig {

    @Bean
    WebSecurityCustomizer webSecurityCustomizer() {
        return web -> web.ignoring().antMatchers( "/login","css/**","/js/**","/image/*")
            .antMatchers("/assets/**")
            .antMatchers("/webjars/**")
            .antMatchers("/swagger-resources/**")
            .antMatchers("/v2/api-docs/**")
            .antMatchers("/v3/api-docs/**")
            .antMatchers("/swagger-ui/**")
            .antMatchers("/doc.html")
            .requestMatchers(PathRequest.toStaticResources().atCommonLocations());
    }
}
上次更新: 7 个月前