用spring安全处理成功的登录事件 [英] handle successful login event with spring security

查看:81
本文介绍了用spring安全处理成功的登录事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Grails应用程序使用Spring Security插件。每当用户成功登录时,我都会:


  • 在会话中存储内容

  • 重定向它们到一个自定义页面(取决于他们的角色)


    我需要类似地处理注销事件,这非常简单,因为插件提供了一个名为 logoutSuccessHandler 的bean可以被重写。我希望能够类似地找到一个名为 loginSuccessHandler 的bean,但是没有这样的运气。 插件关于事件处理的文档,但是这两种事件处理机制似乎都无法让我访问当前的请求或会话。

    解决方案

    添加配置参数:

    $ p $ grails.plugins.springsecurity.successHandler.defaultTargetUrl ='/ myLogin / handleSuccessLogin'

    code>

    然后在处理此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的登录处理程序

    $ b $ def def defaultLogin(){
    //默认登录处理程序
    }
    }


    My Grails app uses the Spring Security plugin. Whenever a user successfully logs in I want to:

    • store something in the session
    • redirect them to a custom page (depending on their role)

    I need to handle logout events similarly, which was pretty straightforward because the plugin provides a bean named logoutSuccessHandler that can be overriden. I was hoping to similarly find a bean named loginSuccessHandler, but no such luck.

    I read the page in the plugin's docs about event handling, but neither of the event handling mechanisms appears to give me access to the current request or session.

    解决方案

    Add a config parameter:

    grails.plugins.springsecurity.successHandler.defaultTargetUrl = '/myLogin/handleSuccessLogin'
    

    Then add your custom login-handling in the action that handles this 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
      }
    }
    

    这篇关于用spring安全处理成功的登录事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆