如何仅使用 grails 3 拦截器来强制执行表单,然后恢复 grails 应用程序的正常流程

How to use a grails 3 interceptor only to enforce a form and then resume normal flow of grails app

我一直在尝试使用 grails 3 拦截器在首次登录时捕获用户电子邮件,但是在捕获电子邮件后,他们下次登录时将被重定向到电子邮件捕获表单而不是设置的主页。如果这是相关信息,我正在使用 spring 安全性。我的代码片段波纹管显示了我到目前为止所做的。如果有更简单的方法可以实现我的目标或我做错了什么,请告诉我。

class HomeInterceptor {

public HomeInterceptor() {
    // match all requests except requests
    // to the auth controller
    matchAll().excludes(controller: 'logout')
            .excludes(controller: 'login')
            .excludes(controller: 'home')
            .excludes(controller: 'user', action: 'userProfile')
            .excludes(controller: 'user', action: 'showUserProfile')
}

boolean before() {
    // if the user's email has not been captured,
    // redirect to the user profile

    def springSecurityService = AppHolder.bean(SpringSecurityService)
    def loggedInUser = springSecurityService.currentUser as User

    if (loggedInUser?.email == null){
        flash.message = "Please enter your email before you can proceed"
        redirect(controller: 'user', action: 'userProfile')
        return false
    }else {
        return true
    }
}

boolean after() { true }

void afterView() {
    // no-op
}

}

像这样尝试

import grails.web.mapping.LinkGenerator

class HomeInterceptor {

def springSecurityService
LinkGenerator grailsLinkGenerator

public HomeInterceptor() {
// match all requests except requests
// to the auth controller
matchAll().excludes(controller: 'logout')
        .excludes(controller: 'login')
        .excludes(controller: 'home')
        .excludes(controller: 'user', action: 'userProfile')
        .excludes(controller: 'user', action: 'showUserProfile')
}

boolean before() {

if (!springSecurityService.principal.email){
    flash.message = "Please enter your email before you can proceed"
    response.sendRedirect("${grailsLinkGenerator.link(controller:'user',action:'userProfile')}")
    return false
}else {
    return true
}
}
}