我正在开发一个包含一些公共和受保护资源的 webflux 应用程序。
我的依赖:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>3.0.0</version>
<type>pom</type>
<scope>import</scope>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-stater-web</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb-reactive</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webflux-ui</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-api</artifactId>
<version>0.11.5</version>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-impl</artifactId>
<version>0.11.5</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-jackson</artifactId>
<version>0.11.5</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.liquibase.ext</groupId>
<artifactId>liquibase-mongodb</artifactId>
<version>4.20.0</version>
</dependency>
<dependency>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-core</artifactId>
<version>4.20.0</version>
</dependency>
</dependencies>
应用程序.yml:
spring:
main:
web-application-type: reactive
data:
mongodb:
port: 27017
database: vilya_be
liquibase:
change-log: classpath:/mongo/changelog-master.xml
springdoc:
api-docs:
path: /v3/swagger-ui.html
swagger-ui:
path: /v3/api-docs
Java代码:
@Configuration
@EnableWebFluxSecurity
@EnableMethodSecurity(securedEnabled = true, jsr250Enabled = true)
@RequiredArgsConstructor
public class JWTSecurityConfig {
private final BearerAuthenticationFilter bearerAuthenticationFilter;
@Bean
public SecurityWebFilterChain filterChain(ServerHttpSecurity http) {
return http.cors()
.and()
.csrf()
.disable()
.httpBasic()
.disable()
.exceptionHandling()
.authenticationEntryPoint(entryPoint)
.and()
.authorizeExchange()
.pathMatchers(USER_V1 + REGISTRATION, USER_V1 + SIGN_IN, "/v3/**")
.permitAll()
.anyExchange()
.authenticated()
.and()
.securityContextRepository(NoOpServerSecurityContextRepository.getInstance())
.addFilterAt(
bearerAuthenticationFilter,
SecurityWebFiltersOrder.AUTHENTICATION)
.build();
}
}
@RestController
@RequestMapping(USER_V1)
@RequiredArgsConstructor
public class UserResource {
private final UserService userService;
private final AuthenticationService authenticationService;
@PostMapping(REGISTRATION)
public Mono<RegistrationResponse> register(
@Valid @RequestBody Mono<RegistrationRequest> request) {
return userService.register(request);
}
@PostMapping(SIGN_IN)
public Mono<TokenPayload> signIn(@RequestBody Mono<SignInRequest> request) {
return authenticationService.authenticate(request);
}
@GetMapping
public Flux<User> findAll() {
return userService.findAll();
}
}
前两个 api 是公开的,我对它们没有问题,但最后一个 api 是受保护的,所以我希望 swagger ui 会有一个授权或标头部分让我放置不记名令牌。 当前行为:
正如我所提到的,我正在使用
springdoc-openapi-starter-webflux-ui
,我认为它会自动配置,所以我想我不需要手动创建 @Bean
类型的 OpenAPI
。但我不知道如何配置它以尊重我的安全配置。请帮忙,我也是新手。
根据文档:
您应该将
标签添加到受保护的 API。@SecurityRequirement
例如:
@Operation(security = { @SecurityRequirement(name = "bearer-key") })
安全定义示例:
@Bean
public OpenAPI customOpenAPI() {
return new OpenAPI()
.components(
new Components()
.addSecuritySchemes(
"bearer-key",
new SecurityScheme()
.type(SecurityScheme.Type.HTTP)
.scheme("bearer")
.bearerFormat("JWT")));
}
之后,一个小锁图标将出现在受保护的 API 的右侧,如果单击,将显示授权弹出窗口。
在
swagger-ui
页面的右上角也会有授权按钮。