Add authentication
This commit is contained in:
26
src/main/kotlin/top/fatweb/api/util/ApiVersionCondition.kt
Normal file
26
src/main/kotlin/top/fatweb/api/util/ApiVersionCondition.kt
Normal file
@@ -0,0 +1,26 @@
|
||||
package top.fatweb.api.util
|
||||
|
||||
import jakarta.servlet.http.HttpServletRequest
|
||||
import org.springframework.web.servlet.mvc.condition.RequestCondition
|
||||
import java.util.regex.Pattern
|
||||
|
||||
class ApiVersionCondition(private val apiVersion: Int) : RequestCondition<ApiVersionCondition> {
|
||||
private val versionPrefixPattern: Pattern = Pattern.compile(".*v(\\d+).*")
|
||||
|
||||
override fun combine(other: ApiVersionCondition): ApiVersionCondition = ApiVersionCondition(other.apiVersion)
|
||||
|
||||
override fun getMatchingCondition(request: HttpServletRequest): ApiVersionCondition? {
|
||||
val matcher = versionPrefixPattern.matcher(request.requestURI)
|
||||
if (matcher.find()) {
|
||||
val version = matcher.group(1).toInt()
|
||||
if (version >= this.apiVersion) {
|
||||
return this
|
||||
}
|
||||
}
|
||||
|
||||
return null
|
||||
}
|
||||
|
||||
override fun compareTo(other: ApiVersionCondition, request: HttpServletRequest): Int =
|
||||
other.apiVersion - this.apiVersion
|
||||
}
|
||||
Reference in New Issue
Block a user