Grails中如何监听用户是否登录

grails登录监听

有朋友问我,在grails中如何监听用户已经登录呢?我也不知道,所以写下此文章以帮助哪些需要帮助的人。

1、在项目src/main/groovy下面新建一个用户登录监听,在onApplicationEvent方法中可以执行操作

package com.listener

import org.springframework.context.ApplicationListener
import org.springframework.security.authentication.event.AuthenticationSuccessEvent
import org.springframework.security.authentication.event.InteractiveAuthenticationSuccessEvent
import org.springframework.security.core.userdetails.UserDetails

/**
* 登录监听
* @auther Lee
* @Date 2017/11/30 11:42
* return
*
*/
class LoginEventListener implements ApplicationListener<InteractiveAuthenticationSuccessEvent> {

/**
* Handle an application event.
* @param event the event to respond to
*/
@Override
void onApplicationEvent(InteractiveAuthenticationSuccessEvent event) {
println "登录成功..."
//获取当前登录用户
UserDetails userDetails = (UserDetails) event.getAuthentication().getPrincipal()
println userDetails.username

}
}

2、在grails-app/conf/resources.groovy注入DSL

// Place your Spring DSL code here
beans = {

loginEventListener(com.listener.LoginEventListener)
}

当你登录成功时

attachments-2017-11-7W0fdCXj5a1f82364611e.png


  • 发表于 2017-11-30 12:00
  • 阅读 ( 1732 )
  • 分类:grails

0 条评论

请先 登录 后评论
不写代码的码农
Jonny

程序猿

65 篇文章

作家榜 »

  1. 威猛的小站长 124 文章
  2. Jonny 65 文章
  3. 江南烟雨 36 文章
  4. - Nightmare 33 文章
  5. doublechina 31 文章
  6. HJ社区-肖峰 29 文章
  7. 伪摄影 22 文章
  8. Alan 14 文章