在 grails Spring 安全性中检查用户是否登录的最佳方法是什么?

What is the best way to check if the user is logged in or not in grails Spring Security?

我想检查用户是否登录或者not.I大多数项目都集成了拦截器来检查用户是否登录。但这不适用于 AJAX 请求并且匹配拦截器中的所有控制器会导致过滤器应用于所有控制器,包括 /static/** ,在这种情况下我将不得不排除 (.excludes( uri: "/static/**")) 。这是正确和标准的做法吗?

LoginInterceptor() {
        matchAll()
                .excludes(controller: "login") 
                .excludes(controller: "signUp") 
                .excludes(uri: "/static/**")
                .excludes(uri: "/assets/**")
    }
    boolean before() {
        if (!springSecurityService.isLoggedIn()) {
            flash.message="You need to login to access furthur pages"
            redirect( controller: 'login',action: 'auth');
            return false
        }
        true
    }

    boolean after() { true }

    void afterView() {
        // no-op
    }

以上代码不适用于 AJAX 请求。我如何制作一个适用于 AJAX 和 WEB 请求的通用拦截器?这是监控登录请求的标准方式吗?

如果您使用 spring 安全插件。您可以通过配置安全配置来做到这一点。您可以找到更多 here

对于 ajax 请求,您可以检查 return 代码,如果它是 == 403,您可以将用户重定向到登录页面。