【发布时间】:2018-10-31 13:47:19
【问题描述】:
我在 Spring MVC 中有以下拦截器,用于检查用户是否可以访问处理程序方法:
class AccessInterceptor : HandlerInterceptorAdapter() {
override fun preHandle(request: HttpServletRequest, response: HttpServletResponse, handler: Any?): Boolean {
val auth: Auth =
(if (method.getAnnotation(Auth::class.java) != null) {
method.getAnnotation(Auth::class.java)
} else {
method.declaringClass.getAnnotation(Auth::class.java)
}) ?: return true
if (auth.value == AuthType.ALLOW) {
return true
}
val user = getUserFromRequest(request) // checks request for auth token
// and checking auth for out user in future.
return renderError(403, response)
在我的控制器中,我会注释方法,如下所示:
@GetMapping("/foo")
@Auth(AuthType.ALLOW)
fun doesntNeedAuth(...) { ... }
@GetMapping("/bar")
@Auth(AuthType.ADMIN)
fun adminMethod(...) { ... }
如果用户的令牌错误或没有权限,则会返回错误。
是否可以在 Spring WebFlux 中使用注释样式的控制器来执行此操作?
【问题讨论】:
-
也许这篇文章会有所帮助。 stackoverflow.com/questions/51840647/…
标签: spring spring-mvc spring-webflux