@@ -5,92 +5,51 @@ import io.swagger.v3.core.jackson.ModelResolver
5
5
import io.swagger.v3.oas.models.Components
6
6
import io.swagger.v3.oas.models.ExternalDocumentation
7
7
import io.swagger.v3.oas.models.OpenAPI
8
- import io.swagger.v3.oas.models.Operation
9
8
import io.swagger.v3.oas.models.info.Info
10
9
import io.swagger.v3.oas.models.info.License
11
- import io.swagger.v3.oas.models.security.SecurityRequirement
12
10
import io.swagger.v3.oas.models.security.SecurityScheme
13
- import org.springdoc.core.customizers.OperationCustomizer
14
- import org.springframework.beans.factory.annotation.Value
15
11
import org.springframework.context.annotation.Bean
16
12
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
19
14
20
15
/* *
21
16
* @author AnselYuki
22
17
*/
23
18
@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
34
22
35
23
@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
+ })
58
49
}
59
50
60
- /* *
61
- * 为使用了 [CurrentUser] 注解的接口在 OpenAPI 上添加 security scheme
62
- */
63
51
@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)
94
53
95
54
companion object {
96
55
const val SECURITY_SCHEME_JWT : String = " Jwt"
0 commit comments