Skip to content

Commit

Permalink
refactor: remove @CurrentUser
Browse files Browse the repository at this point in the history
  • Loading branch information
Handiwork committed Feb 23, 2024
1 parent 22856f5 commit 925ee5d
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 88 deletions.
11 changes: 0 additions & 11 deletions src/main/kotlin/plus/maa/backend/common/annotation/CurrentUser.kt

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,5 @@ package plus.maa.backend.common.annotation
* Date 2023-01-22 17:49
*/
@Target(AnnotationTarget.FUNCTION, AnnotationTarget.PROPERTY_GETTER, AnnotationTarget.PROPERTY_SETTER)
@Retention(
AnnotationRetention.RUNTIME
)
@Retention(AnnotationRetention.RUNTIME)
annotation class JsonSchema
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ package plus.maa.backend.common.annotation
*/
@MustBeDocumented
@Target(AnnotationTarget.FUNCTION, AnnotationTarget.PROPERTY_GETTER, AnnotationTarget.PROPERTY_SETTER)
@Retention(
AnnotationRetention.RUNTIME
)
@Retention(AnnotationRetention.RUNTIME)
annotation class SensitiveWordDetection(
/**
* SpEL 表达式
Expand Down
101 changes: 30 additions & 71 deletions src/main/kotlin/plus/maa/backend/config/doc/SpringDocConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,92 +5,51 @@ import io.swagger.v3.core.jackson.ModelResolver
import io.swagger.v3.oas.models.Components
import io.swagger.v3.oas.models.ExternalDocumentation
import io.swagger.v3.oas.models.OpenAPI
import io.swagger.v3.oas.models.Operation
import io.swagger.v3.oas.models.info.Info
import io.swagger.v3.oas.models.info.License
import io.swagger.v3.oas.models.security.SecurityRequirement
import io.swagger.v3.oas.models.security.SecurityScheme
import org.springdoc.core.customizers.OperationCustomizer
import org.springframework.beans.factory.annotation.Value
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.web.method.HandlerMethod
import plus.maa.backend.common.annotation.CurrentUser
import plus.maa.backend.config.external.MaaCopilotProperties

/**
* @author AnselYuki
*/
@Configuration
class SpringDocConfig(
@Value("\${maa-copilot.info.version}")
private val version: String,
@Value("\${maa-copilot.info.title}")
private val title: String,
@Value("\${maa-copilot.info.description}")
private val description: String,
@Value("\${maa-copilot.jwt.header}")
private val securitySchemeHeader: String
) {
class SpringDocConfig(properties: MaaCopilotProperties) {
private val _info = properties.info
private val jwt = properties.jwt

@Bean
fun emergencyLogistics(): OpenAPI {
return OpenAPI()
.info(docInfos())
.externalDocs(
ExternalDocumentation()
.description("GitHub repo")
.url("https://github.com/MaaAssistantArknights/MaaBackendCenter")
)
.components(
Components()
.addSecuritySchemes(
SECURITY_SCHEME_JWT,
SecurityScheme()
.type(SecurityScheme.Type.HTTP)
.scheme("bearer")
.`in`(SecurityScheme.In.HEADER)
.name(securitySchemeHeader)
.description(
"JWT Authorization header using the Bearer scheme. Raw head example: \"$securitySchemeHeader: Bearer {token}\""
)
)
)
fun emergencyLogistics(): OpenAPI = OpenAPI().apply {
info(Info().apply {
title(_info.title)
description(_info.description)
version(_info.version)
license(License().apply {
name("GNU Affero General Public License v3.0")
url("https://www.gnu.org/licenses/agpl-3.0.html")
})
})
externalDocs(ExternalDocumentation().apply {
description("GitHub repo")
url("https://github.com/MaaAssistantArknights/MaaBackendCenter")
})
components(Components().apply {
addSecuritySchemes(SECURITY_SCHEME_JWT, SecurityScheme().apply {
type(SecurityScheme.Type.HTTP)
scheme("bearer")
`in`(SecurityScheme.In.HEADER)
name(jwt.header)
val s = "JWT Authorization header using the Bearer scheme. Raw head example: " +
"\"${jwt.header}: Bearer {token}\""
description(s)
})
})
}

/**
* 为使用了 [CurrentUser] 注解的接口在 OpenAPI 上添加 security scheme
*/
@Bean
fun currentUserOperationCustomizer(): OperationCustomizer {
return OperationCustomizer { operation: Operation, handlerMethod: HandlerMethod ->
for (parameter in handlerMethod.methodParameters) {
if (parameter.hasParameterAnnotation(CurrentUser::class.java)) {
// 已有 security scheme
if (operation.security.any { it.containsKey(SECURITY_SCHEME_JWT) }) {
break
}

// 添加 security scheme
operation.security.add(SecurityRequirement().addList(SECURITY_SCHEME_JWT))
break
}
}
operation
}
}

private fun docInfos() = Info()
.title(title)
.description(description)
.version(version)
.license(
License()
.name("GNU Affero General Public License v3.0")
.url("https://www.gnu.org/licenses/agpl-3.0.html")
)

@Bean
fun modelResolver(objectMapper: ObjectMapper?) = ModelResolver(objectMapper)
fun modelResolver(objectMapper: ObjectMapper) = ModelResolver(objectMapper)

companion object {
const val SECURITY_SCHEME_JWT: String = "Jwt"
Expand Down

0 comments on commit 925ee5d

Please sign in to comment.