- 前言
由于权限框架对所有的请求都进行了拦截,发现Swagger页面的jss和css都被拦截了,这样就需要对Swagger所有的资源请求路径放开
- 解决办法
一、首先要引入对应的Swagger依赖
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.7.0</version>
</dependency>
二、添加一下对应的Swagger配置类
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket createRestApi(){
return new Docket(DocumentationType.SWAGGER_2)
.enable(true)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.gsww.zwfwsx.controller"))
.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo(){
return new ApiInfoBuilder()
.title("zwfwsx-Api")
.description("zwfwsx Api")
.version("0.0.1")
.build();
}
}
三、然而Spring Security集成Swagger后上面这2步是不能成功的,还是资源请求会被拦截,要在你的Spring Security的配置类里面加入对资源请求的放开
httpSecurity.csrf().disable().sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and().authorizeRequests()
// 所有/trace/user/ 的所有请求 都放行
.antMatchers("/trace/users/**").permitAll()
// swagger start
.antMatchers("/swagger-ui.html").permitAll()
.antMatchers("/swagger-resources/**").permitAll()
.antMatchers("/images/**").permitAll()
.antMatchers("/webjars/**").permitAll()
.antMatchers("/v2/api-docs").permitAll()
.antMatchers("/configuration/ui").permitAll()
.antMatchers("/configuration/security").permitAll()
// swagger end
// 所有请求都需要认证
.anyRequest().authenticated();
httpSecurity.headers().cacheControl();
httpSecurity.csrf().disable();
httpSecurity.addFilterBefore(jwtAuthenticationTokenFilter, UsernamePasswordAuthenticationFilter.class);