|
|
@ -18,9 +18,11 @@ package org.nl.system.service.secutiry.impl; |
|
|
|
import cn.dev33.satoken.secure.SaSecureUtil; |
|
|
|
import cn.dev33.satoken.stp.SaLoginModel; |
|
|
|
import cn.dev33.satoken.stp.StpUtil; |
|
|
|
import cn.hutool.core.util.ObjectUtil; |
|
|
|
import cn.hutool.core.util.StrUtil; |
|
|
|
import com.alibaba.fastjson.JSON; |
|
|
|
import com.alibaba.fastjson.JSONObject; |
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
|
|
|
import lombok.SneakyThrows; |
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
@ -28,6 +30,8 @@ import org.nl.common.utils.*; |
|
|
|
import org.nl.config.RsaProperties; |
|
|
|
import org.nl.common.exception.BadRequestException; |
|
|
|
import org.nl.common.utils.dto.CurrentUser; |
|
|
|
import org.nl.system.service.dept.ISysDeptService; |
|
|
|
import org.nl.system.service.dept.dao.SysDept; |
|
|
|
import org.nl.system.service.secutiry.dto.UserDto; |
|
|
|
import org.nl.system.service.role.ISysRoleService; |
|
|
|
import org.nl.system.service.secutiry.dto.AuthUserDto; |
|
|
@ -55,6 +59,8 @@ public class OnlineUserService { |
|
|
|
@Autowired |
|
|
|
private ISysUserService sysUserService; |
|
|
|
@Autowired |
|
|
|
private ISysDeptService deptService; |
|
|
|
@Autowired |
|
|
|
private ISysRoleService roleService; |
|
|
|
@Autowired |
|
|
|
private RedisUtils redisUtils; |
|
|
@ -69,20 +75,27 @@ public class OnlineUserService { |
|
|
|
* @param token / |
|
|
|
* @param request / |
|
|
|
*/ |
|
|
|
public void save(UserDto userDto, String token, HttpServletRequest request){ |
|
|
|
// String dept = userDto.getDept().getName();
|
|
|
|
String dept = ""; |
|
|
|
public void save(SysUser userDto, String token, HttpServletRequest request){ |
|
|
|
// 获取用户部门
|
|
|
|
List<SysDept> userDeptByUserId = deptService.getUserDeptByUserId(userDto.getUser_id()); |
|
|
|
StringBuilder sb = new StringBuilder(); |
|
|
|
for (SysDept dept : userDeptByUserId) { |
|
|
|
sb.append(dept.getName()).append("、"); |
|
|
|
} |
|
|
|
if (sb.length() > 0) { |
|
|
|
sb.setLength(sb.length() - 1); |
|
|
|
} |
|
|
|
String dept = sb.toString(); |
|
|
|
String ip = StringUtils.getIp(request); |
|
|
|
String browser = StringUtils.getBrowser(request); |
|
|
|
// String address = StringUtils.getCityInfo(ip);
|
|
|
|
String address = "局域网"; |
|
|
|
String address = StringUtils.getCityInfo(ip); |
|
|
|
OnlineUserDto onlineUserDto = null; |
|
|
|
try { |
|
|
|
// onlineUserDto = new OnlineUserDto(userDto.getUsername(), userDto.getNickName(), dept, browser , ip, address, EncryptUtils.desEncrypt(token), new Date());
|
|
|
|
onlineUserDto = new OnlineUserDto(userDto.getUsername(), userDto.getPerson_name(), dept, browser , ip, address, EncryptUtils.desEncrypt(token), new Date()); |
|
|
|
} catch (Exception e) { |
|
|
|
log.error(e.getMessage(),e); |
|
|
|
} |
|
|
|
redisUtils.set(token, onlineUserDto, StpUtil.getTokenTimeout()); |
|
|
|
redisUtils.set("oline-" + userDto.getUsername(), onlineUserDto, StpUtil.getTokenTimeout()); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
@ -109,7 +122,7 @@ public class OnlineUserService { |
|
|
|
Collections.reverse(keys); |
|
|
|
List<OnlineUserDto> onlineUserDtos = new ArrayList<>(); |
|
|
|
for (String key : keys) { |
|
|
|
if (key.length() == 1511) { |
|
|
|
if (key.startsWith("oline-")) { |
|
|
|
OnlineUserDto onlineUserDto = (OnlineUserDto) redisUtils.get(key); |
|
|
|
if(StrUtil.isNotEmpty(filter)){ |
|
|
|
if(onlineUserDto.toString().contains(filter)){ |
|
|
@ -127,10 +140,26 @@ public class OnlineUserService { |
|
|
|
|
|
|
|
/** |
|
|
|
* 踢出用户 |
|
|
|
* @param key / |
|
|
|
* @param key: OnlineUserDto / |
|
|
|
*/ |
|
|
|
public void kickOut(OnlineUserDto key) { |
|
|
|
// 获取用户
|
|
|
|
SysUser one = sysUserService.getOne(new LambdaQueryWrapper<SysUser>().eq(SysUser::getUsername, key.getUserName())); |
|
|
|
if (ObjectUtil.isNotEmpty(one)) { |
|
|
|
redisUtils.del("oline-" + one.getUsername()); |
|
|
|
} |
|
|
|
// 下线
|
|
|
|
StpUtil.logoutByTokenValue(key.getKey()); // 通过token强退
|
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 踢出用户 |
|
|
|
* @param key:token / |
|
|
|
*/ |
|
|
|
public void kickOut(String key){ |
|
|
|
public void kickOut(String key) { |
|
|
|
redisUtils.del(key); |
|
|
|
// 下线
|
|
|
|
StpUtil.logoutByTokenValue(key); // 通过token强退
|
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
@ -211,7 +240,7 @@ public class OnlineUserService { |
|
|
|
} |
|
|
|
} |
|
|
|
@SneakyThrows |
|
|
|
public Map<String, Object> login(Map paramMap){ |
|
|
|
public Map<String, Object> login(Map paramMap, HttpServletRequest request){ |
|
|
|
// 密码解密 - 前端的加密规则: encrypt
|
|
|
|
AuthUserDto authUser = JSON.toJavaObject((JSON) JSON.toJSON(paramMap), AuthUserDto.class); |
|
|
|
String password = RsaUtils.decryptByPrivateKey(RsaProperties.privateKey, authUser.getPassword()); |
|
|
@ -263,7 +292,7 @@ public class OnlineUserService { |
|
|
|
put("user", user); |
|
|
|
}}; |
|
|
|
// 保存在线信息
|
|
|
|
// onlineUserService.save(userDto, StpUtil.getTokenValue(), request);
|
|
|
|
this.save(userInfo, StpUtil.getTokenValue(), request); |
|
|
|
return authInfo; |
|
|
|
} |
|
|
|
} |
|
|
|