初始化项目

This commit is contained in:
zhangzhiqiang
2023-03-14 15:32:10 +08:00
parent 8139df6099
commit 163721bdc9

View File

@@ -13,14 +13,29 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.nl.modules.security.service; 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.StrUtil; import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.nl.modules.security.config.bean.SecurityProperties; import org.nl.modules.common.config.RsaProperties;
import org.nl.modules.security.service.dto.JwtUserDto; import org.nl.modules.common.exception.BadRequestException;
import org.nl.modules.security.service.dto.OnlineUserDto; import org.nl.modules.common.utils.*;
import org.nl.utils.*; import org.nl.system.service.role.ISysRoleService;
import org.nl.system.service.secutiry.dto.AuthUserDto;
import org.nl.system.service.user.ISysUserService;
import org.nl.system.service.user.dao.SysUser;
import org.nl.system.service.user.dto.CurrentUser;
import org.nl.system.service.user.dto.OnlineUserDto;
import org.nl.system.service.user.dto.UserDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Pageable;
import org.springframework.scheduling.annotation.Async; import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@@ -38,32 +53,38 @@ import java.util.*;
@Slf4j @Slf4j
public class OnlineUserService { public class OnlineUserService {
private final SecurityProperties properties; @Autowired
private ISysUserService sysUserService;
@Autowired
private ISysRoleService roleService;
private final RedisUtils redisUtils; private final RedisUtils redisUtils;
@Value("${sa-token.cookie.domain}")
private String domain;
public OnlineUserService(SecurityProperties properties, RedisUtils redisUtils) { public OnlineUserService(RedisUtils redisUtils) {
this.properties = properties;
this.redisUtils = redisUtils; this.redisUtils = redisUtils;
} }
/** /**
* 保存在线用户信息 * 保存在线用户信息
* @param jwtUserDto / * @param userDto /
* @param token / * @param token /
* @param request / * @param request /
*/ */
public void save(JwtUserDto jwtUserDto, String token, HttpServletRequest request){ public void save(UserDto userDto, String token, HttpServletRequest request){
String dept = jwtUserDto.getUser().getDept().getName(); // String dept = userDto.getDept().getName();
String dept = "";
String ip = StringUtils.getIp(request); String ip = StringUtils.getIp(request);
String browser = StringUtils.getBrowser(request); String browser = StringUtils.getBrowser(request);
String address = StringUtils.getCityInfo(ip); // String address = StringUtils.getCityInfo(ip);
String address = "局域网";
OnlineUserDto onlineUserDto = null; OnlineUserDto onlineUserDto = null;
try { try {
onlineUserDto = new OnlineUserDto(jwtUserDto.getUsername(), jwtUserDto.getUser().getNickName(), dept, browser , ip, address, EncryptUtils.desEncrypt(token), new Date()); // onlineUserDto = new OnlineUserDto(userDto.getUsername(), userDto.getNickName(), dept, browser , ip, address, EncryptUtils.desEncrypt(token), new Date());
} catch (Exception e) { } catch (Exception e) {
log.error(e.getMessage(),e); log.error(e.getMessage(),e);
} }
redisUtils.set(properties.getOnlineKey() + token, onlineUserDto, properties.getTokenValidityInSeconds()/1000); redisUtils.set(token, onlineUserDto, StpUtil.getTokenTimeout());
} }
/** /**
@@ -86,18 +107,21 @@ public class OnlineUserService {
* @return / * @return /
*/ */
public List<OnlineUserDto> getAll(String filter){ public List<OnlineUserDto> getAll(String filter){
List<String> keys = redisUtils.scan(properties.getOnlineKey() + "*"); List<String> keys = redisUtils.scan("*");
Collections.reverse(keys); Collections.reverse(keys);
List<OnlineUserDto> onlineUserDtos = new ArrayList<>(); List<OnlineUserDto> onlineUserDtos = new ArrayList<>();
for (String key : keys) { for (String key : keys) {
OnlineUserDto onlineUserDto = (OnlineUserDto) redisUtils.get(key); if (key.length() == 1511) {
if(StrUtil.isNotEmpty(filter)){ OnlineUserDto onlineUserDto = (OnlineUserDto) redisUtils.get(key);
if(onlineUserDto.toString().contains(filter)){ if(StrUtil.isNotEmpty(filter)){
if(onlineUserDto.toString().contains(filter)){
onlineUserDtos.add(onlineUserDto);
}
} else {
onlineUserDtos.add(onlineUserDto); onlineUserDtos.add(onlineUserDto);
} }
} else {
onlineUserDtos.add(onlineUserDto);
} }
} }
onlineUserDtos.sort((o1, o2) -> o2.getLoginTime().compareTo(o1.getLoginTime())); onlineUserDtos.sort((o1, o2) -> o2.getLoginTime().compareTo(o1.getLoginTime()));
return onlineUserDtos; return onlineUserDtos;
@@ -108,7 +132,6 @@ public class OnlineUserService {
* @param key / * @param key /
*/ */
public void kickOut(String key){ public void kickOut(String key){
key = properties.getOnlineKey() + key;
redisUtils.del(key); redisUtils.del(key);
} }
@@ -117,8 +140,7 @@ public class OnlineUserService {
* @param token / * @param token /
*/ */
public void logout(String token) { public void logout(String token) {
String key = properties.getOnlineKey() + token; redisUtils.del(token);
redisUtils.del(key);
} }
/** /**
@@ -190,4 +212,58 @@ public class OnlineUserService {
} }
} }
} }
@SneakyThrows
public Map<String, Object> login(Map paramMap){
// 密码解密 - 前端的加密规则: encrypt
AuthUserDto authUser = JSON.toJavaObject((JSON) JSON.toJSON(paramMap), AuthUserDto.class);
String password = RsaUtils.decryptByPrivateKey(RsaProperties.privateKey, authUser.getPassword());
// 查询验证码
String code = (String) redisUtils.get(authUser.getUuid());
// 清除验证码
redisUtils.del(authUser.getUuid());
// if (StrUtil.isEmpty(code)) {
// throw new BadRequestException("验证码不存在或已过期");
// }
// if (StrUtil.isEmpty(authUser.getCode()) || !authUser.getCode().equalsIgnoreCase(code)) {
// throw new BadRequestException("验证码错误");
// }
// 校验数据库
// 根据用户名查询在比对密码
SysUser userInfo = sysUserService.getOne(new QueryWrapper<SysUser>().eq("username",authUser.getUsername()));
if (userInfo == null||!userInfo.getPassword().equals(SaSecureUtil.md5BySalt(password, "salt"))) { // 这里需要密码加密
throw new BadRequestException("账号或密码错误");
}
// 获取权限列表 - 登录查找权限
List<String> permissionList = roleService.getPermissionList((JSONObject) JSON.toJSON(userInfo));
if (!userInfo.getIsUsed()) {
throw new BadRequestException("账号未激活");
}
// 登录输入登出删除
CurrentUser user = new CurrentUser();
user.setId(userInfo.getUserId());
user.setUsername(userInfo.getUsername());
user.setPresonName((userInfo.getPersonName()));
user.setUser(userInfo);
user.setPermissions(permissionList);
// SaLoginModel 配置登录相关参数
StpUtil.login(userInfo.getUserId(), new SaLoginModel()
.setDevice("PC") // 此次登录的客户端设备类型, 用于[同端互斥登录]时指定此次登录的设备类型
.setExtra("loginInfo", user) // Token挂载的扩展参数 此方法只有在集成jwt插件时才会生效
);
Map<String, Object> authInfo = new HashMap<String, Object>(2) {{
put("token", StpUtil.getTokenValue());
put("roles", permissionList);
put("domain", domain);
put("user", user);
}};
// 保存在线信息
// onlineUserService.save(userDto, StpUtil.getTokenValue(), request);
return authInfo;
}
} }