在没有Spring Security或Spring Boot的情况下禁用springfox Swagger UI的csrf保护,可以通过以下步骤实现:
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("com.example.controller"))
.paths(PathSelectors.any())
.build();
}
}
@Configuration
@EnableSwagger2
public class SwaggerConfig {
// ...
@Bean
public FilterRegistrationBean<Filter> swaggerUiCsrfProtectionFilter() {
FilterRegistrationBean<Filter> registration = new FilterRegistrationBean<>();
registration.setFilter(new OncePerRequestFilter() {
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
filterChain.doFilter(request, response);
}
});
registration.setOrder(Ordered.HIGHEST_PRECEDENCE);
registration.addUrlPatterns("/swagger-ui.html");
return registration;
}
}
通过以上步骤,你可以在没有Spring Security或Spring Boot的情况下禁用springfox Swagger UI的csrf保护。请注意,这只是一种解决方案,具体实现可能因项目的不同而有所差异。
没有搜到相关的沙龙
领取专属 10元无门槛券
手把手带您无忧上云