# 事件机制
Heimdall 框架实现了完善的事件通知机制,在发生关键事件后,执行事件回调,将执行结果通知使用者,以便后续处理。
目前框架主要实现了如下两种事件的监听回调:
- Session 事件
- 认证 事件
# Session 事件
定义如下:
/**
* Session事件监听
*
* @author luter
*/
public interface SessionEventListener {
/**
* Session 创建
*
* @param session the session
*/
default void afterCreated(SimpleSession session) {
}
/**
* Session 读取
*
* @param session the session
*/
default void afterRead(SimpleSession session) {
}
/**
* Session 更新
*
* @param session the session
*/
default void afterUpdated(SimpleSession session) {
}
/**
* Session 删除
*
* @param session the session
*/
default void afterDeleted(SimpleSession session) {
}
/**
* 过期 Session 清理
*/
default void afterSessionValidScheduled() {
}
}
# 使用:
- 1 实现自定义事件监听
/**
* Session 事件监听
*
* @author Luter
*/
@Slf4j
public class CustomSessionEventListener implements com.luter.heimdall.core.session.listener.SessionEventListener {
@Override
public void afterCreated(SimpleSession session) {
log.info("Session 创建,Session:{}", session);
}
@Override
public void afterRead(SimpleSession session) {
log.debug("Session 读取,Session:{}", session);
}
@Override
public void afterUpdated(SimpleSession session) {
log.debug("Session 更新,Session:{}", session);
}
@Override
public void afterDeleted(SimpleSession session) {
log.info("Session 删除,Session:{}", session);
}
@Override
public void afterSessionValidScheduled() {
log.debug("Session 有效性定时验证完毕");
}
}
- 2 注册自定义监听 在 SessionDao 中注册监听器,如下所示:
@Bean
public SessionDAO sessionDAO(CookieService cookieService, ServletHolder servletHolder) {
log.warn("初始化 SessionDAO");
final CaffeineSessionDaoImpl sessionDao = new CaffeineSessionDaoImpl(servletHolder, cookieService);
// 注册 监听器,可以同时注册多个
List<SessionEventListener> listeners = new ArrayList<>();
listeners.add(new CustomSessionEventListener());
sessionDao.setListeners(listeners);
return sessionDao;
}
# 认证事件
定义如下:
/**
* 认证事件监听
* <p>
* 可以实现此接口,监听 登录、注销、踢出等事件,记录日志.
*
* @author Luter
*/
public interface AuthenticationEventListener {
/**
* 认证登录成功
*
* @param code 0: 重复登录,新来的被拒绝登录
* 1: 重复登录,上一个登录 Session 被踢出
* 2:登录成功,创建了新 Session
* @param session the session
*/
default void onLogin(int code, SimpleSession session) {
}
/**
* 注销认证成功
*
* @param session the session
*/
default void onLogout(SimpleSession session) {
}
/**
* Session 被成功踢出
*
* @param session the session
*/
default void onSessionKickOut(SimpleSession session) {
}
/**
* principal 被成功踢出
*
* @param principal the principal
* @param session the session
*/
default void onPrincipalKickOut(String principal, SimpleSession session) {
}
}
# 使用
- 自定义事件监听器
/**
* 自定义认证事件监听器
*
* @author Luter
*/
@Slf4j
public class CustomAuthenticationListener implements AuthenticationEventListener {
@Override
public void onLogin(int code, SimpleSession session) {
switch (code) {
case 0:
log.info("重复登录,新登录被拒绝:Session:{}", session);
break;
case 1:
log.info("重复登录,已登录的被踢出:Session:{}", session);
break;
case 2:
log.info("系统登录:Session:{}", session);
break;
default:
break;
}
}
@Override
public void onLogout(SimpleSession session) {
log.info("注销登录,Session:{}", session);
}
@Override
public void onSessionKickOut(SimpleSession session) {
log.info("session 被踢出,Session:{}", session);
}
@Override
public void onPrincipalKickOut(String principal, SimpleSession session) {
log.info("principal 被踢出,principal:{},Session:{}", principal, session);
}
}
- 注册自定义认证监听器
在认证管理器 Bean 注册的时候,注册自定义认证监听器,如下所示:
@Bean
public AuthenticationManager authenticationManager(SessionDAO sessionDAO) {
log.warn("初始化 认证管理器");
final AuthenticationManager authenticationManager = new AuthenticationManager(sessionDAO);
// 注册 自定义认证监听器,支持注册多个
List<AuthenticationEventListener> authenticationListeners = new ArrayList<>();
authenticationListeners.add(new CustomAuthenticationListener());
authenticationManager.setListeners(authenticationListeners);
return authenticationManager;
}