java - 如何使用 WebFluxConfigurer 完全禁用 Swagger 3.0.0 的身份验证?
问题描述
如果我遗漏了一些重要信息,我深表歉意,因为我对这些库没有经验。随意问他们!:)
我将 Spring Boot2.3.2.RELEASE
与 Spring CloudHoxton.SR6
和 Springfox 一起使用3.0.0
。我使用的安全性是spring-boot-starter-security
. 以下是相关的pom.xml
依赖项:
pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>${springfox.version}</version>
</dependency>
我在上使用以下注释Application
:
@EnableWebFlux
@EnableDiscoveryClient
@SpringBootApplication
这是我的一个样本SwaggerResourceProvider
:
return () -> Stream.of(
swaggerResource("Commander", "/docs/commander", "1.0"),
...
swaggerResource("Querier", "/docs/querier", "3.0")
).collect(Collectors.toList());
这是我的SwaggerResource
:
private SwaggerResource swaggerResource(String name, String location, String serviceHighestVersion) {
SwaggerResource swaggerResource = new SwaggerResource();
swaggerResource.setName(name);
swaggerResource.setLocation(location);
swaggerResource.setSwaggerVersion(serviceHighestVersion);
return swaggerResource;
}
这是我的SecurityConfig
:
@EnableSwagger2
public class SecurityConfig {
@Bean
public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
ServerHttpSecurity http_ = http
.csrf().disable()
.cors().disable()
.httpBasic().disable();
http_
.authorizeExchange()
.pathMatchers(
"/v2/api-docs",
"/swagger-resources/**",
"/swagger-ui.html**",
"/webjars/**",
"/swagger-ui/**",
"favicon.ico"
)
.permitAll()
.anyExchange()
.permitAll();
return http_.build();
}
@Bean
public ReactiveAuthenticationManager authManager() {
return (authentication) -> {
authentication.setAuthenticated(false);
return Mono.just(authentication);
};
}
最后(我认为)是WebFluxConfigurer
:
public class SwaggerUiWebFluxConfigurer implements WebFluxConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/swagger-ui.html**")
.addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/");
}
@Bean
public Docket restApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.genericModelSubstitutes(Optional.class)
.select()
.paths(PathSelectors.any())
.apis(RequestHandlerSelectors.any())
.build()
.useDefaultResponseMessages(false);
}
@Bean
UiConfiguration uiConfig() {
return UiConfigurationBuilder.builder()
.docExpansion(DocExpansion.LIST)
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("Workflow Services")
.description("Workflow Services API Description")
.contact(
new Contact(
"My Team",
"https://moo.com",
"hello@there.com"))
.version("1.0.0")
.build();
}
}
我去 http://localhost:8080/swagger-ui 并被转发到 http://localhost:8080/login
我只想为大摇大摆禁用安全性。有什么帮助吗?
解决方案
就用这个,
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers("/v2/api-docs",
"/configuration/ui",
"/swagger-resources/**",
"/configuration/security",
"/swagger-ui/**",
"/swagger-ui",
"/webjars/**");
}
推荐阅读
- arduino - 在 ESP8266 的单个 GPIO 引脚上检测来自多个组件的输入
- java - Java Spring:在控制器中获取当前映射
- javascript - Plotly.js x 轴无法删除线
- c# - IIS Express 中的 Windows 身份验证不会获取用户详细信息
- javascript - [ant]warning.js:34 警告:重复的字段名称将导致两个字段一起被编辑。字段名称必须是唯一的
- typo3 - TYPO3 哪些文件包含扩展信息
- python - 如何在 Python 中使用 NLP 分析聊天对话中的问题
- dpkg - 以静默模式运行 dpkg --configure -a
- ios - 在 react js 中使用 Web Api
- r-markdown - R变量作为Rmarkdown中的页脚