有朋友问我,在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)
}
当你登录成功时
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!