2016-04-29 29 views
0

我一直在尝试使用grails 3拦截器在第一次登录时捕获用户电子邮件,但是在捕获电子邮件后,下次他们登录后,他们被重定向到电子邮件捕获表单而不是设置的主页。如果这是相关信息,我正在使用spring security。我的代码片段咆哮显示了我迄今为止所做的。请告诉我是否有更简单的方法来实现我的目标或我做错了什么。如何使用grails 3拦截器来强制执行表单然后恢复grails应用程序的正常流程

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 
} 

}

回答

1

尝试像这样

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 
} 
} 
} 
+0

谢谢,这是有益的 –

相关问题