Как перехватывать запросы методом-обработчиком в Spring WebFlux - PullRequest
0 голосов
/ 22 мая 2018

В 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 с контроллерами в стиле аннотаций?

...