From 925ee5d742fe82504d001e4fb099387c3f8f3fa4 Mon Sep 17 00:00:00 2001 From: Handiwork Date: Fri, 23 Feb 2024 21:51:18 +0800 Subject: [PATCH] refactor: remove `@CurrentUser` --- .../backend/common/annotation/CurrentUser.kt | 11 -- .../backend/common/annotation/JsonSchema.kt | 4 +- .../annotation/SensitiveWordDetection.kt | 4 +- .../maa/backend/config/doc/SpringDocConfig.kt | 101 ++++++------------ 4 files changed, 32 insertions(+), 88 deletions(-) delete mode 100644 src/main/kotlin/plus/maa/backend/common/annotation/CurrentUser.kt diff --git a/src/main/kotlin/plus/maa/backend/common/annotation/CurrentUser.kt b/src/main/kotlin/plus/maa/backend/common/annotation/CurrentUser.kt deleted file mode 100644 index b4aa1a83..00000000 --- a/src/main/kotlin/plus/maa/backend/common/annotation/CurrentUser.kt +++ /dev/null @@ -1,11 +0,0 @@ -package plus.maa.backend.common.annotation - -import org.springframework.security.core.annotation.AuthenticationPrincipal - -/** - * @author john180 - */ -@Target(AnnotationTarget.VALUE_PARAMETER) -@Retention(AnnotationRetention.RUNTIME) -@AuthenticationPrincipal -annotation class CurrentUser diff --git a/src/main/kotlin/plus/maa/backend/common/annotation/JsonSchema.kt b/src/main/kotlin/plus/maa/backend/common/annotation/JsonSchema.kt index 7bca737d..32826528 100644 --- a/src/main/kotlin/plus/maa/backend/common/annotation/JsonSchema.kt +++ b/src/main/kotlin/plus/maa/backend/common/annotation/JsonSchema.kt @@ -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 diff --git a/src/main/kotlin/plus/maa/backend/common/annotation/SensitiveWordDetection.kt b/src/main/kotlin/plus/maa/backend/common/annotation/SensitiveWordDetection.kt index 78de8081..6f8e22e0 100644 --- a/src/main/kotlin/plus/maa/backend/common/annotation/SensitiveWordDetection.kt +++ b/src/main/kotlin/plus/maa/backend/common/annotation/SensitiveWordDetection.kt @@ -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 表达式 diff --git a/src/main/kotlin/plus/maa/backend/config/doc/SpringDocConfig.kt b/src/main/kotlin/plus/maa/backend/config/doc/SpringDocConfig.kt index 186d47d8..5e21f73a 100644 --- a/src/main/kotlin/plus/maa/backend/config/doc/SpringDocConfig.kt +++ b/src/main/kotlin/plus/maa/backend/config/doc/SpringDocConfig.kt @@ -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"