Add security for openapi endpoint doc

This commit is contained in:
Sambo Chea 2021-12-11 12:06:10 +07:00
parent 6d1ebcc414
commit 58087efdc1
Signed by: sombochea
GPG Key ID: 3C7CF22A05D95490
3 changed files with 35 additions and 13 deletions

View File

@ -1,5 +1,7 @@
package com.cubetiqs.web.config
import io.swagger.v3.oas.annotations.enums.SecuritySchemeType
import io.swagger.v3.oas.annotations.security.SecurityScheme
import io.swagger.v3.oas.models.ExternalDocumentation
import io.swagger.v3.oas.models.OpenAPI
import io.swagger.v3.oas.models.info.Info
@ -9,9 +11,15 @@ import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
@Configuration
class SpringDocConfig {
@SecurityScheme(
name = "bearerAuth",
type = SecuritySchemeType.HTTP,
scheme = "bearer",
bearerFormat = "JWT",
)
class OpenApiDocConfig {
companion object {
private val PUBLIC_API_PATH get() = "/public/**"
private val ADMIN_API_PATH get() = "/admin/**"
private val DEFAULT_API_PATH get() = "/**"
}
@ -19,25 +27,17 @@ class SpringDocConfig {
@Bean
fun defaultApi(): GroupedOpenApi {
return GroupedOpenApi.builder()
.group("default-api")
.group("default")
.pathsToMatch(DEFAULT_API_PATH)
.pathsToExclude("/error", PUBLIC_API_PATH, ADMIN_API_PATH)
.pathsToExclude("/error", ADMIN_API_PATH)
.packagesToScan("com.cubetiqs.web")
.build()
}
@Bean
fun publicApi(): GroupedOpenApi {
return GroupedOpenApi.builder()
.group("public-api")
.pathsToMatch(PUBLIC_API_PATH)
.build()
}
@Bean
fun adminApi(): GroupedOpenApi {
return GroupedOpenApi.builder()
.group("admin-api")
.group("admin")
.pathsToMatch(ADMIN_API_PATH)
.build()
}

View File

@ -0,0 +1,21 @@
package com.cubetiqs.web.controller.admin
import com.cubetiqs.web.controller.BaseController
import com.cubetiqs.web.util.RouteConstants
import io.swagger.v3.oas.annotations.Operation
import io.swagger.v3.oas.annotations.security.SecurityRequirement
import io.swagger.v3.oas.annotations.tags.Tag
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
@Tag(name = "Admin")
@RestController
@RequestMapping(RouteConstants.ADMIN)
class AdminController : BaseController {
@Operation(security = [SecurityRequirement(name = "bearerAuth")])
@GetMapping
fun getAdmin(): String {
return "Admin"
}
}

View File

@ -2,6 +2,7 @@ package com.cubetiqs.web.util
object RouteConstants {
const val INDEX = "/"
const val ADMIN = "/admin"
}
object AppConstants {