add 增加 sa-token 用户行为监听
parent
06347cacef
commit
9d6be43103
@ -0,0 +1,118 @@
|
|||||||
|
package com.ruoyi.common.satoken.listener;
|
||||||
|
|
||||||
|
import cn.dev33.satoken.config.SaTokenConfig;
|
||||||
|
import cn.dev33.satoken.listener.SaTokenListener;
|
||||||
|
import cn.dev33.satoken.stp.SaLoginModel;
|
||||||
|
import cn.dev33.satoken.stp.StpUtil;
|
||||||
|
import cn.hutool.http.useragent.UserAgent;
|
||||||
|
import cn.hutool.http.useragent.UserAgentUtil;
|
||||||
|
import com.ruoyi.common.core.constant.CacheConstants;
|
||||||
|
import com.ruoyi.common.core.enums.UserType;
|
||||||
|
import com.ruoyi.common.core.utils.ServletUtils;
|
||||||
|
import com.ruoyi.common.core.utils.ip.AddressUtils;
|
||||||
|
import com.ruoyi.common.redis.utils.RedisUtils;
|
||||||
|
import com.ruoyi.common.satoken.utils.LoginHelper;
|
||||||
|
import com.ruoyi.system.api.domain.SysUserOnline;
|
||||||
|
import com.ruoyi.system.api.model.LoginUser;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 用户行为 侦听器的实现
|
||||||
|
*
|
||||||
|
* @author Lion Li
|
||||||
|
*/
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
@Component
|
||||||
|
@Slf4j
|
||||||
|
public class UserActionListener implements SaTokenListener {
|
||||||
|
|
||||||
|
private final SaTokenConfig tokenConfig;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 每次登录时触发
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void doLogin(String loginType, Object loginId, SaLoginModel loginModel) {
|
||||||
|
UserType userType = UserType.getUserType(loginId.toString());
|
||||||
|
if (userType == UserType.SYS_USER) {
|
||||||
|
UserAgent userAgent = UserAgentUtil.parse(ServletUtils.getRequest().getHeader("User-Agent"));
|
||||||
|
String ip = ServletUtils.getClientIP();
|
||||||
|
LoginUser user = LoginHelper.getLoginUser();
|
||||||
|
String tokenValue = StpUtil.getTokenValueByLoginId(loginId);
|
||||||
|
SysUserOnline dto = new SysUserOnline();
|
||||||
|
dto.setIpaddr(ip);
|
||||||
|
dto.setLoginLocation(AddressUtils.getRealAddressByIP(ip));
|
||||||
|
dto.setBrowser(userAgent.getBrowser().getName());
|
||||||
|
dto.setOs(userAgent.getOs().getName());
|
||||||
|
dto.setLoginTime(System.currentTimeMillis());
|
||||||
|
dto.setTokenId(tokenValue);
|
||||||
|
dto.setUserName(user.getUsername());
|
||||||
|
dto.setDeptName(user.getDeptName());
|
||||||
|
RedisUtils.setCacheObject(CacheConstants.ONLINE_TOKEN_KEY + tokenValue, dto, tokenConfig.getTimeout(), TimeUnit.SECONDS);
|
||||||
|
log.info("user doLogin, userId:{}, token:{}", loginId, tokenValue);
|
||||||
|
} else if (userType == UserType.APP_USER) {
|
||||||
|
// app端 自行根据业务编写
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 每次注销时触发
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void doLogout(String loginType, Object loginId, String tokenValue) {
|
||||||
|
RedisUtils.deleteObject(CacheConstants.ONLINE_TOKEN_KEY + tokenValue);
|
||||||
|
log.info("user doLogout, userId:{}, token:{}", loginId, tokenValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 每次被踢下线时触发
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void doKickout(String loginType, Object loginId, String tokenValue) {
|
||||||
|
RedisUtils.deleteObject(CacheConstants.ONLINE_TOKEN_KEY + tokenValue);
|
||||||
|
log.info("user doLogoutByLoginId, userId:{}, token:{}", loginId, tokenValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 每次被顶下线时触发
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void doReplaced(String loginType, Object loginId, String tokenValue) {
|
||||||
|
RedisUtils.deleteObject(CacheConstants.ONLINE_TOKEN_KEY + tokenValue);
|
||||||
|
log.info("user doReplaced, userId:{}, token:{}", loginId, tokenValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 每次被封禁时触发
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void doDisable(String loginType, Object loginId, long disableTime) {
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 每次被解封时触发
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void doUntieDisable(String loginType, Object loginId) {
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 每次创建Session时触发
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void doCreateSession(String id) {
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 每次注销Session时触发
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void doLogoutSession(String id) {
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -1,4 +1,5 @@
|
|||||||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
||||||
com.ruoyi.common.satoken.core.dao.PlusSaTokenDao,\
|
com.ruoyi.common.satoken.core.dao.PlusSaTokenDao,\
|
||||||
com.ruoyi.common.satoken.core.service.SaInterfaceImpl,\
|
com.ruoyi.common.satoken.core.service.SaInterfaceImpl,\
|
||||||
com.ruoyi.common.satoken.config.SaTokenConfiguration
|
com.ruoyi.common.satoken.config.SaTokenConfiguration,\
|
||||||
|
com.ruoyi.common.satoken.listener.UserActionListener
|
||||||
|
Loading…
Reference in New Issue