Grails 使用spring security处理成功登录事件

Grails 使用spring security处理成功登录事件,grails,spring-security,grails-plugin,Grails,Spring Security,Grails Plugin,我的Grails应用程序使用Spring安全插件。每当用户成功登录时,我希望: 在会话中存储某些内容 将其重定向到自定义页面(取决于其角色) 我需要类似地处理注销事件,这非常简单,因为插件提供了一个名为logoutSuccessHandler的bean,可以覆盖它。我也希望找到一个名为loginsucesshandler的bean,但没有这样的运气 我阅读了插件文档中关于的页面,但两种事件处理机制似乎都不允许我访问当前的请求或会话。我最近在一个项目中使用它进行登录。这有点像黑客,但对我有效。

我的Grails应用程序使用Spring安全插件。每当用户成功登录时,我希望:

  • 在会话中存储某些内容
  • 将其重定向到自定义页面(取决于其角色)
我需要类似地处理注销事件,这非常简单,因为插件提供了一个名为
logoutSuccessHandler
的bean,可以覆盖它。我也希望找到一个名为
loginsucesshandler
的bean,但没有这样的运气


我阅读了插件文档中关于的页面,但两种事件处理机制似乎都不允许我访问当前的请求或会话。

我最近在一个项目中使用它进行登录。这有点像黑客,但对我有效。我正在使用插件的1.2.7.3版本

def auth() {        

    def config = SpringSecurityUtils.securityConfig

    if (springSecurityService.isLoggedIn()) {
        def user = User.get(principal.id)

        def roles = user.getAuthorities()

        def admin_role = Role.findByAuthority("ROLE_ADMIN")

        //this user is not admin
        if(!roles.contains(admin_role)){
            //perform redirect to appropriate page
        }
        redirect uri: config.successHandler.defaultTargetUrl
        //log.info(getPrincipal().username + "logged in at :"+new Date())
        return
    }

    String view = 'auth'
    String postUrl = "${request.contextPath}${config.apf.filterProcessesUrl}"
    render view: view, model: [postUrl: postUrl,
                               rememberMeParameter: config.rememberMe.parameter]
}
对于注销,我使用了注销控制器,在重定向到注销处理程序之前执行了一些操作:

class LogoutController {

     /**
     * Index action. Redirects to the Spring security logout uri.
     */
     def index = {
         // perform some action here
         redirect uri: SpringSecurityUtils.securityConfig.logout.filterProcessesUrl 
     }
}

如果你想在成功登录后做一些事情。您可以收听InteractiveAuthenticationSuccessEvent

class AuthenticationSuccessEventListener implements    
                       ApplicationListener<InteractiveAuthenticationSuccessEvent> {


    @Override
    public void onApplicationEvent(InteractiveAuthenticationSuccessEvent event) {
         .......do some stuff here
    }
   }
类身份验证SuccessEventListener实现
应用程序侦听器{
@凌驾
ApplicationEvent上的公共无效(InteractiveAuthenticationSuccessEvent事件){
……在这里做些事情
}
}
然后在resources.groovy中将AuthenticationSuccessEventListener注册为Springbean 您可以在这里做任何您想做的事情,但是您将无法从侦听器重定向


下面是另一个类似的

添加配置参数:

grails.plugins.springsecurity.successHandler.defaultTargetUrl = '/myLogin/handleSuccessLogin'
然后在处理此URL的操作中添加自定义登录处理

class MyLoginController {

  def springSecurityService

  def handleSuccessLogin() {
    session.foo = 'bar'

    if (springSecurityService.currentUser.username == 'bob') {
      redirect action: 'bobLogin'
    } else {
      redirect action: 'defaultLogin'
    }         
  }  

  def bobLogin() {
    // bob's login handler
  }

  def defaultLogin() {
    // default login handler
  }
}

您正在使用哪个版本的插件?他可以从这里访问会话吗?您可以从
SecurityRequestHolder.request.session
访问会话
LoginController
auth
操作在成功登录后不会被调用,所以我看不出第一个代码示例对我有什么帮助?好的。你找到解决办法了吗?你做了什么?Sudhir的答案看起来不错,但我没有尝试过。在登录时启动一个操作比使用侦听器方法更强大:直接访问http会话、操作重定向等。