-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: simplify AccessLimitInterceptor and remove SpringUtil class…
… which counters Spring pattern
- Loading branch information
Showing
8 changed files
with
77 additions
and
165 deletions.
There are no files selected for viewing
40 changes: 0 additions & 40 deletions
40
src/main/kotlin/plus/maa/backend/common/utils/SpringUtil.kt
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
src/main/kotlin/plus/maa/backend/config/accesslimit/AccessLimitConfig.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package plus.maa.backend.config.accesslimit | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper | ||
import org.springframework.context.annotation.Configuration | ||
import org.springframework.data.redis.core.StringRedisTemplate | ||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry | ||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer | ||
|
||
@Configuration | ||
class AccessLimitConfig( | ||
private val stringRedisTemplate: StringRedisTemplate, | ||
private val objectMapper: ObjectMapper, | ||
) : WebMvcConfigurer { | ||
|
||
override fun addInterceptors(registry: InterceptorRegistry) { | ||
registry.addInterceptor(AccessLimitInterceptor(stringRedisTemplate, objectMapper)) | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
src/main/kotlin/plus/maa/backend/config/accesslimit/AccessLimitInterceptor.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package plus.maa.backend.config.accesslimit | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper | ||
import io.github.oshai.kotlinlogging.KotlinLogging | ||
import jakarta.servlet.http.HttpServletRequest | ||
import jakarta.servlet.http.HttpServletResponse | ||
import org.springframework.data.redis.core.StringRedisTemplate | ||
import org.springframework.http.HttpStatus | ||
import org.springframework.web.method.HandlerMethod | ||
import org.springframework.web.servlet.HandlerInterceptor | ||
import plus.maa.backend.common.utils.IpUtil | ||
import plus.maa.backend.common.utils.WebUtils | ||
import plus.maa.backend.controller.response.MaaResult.Companion.fail | ||
import java.util.concurrent.TimeUnit | ||
|
||
/** | ||
* @author Baip1995 | ||
*/ | ||
class AccessLimitInterceptor( | ||
private val stringRedisTemplate: StringRedisTemplate, | ||
private val objectMapper: ObjectMapper, | ||
) : HandlerInterceptor { | ||
|
||
private val log = KotlinLogging.logger { } | ||
|
||
@Throws(Exception::class) | ||
override fun preHandle(request: HttpServletRequest, response: HttpServletResponse, handler: Any): Boolean { | ||
val ann = (handler as? HandlerMethod)?.method?.getAnnotation(AccessLimit::class.java) ?: return true | ||
// 拼接 redis key = IP + Api 限流 | ||
val key = IpUtil.getIpAddr(request) + request.requestURI | ||
|
||
// 获取 redis 的 value | ||
val count = stringRedisTemplate.opsForValue()[key]?.toInt() ?: 0 | ||
if (count < ann.times) { | ||
// 如果 redis 中的时间比注解上的时间小则表示可以允许访问,这时修改 redis 的 value 时间 | ||
stringRedisTemplate.opsForValue().set( | ||
key, | ||
(count + 1).toString(), | ||
ann.second.toLong(), | ||
TimeUnit.SECONDS | ||
) | ||
} else { | ||
// 请求过于频繁 | ||
log.info { "$key 请求过于频繁" } | ||
val result = fail(HttpStatus.TOO_MANY_REQUESTS.value(), "请求过于频繁") | ||
val json = objectMapper.writeValueAsString(result) | ||
WebUtils.renderString(response, json, HttpStatus.TOO_MANY_REQUESTS.value()) | ||
return false | ||
} | ||
|
||
return true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 0 additions & 108 deletions
108
src/main/kotlin/plus/maa/backend/handler/AccessLimitInterceptHandlerImpl.kt
This file was deleted.
Oops, something went wrong.