Skip to content

Commit 925ee5d

Browse files
committed
refactor: remove @CurrentUser
1 parent 22856f5 commit 925ee5d

File tree

4 files changed

+32
-88
lines changed

4 files changed

+32
-88
lines changed

src/main/kotlin/plus/maa/backend/common/annotation/CurrentUser.kt

-11
This file was deleted.

src/main/kotlin/plus/maa/backend/common/annotation/JsonSchema.kt

+1-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,5 @@ package plus.maa.backend.common.annotation
55
* Date 2023-01-22 17:49
66
*/
77
@Target(AnnotationTarget.FUNCTION, AnnotationTarget.PROPERTY_GETTER, AnnotationTarget.PROPERTY_SETTER)
8-
@Retention(
9-
AnnotationRetention.RUNTIME
10-
)
8+
@Retention(AnnotationRetention.RUNTIME)
119
annotation class JsonSchema

src/main/kotlin/plus/maa/backend/common/annotation/SensitiveWordDetection.kt

+1-3
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@ package plus.maa.backend.common.annotation
1010
*/
1111
@MustBeDocumented
1212
@Target(AnnotationTarget.FUNCTION, AnnotationTarget.PROPERTY_GETTER, AnnotationTarget.PROPERTY_SETTER)
13-
@Retention(
14-
AnnotationRetention.RUNTIME
15-
)
13+
@Retention(AnnotationRetention.RUNTIME)
1614
annotation class SensitiveWordDetection(
1715
/**
1816
* SpEL 表达式

src/main/kotlin/plus/maa/backend/config/doc/SpringDocConfig.kt

+30-71
Original file line numberDiff line numberDiff line change
@@ -5,92 +5,51 @@ import io.swagger.v3.core.jackson.ModelResolver
55
import io.swagger.v3.oas.models.Components
66
import io.swagger.v3.oas.models.ExternalDocumentation
77
import io.swagger.v3.oas.models.OpenAPI
8-
import io.swagger.v3.oas.models.Operation
98
import io.swagger.v3.oas.models.info.Info
109
import io.swagger.v3.oas.models.info.License
11-
import io.swagger.v3.oas.models.security.SecurityRequirement
1210
import io.swagger.v3.oas.models.security.SecurityScheme
13-
import org.springdoc.core.customizers.OperationCustomizer
14-
import org.springframework.beans.factory.annotation.Value
1511
import org.springframework.context.annotation.Bean
1612
import org.springframework.context.annotation.Configuration
17-
import org.springframework.web.method.HandlerMethod
18-
import plus.maa.backend.common.annotation.CurrentUser
13+
import plus.maa.backend.config.external.MaaCopilotProperties
1914

2015
/**
2116
* @author AnselYuki
2217
*/
2318
@Configuration
24-
class SpringDocConfig(
25-
@Value("\${maa-copilot.info.version}")
26-
private val version: String,
27-
@Value("\${maa-copilot.info.title}")
28-
private val title: String,
29-
@Value("\${maa-copilot.info.description}")
30-
private val description: String,
31-
@Value("\${maa-copilot.jwt.header}")
32-
private val securitySchemeHeader: String
33-
) {
19+
class SpringDocConfig(properties: MaaCopilotProperties) {
20+
private val _info = properties.info
21+
private val jwt = properties.jwt
3422

3523
@Bean
36-
fun emergencyLogistics(): OpenAPI {
37-
return OpenAPI()
38-
.info(docInfos())
39-
.externalDocs(
40-
ExternalDocumentation()
41-
.description("GitHub repo")
42-
.url("https://github.com/MaaAssistantArknights/MaaBackendCenter")
43-
)
44-
.components(
45-
Components()
46-
.addSecuritySchemes(
47-
SECURITY_SCHEME_JWT,
48-
SecurityScheme()
49-
.type(SecurityScheme.Type.HTTP)
50-
.scheme("bearer")
51-
.`in`(SecurityScheme.In.HEADER)
52-
.name(securitySchemeHeader)
53-
.description(
54-
"JWT Authorization header using the Bearer scheme. Raw head example: \"$securitySchemeHeader: Bearer {token}\""
55-
)
56-
)
57-
)
24+
fun emergencyLogistics(): OpenAPI = OpenAPI().apply {
25+
info(Info().apply {
26+
title(_info.title)
27+
description(_info.description)
28+
version(_info.version)
29+
license(License().apply {
30+
name("GNU Affero General Public License v3.0")
31+
url("https://www.gnu.org/licenses/agpl-3.0.html")
32+
})
33+
})
34+
externalDocs(ExternalDocumentation().apply {
35+
description("GitHub repo")
36+
url("https://github.com/MaaAssistantArknights/MaaBackendCenter")
37+
})
38+
components(Components().apply {
39+
addSecuritySchemes(SECURITY_SCHEME_JWT, SecurityScheme().apply {
40+
type(SecurityScheme.Type.HTTP)
41+
scheme("bearer")
42+
`in`(SecurityScheme.In.HEADER)
43+
name(jwt.header)
44+
val s = "JWT Authorization header using the Bearer scheme. Raw head example: " +
45+
"\"${jwt.header}: Bearer {token}\""
46+
description(s)
47+
})
48+
})
5849
}
5950

60-
/**
61-
* 为使用了 [CurrentUser] 注解的接口在 OpenAPI 上添加 security scheme
62-
*/
6351
@Bean
64-
fun currentUserOperationCustomizer(): OperationCustomizer {
65-
return OperationCustomizer { operation: Operation, handlerMethod: HandlerMethod ->
66-
for (parameter in handlerMethod.methodParameters) {
67-
if (parameter.hasParameterAnnotation(CurrentUser::class.java)) {
68-
// 已有 security scheme
69-
if (operation.security.any { it.containsKey(SECURITY_SCHEME_JWT) }) {
70-
break
71-
}
72-
73-
// 添加 security scheme
74-
operation.security.add(SecurityRequirement().addList(SECURITY_SCHEME_JWT))
75-
break
76-
}
77-
}
78-
operation
79-
}
80-
}
81-
82-
private fun docInfos() = Info()
83-
.title(title)
84-
.description(description)
85-
.version(version)
86-
.license(
87-
License()
88-
.name("GNU Affero General Public License v3.0")
89-
.url("https://www.gnu.org/licenses/agpl-3.0.html")
90-
)
91-
92-
@Bean
93-
fun modelResolver(objectMapper: ObjectMapper?) = ModelResolver(objectMapper)
52+
fun modelResolver(objectMapper: ObjectMapper) = ModelResolver(objectMapper)
9453

9554
companion object {
9655
const val SECURITY_SCHEME_JWT: String = "Jwt"

0 commit comments

Comments
 (0)