Add authentication

This commit is contained in:
2023-10-05 21:11:22 +08:00
parent 78de04713f
commit 8e5375ab30
24 changed files with 580 additions and 15 deletions

View 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
}