2
0

feat: 登录认证的类型分为sys/u,对应系统和用户平台

This commit is contained in:
TsMask
2024-11-27 15:23:49 +08:00
parent b7e8da2091
commit 5b8c1b1e94
25 changed files with 262 additions and 79 deletions

View File

@@ -11,6 +11,7 @@
<modules> <modules>
<module>wfc-api-system</module> <module>wfc-api-system</module>
<module>wfc-api-omada</module> <module>wfc-api-omada</module>
<module>wfc-api-user</module>
</modules> </modules>
<artifactId>wfc-api</artifactId> <artifactId>wfc-api</artifactId>

View File

@@ -57,6 +57,11 @@
<artifactId>wfc-common-swagger</artifactId> <artifactId>wfc-common-swagger</artifactId>
</dependency> </dependency>
<!-- WFC Api User -->
<dependency>
<groupId>org.wfc</groupId>
<artifactId>wfc-api-user</artifactId>
</dependency>
</dependencies> </dependencies>
<build> <build>

View File

@@ -1,21 +1,25 @@
package org.wfc.auth.controller; package org.wfc.auth.controller;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import org.wfc.auth.form.LoginBody; import org.wfc.common.core.web.form.LoginBody;
import org.wfc.auth.form.RegisterBody; import org.wfc.common.core.web.form.RegisterBody;
import org.wfc.auth.service.SysLoginService; import org.wfc.auth.service.SysLoginService;
import org.wfc.auth.service.ULoginService;
import org.wfc.common.core.domain.R; import org.wfc.common.core.domain.R;
import org.wfc.common.core.utils.JwtUtils; import org.wfc.common.core.utils.JwtUtils;
import org.wfc.common.core.utils.StringUtils; import org.wfc.common.core.utils.StringUtils;
import org.wfc.common.security.auth.AuthUtil; import org.wfc.common.security.auth.AuthUtil;
import org.wfc.common.security.service.TokenService; import org.wfc.common.security.service.TokenService;
import org.wfc.common.security.utils.SecurityUtils; import org.wfc.common.security.utils.SecurityUtils;
import org.wfc.system.api.model.LoginUser; import org.wfc.common.core.domain.LoginUser;
import org.wfc.system.api.domain.SysUser;
import org.wfc.user.api.domain.UUser;
import javax.servlet.http.HttpServletRequest;
/** /**
* token 控制 * token 控制
@@ -31,13 +35,25 @@ public class TokenController
@Autowired @Autowired
private SysLoginService sysLoginService; private SysLoginService sysLoginService;
@Autowired
private ULoginService uLoginService;
@PostMapping("login") @PostMapping("login")
public R<?> login(@RequestBody LoginBody form) public R<?> login(@RequestBody LoginBody form)
{ {
if ("u".equals(form.getAuthType())){
// 用户登录 // 用户登录
LoginUser userInfo = sysLoginService.login(form.getUsername(), form.getPassword()); LoginUser<UUser> userInfo = uLoginService.login(form.getUsername(), form.getPassword());
// 获取登录token // 获取登录token
return R.ok(tokenService.createToken(userInfo)); return R.ok(tokenService.createToken(userInfo));
} if ("sys".equals(form.getAuthType())){
// 用户登录
LoginUser<SysUser> sysInfo = sysLoginService.login(form.getUsername(), form.getPassword());
// 获取登录token
return R.ok(tokenService.createToken(sysInfo));
}
return R.fail("authentication type not supported");
} }
@DeleteMapping("logout") @DeleteMapping("logout")

View File

@@ -17,7 +17,8 @@ import org.wfc.common.redis.service.RedisService;
import org.wfc.common.security.utils.SecurityUtils; import org.wfc.common.security.utils.SecurityUtils;
import org.wfc.system.api.RemoteUserService; import org.wfc.system.api.RemoteUserService;
import org.wfc.system.api.domain.SysUser; import org.wfc.system.api.domain.SysUser;
import org.wfc.system.api.model.LoginUser; import org.wfc.common.core.domain.LoginUser;
import org.wfc.user.api.domain.UUser;
/** /**
* 登录校验方法 * 登录校验方法
@@ -42,7 +43,7 @@ public class SysLoginService
/** /**
* 登录 * 登录
*/ */
public LoginUser login(String username, String password) public LoginUser<SysUser> login(String username, String password)
{ {
// 用户名或密码为空 错误 // 用户名或密码为空 错误
if (StringUtils.isAnyBlank(username, password)) if (StringUtils.isAnyBlank(username, password))
@@ -72,7 +73,7 @@ public class SysLoginService
throw new ServiceException("很遗憾访问IP已被列入系统黑名单"); throw new ServiceException("很遗憾访问IP已被列入系统黑名单");
} }
// 查询用户信息 // 查询用户信息
R<LoginUser> userResult = remoteUserService.getUserInfo(username, SecurityConstants.INNER); R<LoginUser<SysUser>> userResult = remoteUserService.getUserInfo(username, SecurityConstants.INNER);
if (StringUtils.isNull(userResult) || StringUtils.isNull(userResult.getData())) if (StringUtils.isNull(userResult) || StringUtils.isNull(userResult.getData()))
{ {
@@ -85,8 +86,8 @@ public class SysLoginService
throw new ServiceException(userResult.getMsg()); throw new ServiceException(userResult.getMsg());
} }
LoginUser userInfo = userResult.getData(); LoginUser<SysUser> userInfo = userResult.getData();
SysUser user = userResult.getData().getSysUser(); SysUser user = userInfo.getUser();
if (UserStatus.DELETED.getCode().equals(user.getDelFlag())) if (UserStatus.DELETED.getCode().equals(user.getDelFlag()))
{ {
recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "对不起,您的账号已被删除"); recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "对不起,您的账号已被删除");

View File

@@ -15,9 +15,9 @@ import org.wfc.common.core.utils.StringUtils;
import org.wfc.common.core.utils.ip.IpUtils; import org.wfc.common.core.utils.ip.IpUtils;
import org.wfc.common.redis.service.RedisService; import org.wfc.common.redis.service.RedisService;
import org.wfc.common.security.utils.SecurityUtils; import org.wfc.common.security.utils.SecurityUtils;
import org.wfc.system.api.RemoteUserService; import org.wfc.common.core.domain.LoginUser;
import org.wfc.system.api.domain.SysUser; import org.wfc.user.api.RemoteUUserService;
import org.wfc.system.api.model.LoginUser; import org.wfc.user.api.domain.UUser;
/** /**
* 用户平台-登录校验方法 * 用户平台-登录校验方法
@@ -28,13 +28,13 @@ import org.wfc.system.api.model.LoginUser;
public class ULoginService public class ULoginService
{ {
@Autowired @Autowired
private RemoteUserService remoteUserService; private RemoteUUserService remoteUserService;
@Autowired @Autowired
private SysPasswordService passwordService; private UPasswordService passwordService;
@Autowired @Autowired
private SysRecordLogService recordLogService; private URecordLogService recordLogService;
@Autowired @Autowired
private RedisService redisService; private RedisService redisService;
@@ -42,7 +42,7 @@ public class ULoginService
/** /**
* 登录 * 登录
*/ */
public LoginUser login(String username, String password) public LoginUser<UUser> login(String username, String password)
{ {
// 用户名或密码为空 错误 // 用户名或密码为空 错误
if (StringUtils.isAnyBlank(username, password)) if (StringUtils.isAnyBlank(username, password))
@@ -72,7 +72,7 @@ public class ULoginService
throw new ServiceException("很遗憾访问IP已被列入系统黑名单"); throw new ServiceException("很遗憾访问IP已被列入系统黑名单");
} }
// 查询用户信息 // 查询用户信息
R<LoginUser> userResult = remoteUserService.getUserInfo(username, SecurityConstants.INNER); R<LoginUser<UUser>> userResult = remoteUserService.getUserInfo(username, SecurityConstants.INNER);
if (StringUtils.isNull(userResult) || StringUtils.isNull(userResult.getData())) if (StringUtils.isNull(userResult) || StringUtils.isNull(userResult.getData()))
{ {
@@ -85,8 +85,8 @@ public class ULoginService
throw new ServiceException(userResult.getMsg()); throw new ServiceException(userResult.getMsg());
} }
LoginUser userInfo = userResult.getData(); LoginUser<UUser> userInfo = userResult.getData();
SysUser user = userResult.getData().getSysUser(); UUser user = userInfo.getUser();
if (UserStatus.DELETED.getCode().equals(user.getDelFlag())) if (UserStatus.DELETED.getCode().equals(user.getDelFlag()))
{ {
recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "对不起,您的账号已被删除"); recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "对不起,您的账号已被删除");
@@ -110,7 +110,7 @@ public class ULoginService
*/ */
public void recordLoginInfo(Long userId) public void recordLoginInfo(Long userId)
{ {
SysUser sysUser = new SysUser(); UUser sysUser = new UUser();
sysUser.setUserId(userId); sysUser.setUserId(userId);
// 更新用户登录IP // 更新用户登录IP
sysUser.setLoginIp(IpUtils.getIpAddr()); sysUser.setLoginIp(IpUtils.getIpAddr());
@@ -146,7 +146,7 @@ public class ULoginService
} }
// 注册用户信息 // 注册用户信息
SysUser sysUser = new SysUser(); UUser sysUser = new UUser();
sysUser.setUserName(username); sysUser.setUserName(username);
sysUser.setNickName(username); sysUser.setNickName(username);
sysUser.setPassword(SecurityUtils.encryptPassword(password)); sysUser.setPassword(SecurityUtils.encryptPassword(password));

View File

@@ -0,0 +1,87 @@
package org.wfc.auth.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.wfc.common.core.constant.CacheConstants;
import org.wfc.common.core.constant.Constants;
import org.wfc.common.core.exception.ServiceException;
import org.wfc.common.core.exception.user.UserPasswordNotMatchException;
import org.wfc.common.redis.service.RedisService;
import org.wfc.common.security.utils.SecurityUtils;
import org.wfc.user.api.domain.UUser;
import java.util.concurrent.TimeUnit;
/**
* 登录密码方法
*
* @author wfc
*/
@Component
public class UPasswordService
{
@Autowired
private RedisService redisService;
private int maxRetryCount = CacheConstants.PASSWORD_MAX_RETRY_COUNT;
private Long lockTime = CacheConstants.PASSWORD_LOCK_TIME;
@Autowired
private SysRecordLogService recordLogService;
/**
* 登录账户密码错误次数缓存键名
*
* @param username 用户名
* @return 缓存键key
*/
private String getCacheKey(String username)
{
return CacheConstants.PWD_ERR_CNT_KEY + username;
}
public void validate(UUser user, String password)
{
String username = user.getUserName();
Integer retryCount = redisService.getCacheObject(getCacheKey(username));
if (retryCount == null)
{
retryCount = 0;
}
if (retryCount >= Integer.valueOf(maxRetryCount).intValue())
{
String errMsg = String.format("密码输入错误%s次帐户锁定%s分钟", maxRetryCount, lockTime);
recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL,errMsg);
throw new ServiceException(errMsg);
}
if (!matches(user, password))
{
retryCount = retryCount + 1;
recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, String.format("密码输入错误%s次", retryCount));
redisService.setCacheObject(getCacheKey(username), retryCount, lockTime, TimeUnit.MINUTES);
throw new UserPasswordNotMatchException();
}
else
{
clearLoginRecordCache(username);
}
}
public boolean matches(UUser user, String rawPassword)
{
return SecurityUtils.matchesPassword(rawPassword, user.getPassword());
}
public void clearLoginRecordCache(String loginName)
{
if (redisService.hasKey(getCacheKey(loginName)))
{
redisService.deleteObject(getCacheKey(loginName));
}
}
}

View File

@@ -0,0 +1,48 @@
package org.wfc.auth.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.wfc.common.core.constant.Constants;
import org.wfc.common.core.constant.SecurityConstants;
import org.wfc.common.core.utils.StringUtils;
import org.wfc.common.core.utils.ip.IpUtils;
import org.wfc.user.api.RemoteULogService;
import org.wfc.user.api.domain.ULogininfor;
/**
* 记录日志方法
*
* @author wfc
*/
@Component
public class URecordLogService
{
@Autowired
private RemoteULogService remoteLogService;
/**
* 记录登录信息
*
* @param username 用户名
* @param status 状态
* @param message 消息内容
* @return
*/
public void recordLogininfor(String username, String status, String message)
{
ULogininfor logininfor = new ULogininfor();
logininfor.setUserName(username);
logininfor.setIpaddr(IpUtils.getIpAddr());
logininfor.setMsg(message);
// 日志状态
if (StringUtils.equalsAny(status, Constants.LOGIN_SUCCESS, Constants.LOGOUT, Constants.REGISTER))
{
logininfor.setStatus(Constants.LOGIN_SUCCESS_STATUS);
}
else if (Constants.LOGIN_FAIL.equals(status))
{
logininfor.setStatus(Constants.LOGIN_FAIL_STATUS);
}
remoteLogService.saveLogininfor(logininfor, SecurityConstants.INNER);
}
}

View File

@@ -21,4 +21,10 @@ public class ServiceNameConstants
* 文件服务的serviceid * 文件服务的serviceid
*/ */
public static final String FILE_SERVICE = "wfc-file"; public static final String FILE_SERVICE = "wfc-file";
/**
* 用户平台模块的 service_id
*/
public static final String USER_SERVICE = "wfc-modules-user";
} }

View File

@@ -1,15 +1,13 @@
package org.wfc.system.api.model; package org.wfc.common.core.domain;
import java.io.Serializable; import java.io.Serializable;
import java.util.Set; import java.util.Set;
import org.wfc.system.api.domain.SysUser;
/** /**
* 用户信息 * 用户信息
* *
* @author wfc * @author wfc
*/ */
public class LoginUser implements Serializable public class LoginUser<T> implements Serializable
{ {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
@@ -56,7 +54,7 @@ public class LoginUser implements Serializable
/** /**
* 用户信息 * 用户信息
*/ */
private SysUser sysUser; private T user;
public String getToken() public String getToken()
{ {
@@ -138,13 +136,13 @@ public class LoginUser implements Serializable
this.roles = roles; this.roles = roles;
} }
public SysUser getSysUser() public T getUser()
{ {
return sysUser; return user;
} }
public void setSysUser(SysUser sysUser) public void setUser(T user)
{ {
this.sysUser = sysUser; this.user = user;
} }
} }

View File

@@ -1,4 +1,4 @@
package org.wfc.auth.form; package org.wfc.common.core.web.form;
/** /**
* 用户登录对象 * 用户登录对象

View File

@@ -1,4 +1,4 @@
package org.wfc.auth.form; package org.wfc.common.core.web.form;
/** /**
* 用户注册对象 * 用户注册对象

View File

@@ -8,6 +8,7 @@ import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.wfc.common.core.constant.UserConstants; import org.wfc.common.core.constant.UserConstants;
import org.wfc.common.core.context.SecurityContextHolder; import org.wfc.common.core.context.SecurityContextHolder;
import org.wfc.common.core.domain.LoginUser;
import org.wfc.common.core.text.Convert; import org.wfc.common.core.text.Convert;
import org.wfc.common.core.utils.StringUtils; import org.wfc.common.core.utils.StringUtils;
import org.wfc.common.core.web.domain.BaseEntity; import org.wfc.common.core.web.domain.BaseEntity;
@@ -15,7 +16,6 @@ import org.wfc.common.datascope.annotation.DataScope;
import org.wfc.common.security.utils.SecurityUtils; import org.wfc.common.security.utils.SecurityUtils;
import org.wfc.system.api.domain.SysRole; import org.wfc.system.api.domain.SysRole;
import org.wfc.system.api.domain.SysUser; import org.wfc.system.api.domain.SysUser;
import org.wfc.system.api.model.LoginUser;
/** /**
* 数据过滤处理 * 数据过滤处理
@@ -66,10 +66,10 @@ public class DataScopeAspect
protected void handleDataScope(final JoinPoint joinPoint, DataScope controllerDataScope) protected void handleDataScope(final JoinPoint joinPoint, DataScope controllerDataScope)
{ {
// 获取当前的用户 // 获取当前的用户
LoginUser loginUser = SecurityUtils.getLoginUser(); LoginUser<SysUser> loginUser = SecurityUtils.getLoginUser();
if (StringUtils.isNotNull(loginUser)) if (StringUtils.isNotNull(loginUser))
{ {
SysUser currentUser = loginUser.getSysUser(); SysUser currentUser = loginUser.getUser();
// 如果是超级管理员,则不过滤数据 // 如果是超级管理员,则不过滤数据
if (StringUtils.isNotNull(currentUser) && !currentUser.isAdmin()) if (StringUtils.isNotNull(currentUser) && !currentUser.isAdmin())
{ {

View File

@@ -28,12 +28,17 @@
<artifactId>wfc-api-system</artifactId> <artifactId>wfc-api-system</artifactId>
</dependency> </dependency>
<!-- WFC Api User -->
<dependency>
<groupId>org.wfc</groupId>
<artifactId>wfc-api-user</artifactId>
</dependency>
<!-- WFC Common Redis--> <!-- WFC Common Redis-->
<dependency> <dependency>
<groupId>org.wfc</groupId> <groupId>org.wfc</groupId>
<artifactId>wfc-common-redis</artifactId> <artifactId>wfc-common-redis</artifactId>
</dependency> </dependency>
</dependencies> </dependencies>
</project> </project>

View File

@@ -16,7 +16,7 @@ import org.wfc.common.security.annotation.RequiresPermissions;
import org.wfc.common.security.annotation.RequiresRoles; import org.wfc.common.security.annotation.RequiresRoles;
import org.wfc.common.security.service.TokenService; import org.wfc.common.security.service.TokenService;
import org.wfc.common.security.utils.SecurityUtils; import org.wfc.common.security.utils.SecurityUtils;
import org.wfc.system.api.model.LoginUser; import org.wfc.common.core.domain.LoginUser;
/** /**
* Token 权限验证,逻辑实现类 * Token 权限验证,逻辑实现类
@@ -98,7 +98,7 @@ public class AuthLogic
* *
* @param loginUser 当前用户信息 * @param loginUser 当前用户信息
*/ */
public void verifyLoginUserExpire(LoginUser loginUser) public <T> void verifyLoginUserExpire(LoginUser<T> loginUser)
{ {
tokenService.verifyToken(loginUser); tokenService.verifyToken(loginUser);
} }

View File

@@ -2,7 +2,7 @@ package org.wfc.common.security.auth;
import org.wfc.common.security.annotation.RequiresPermissions; import org.wfc.common.security.annotation.RequiresPermissions;
import org.wfc.common.security.annotation.RequiresRoles; import org.wfc.common.security.annotation.RequiresRoles;
import org.wfc.system.api.model.LoginUser; import org.wfc.common.core.domain.LoginUser;
/** /**
* Token 权限验证工具类 * Token 权限验证工具类

View File

@@ -10,7 +10,7 @@ import org.wfc.common.core.utils.ServletUtils;
import org.wfc.common.core.utils.StringUtils; import org.wfc.common.core.utils.StringUtils;
import org.wfc.common.security.auth.AuthUtil; import org.wfc.common.security.auth.AuthUtil;
import org.wfc.common.security.utils.SecurityUtils; import org.wfc.common.security.utils.SecurityUtils;
import org.wfc.system.api.model.LoginUser; import org.wfc.common.core.domain.LoginUser;
/** /**
* 自定义请求头拦截器将Header数据封装到线程变量中方便获取 * 自定义请求头拦截器将Header数据封装到线程变量中方便获取

View File

@@ -17,7 +17,9 @@ import org.wfc.common.core.utils.ip.IpUtils;
import org.wfc.common.core.utils.uuid.IdUtils; import org.wfc.common.core.utils.uuid.IdUtils;
import org.wfc.common.redis.service.RedisService; import org.wfc.common.redis.service.RedisService;
import org.wfc.common.security.utils.SecurityUtils; import org.wfc.common.security.utils.SecurityUtils;
import org.wfc.system.api.model.LoginUser; import org.wfc.common.core.domain.LoginUser;
import org.wfc.system.api.domain.SysUser;
import org.wfc.user.api.domain.UUser;
/** /**
* token验证处理 * token验证处理
@@ -45,11 +47,21 @@ public class TokenService
/** /**
* 创建令牌 * 创建令牌
*/ */
public Map<String, Object> createToken(LoginUser loginUser) public <T> Map<String, Object> createToken(LoginUser<T> loginUser)
{ {
String token = IdUtils.fastUUID(); String token = IdUtils.fastUUID();
Long userId = loginUser.getSysUser().getUserId(); Long userId = 0L;
String userName = loginUser.getSysUser().getUserName(); String userName = "";
T user = loginUser.getUser();
if (user instanceof SysUser) {
SysUser sysUser = (SysUser) user;
userId = sysUser.getUserId();
userName = sysUser.getUserName();
}else if (user instanceof UUser) {
UUser uUser = (UUser) user;
userId = uUser.getUserId();
userName = uUser.getUserName();
}
loginUser.setToken(token); loginUser.setToken(token);
loginUser.setUserid(userId); loginUser.setUserid(userId);
loginUser.setUsername(userName); loginUser.setUsername(userName);
@@ -74,7 +86,7 @@ public class TokenService
* *
* @return 用户信息 * @return 用户信息
*/ */
public LoginUser getLoginUser() public <T> LoginUser<T> getLoginUser()
{ {
return getLoginUser(ServletUtils.getRequest()); return getLoginUser(ServletUtils.getRequest());
} }
@@ -84,7 +96,7 @@ public class TokenService
* *
* @return 用户信息 * @return 用户信息
*/ */
public LoginUser getLoginUser(HttpServletRequest request) public <T> LoginUser<T> getLoginUser(HttpServletRequest request)
{ {
// 获取请求携带的令牌 // 获取请求携带的令牌
String token = SecurityUtils.getToken(request); String token = SecurityUtils.getToken(request);
@@ -96,9 +108,9 @@ public class TokenService
* *
* @return 用户信息 * @return 用户信息
*/ */
public LoginUser getLoginUser(String token) public <T> LoginUser<T> getLoginUser(String token)
{ {
LoginUser user = null; LoginUser<T> user = null;
try try
{ {
if (StringUtils.isNotEmpty(token)) if (StringUtils.isNotEmpty(token))
@@ -118,7 +130,7 @@ public class TokenService
/** /**
* 设置用户身份信息 * 设置用户身份信息
*/ */
public void setLoginUser(LoginUser loginUser) public <T> void setLoginUser(LoginUser<T> loginUser)
{ {
if (StringUtils.isNotNull(loginUser) && StringUtils.isNotEmpty(loginUser.getToken())) if (StringUtils.isNotNull(loginUser) && StringUtils.isNotEmpty(loginUser.getToken()))
{ {
@@ -141,9 +153,9 @@ public class TokenService
/** /**
* 验证令牌有效期相差不足120分钟自动刷新缓存 * 验证令牌有效期相差不足120分钟自动刷新缓存
* *
* @param loginUser * @param loginUser 登录用户
*/ */
public void verifyToken(LoginUser loginUser) public <T> void verifyToken(LoginUser<T> loginUser)
{ {
long expireTime = loginUser.getExpireTime(); long expireTime = loginUser.getExpireTime();
long currentTime = System.currentTimeMillis(); long currentTime = System.currentTimeMillis();
@@ -158,7 +170,7 @@ public class TokenService
* *
* @param loginUser 登录信息 * @param loginUser 登录信息
*/ */
public void refreshToken(LoginUser loginUser) public <T> void refreshToken(LoginUser<T> loginUser)
{ {
loginUser.setLoginTime(System.currentTimeMillis()); loginUser.setLoginTime(System.currentTimeMillis());
loginUser.setExpireTime(loginUser.getLoginTime() + expireTime * MILLIS_MINUTE); loginUser.setExpireTime(loginUser.getLoginTime() + expireTime * MILLIS_MINUTE);

View File

@@ -22,7 +22,7 @@ public class DictUtils
* @param key 参数键 * @param key 参数键
* @param dictDatas 字典数据列表 * @param dictDatas 字典数据列表
*/ */
public static void setDictCache(String key, List<SysDictData> dictDatas) public static <T> void setDictCache(String key, List<T> dictDatas)
{ {
SpringUtils.getBean(RedisService.class).setCacheObject(getCacheKey(key), dictDatas); SpringUtils.getBean(RedisService.class).setCacheObject(getCacheKey(key), dictDatas);
} }
@@ -33,12 +33,12 @@ public class DictUtils
* @param key 参数键 * @param key 参数键
* @return dictDatas 字典数据列表 * @return dictDatas 字典数据列表
*/ */
public static List<SysDictData> getDictCache(String key) public static <T> List<T> getDictCache(String key, Class<T> clazz)
{ {
JSONArray arrayCache = SpringUtils.getBean(RedisService.class).getCacheObject(getCacheKey(key)); JSONArray arrayCache = SpringUtils.getBean(RedisService.class).getCacheObject(getCacheKey(key));
if (StringUtils.isNotNull(arrayCache)) if (StringUtils.isNotNull(arrayCache))
{ {
return arrayCache.toList(SysDictData.class); return arrayCache.toList(clazz);
} }
return null; return null;
} }

View File

@@ -7,7 +7,7 @@ import org.wfc.common.core.constant.TokenConstants;
import org.wfc.common.core.context.SecurityContextHolder; import org.wfc.common.core.context.SecurityContextHolder;
import org.wfc.common.core.utils.ServletUtils; import org.wfc.common.core.utils.ServletUtils;
import org.wfc.common.core.utils.StringUtils; import org.wfc.common.core.utils.StringUtils;
import org.wfc.system.api.model.LoginUser; import org.wfc.common.core.domain.LoginUser;
/** /**
* 权限获取工具类 * 权限获取工具类
@@ -43,7 +43,7 @@ public class SecurityUtils
/** /**
* 获取登录用户信息 * 获取登录用户信息
*/ */
public static LoginUser getLoginUser() public static <T> LoginUser<T> getLoginUser()
{ {
return SecurityContextHolder.get(SecurityConstants.LOGIN_USER, LoginUser.class); return SecurityContextHolder.get(SecurityConstants.LOGIN_USER, LoginUser.class);
} }

View File

@@ -10,6 +10,7 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import org.wfc.common.core.domain.LoginUser;
import org.wfc.common.core.domain.R; import org.wfc.common.core.domain.R;
import org.wfc.common.core.utils.StringUtils; import org.wfc.common.core.utils.StringUtils;
import org.wfc.common.core.utils.file.FileTypeUtils; import org.wfc.common.core.utils.file.FileTypeUtils;
@@ -23,7 +24,6 @@ import org.wfc.common.security.utils.SecurityUtils;
import org.wfc.system.api.RemoteFileService; import org.wfc.system.api.RemoteFileService;
import org.wfc.system.api.domain.SysFile; import org.wfc.system.api.domain.SysFile;
import org.wfc.system.api.domain.SysUser; import org.wfc.system.api.domain.SysUser;
import org.wfc.system.api.model.LoginUser;
import org.wfc.system.service.ISysUserService; import org.wfc.system.service.ISysUserService;
/** /**
@@ -65,8 +65,8 @@ public class SysProfileController extends BaseController
@PutMapping @PutMapping
public AjaxResult updateProfile(@RequestBody SysUser user) public AjaxResult updateProfile(@RequestBody SysUser user)
{ {
LoginUser loginUser = SecurityUtils.getLoginUser(); LoginUser<SysUser> loginUser = SecurityUtils.getLoginUser();
SysUser currentUser = loginUser.getSysUser(); SysUser currentUser = loginUser.getUser();
currentUser.setNickName(user.getNickName()); currentUser.setNickName(user.getNickName());
currentUser.setEmail(user.getEmail()); currentUser.setEmail(user.getEmail());
currentUser.setPhonenumber(user.getPhonenumber()); currentUser.setPhonenumber(user.getPhonenumber());
@@ -110,8 +110,10 @@ public class SysProfileController extends BaseController
if (userService.resetUserPwd(username, newPassword) > 0) if (userService.resetUserPwd(username, newPassword) > 0)
{ {
// 更新缓存用户密码 // 更新缓存用户密码
LoginUser loginUser = SecurityUtils.getLoginUser(); LoginUser<SysUser> loginUser = SecurityUtils.getLoginUser();
loginUser.getSysUser().setPassword(newPassword); SysUser sysUser = loginUser.getUser();
sysUser.setPassword(newPassword);
loginUser.setUser(sysUser);
tokenService.setLoginUser(loginUser); tokenService.setLoginUser(loginUser);
return success(); return success();
} }
@@ -127,7 +129,7 @@ public class SysProfileController extends BaseController
{ {
if (!file.isEmpty()) if (!file.isEmpty())
{ {
LoginUser loginUser = SecurityUtils.getLoginUser(); LoginUser<SysUser> loginUser = SecurityUtils.getLoginUser();
String extension = FileTypeUtils.getExtension(file); String extension = FileTypeUtils.getExtension(file);
if (!StringUtils.equalsAnyIgnoreCase(extension, MimeTypeUtils.IMAGE_EXTENSION)) if (!StringUtils.equalsAnyIgnoreCase(extension, MimeTypeUtils.IMAGE_EXTENSION))
{ {
@@ -144,7 +146,9 @@ public class SysProfileController extends BaseController
AjaxResult ajax = AjaxResult.success(); AjaxResult ajax = AjaxResult.success();
ajax.put("imgUrl", url); ajax.put("imgUrl", url);
// 更新缓存用户头像 // 更新缓存用户头像
loginUser.getSysUser().setAvatar(url); SysUser sysUser = loginUser.getUser();
sysUser.setAvatar(url);
loginUser.setUser(sysUser);
tokenService.setLoginUser(loginUser); tokenService.setLoginUser(loginUser);
return ajax; return ajax;
} }

View File

@@ -29,7 +29,7 @@ import org.wfc.common.security.utils.SecurityUtils;
import org.wfc.system.api.domain.SysDept; import org.wfc.system.api.domain.SysDept;
import org.wfc.system.api.domain.SysRole; import org.wfc.system.api.domain.SysRole;
import org.wfc.system.api.domain.SysUser; import org.wfc.system.api.domain.SysUser;
import org.wfc.system.api.model.LoginUser; import org.wfc.common.core.domain.LoginUser;
import org.wfc.system.service.ISysConfigService; import org.wfc.system.service.ISysConfigService;
import org.wfc.system.service.ISysDeptService; import org.wfc.system.service.ISysDeptService;
import org.wfc.system.service.ISysPermissionService; import org.wfc.system.service.ISysPermissionService;
@@ -133,7 +133,7 @@ public class SysUserController extends BaseController
*/ */
@InnerAuth @InnerAuth
@GetMapping("/info/{username}") @GetMapping("/info/{username}")
public R<LoginUser> info(@PathVariable("username") String username) public R<LoginUser<SysUser>> info(@PathVariable("username") String username)
{ {
SysUser sysUser = userService.selectUserByUserName(username); SysUser sysUser = userService.selectUserByUserName(username);
if (StringUtils.isNull(sysUser)) if (StringUtils.isNull(sysUser))
@@ -144,8 +144,8 @@ public class SysUserController extends BaseController
Set<String> roles = permissionService.getRolePermission(sysUser); Set<String> roles = permissionService.getRolePermission(sysUser);
// 权限集合 // 权限集合
Set<String> permissions = permissionService.getMenuPermission(sysUser); Set<String> permissions = permissionService.getMenuPermission(sysUser);
LoginUser sysUserVo = new LoginUser(); LoginUser<SysUser> sysUserVo = new LoginUser<SysUser>();
sysUserVo.setSysUser(sysUser); sysUserVo.setUser(sysUser);
sysUserVo.setRoles(roles); sysUserVo.setRoles(roles);
sysUserVo.setPermissions(permissions); sysUserVo.setPermissions(permissions);
return R.ok(sysUserVo); return R.ok(sysUserVo);
@@ -188,8 +188,8 @@ public class SysUserController extends BaseController
@GetMapping("getInfo") @GetMapping("getInfo")
public AjaxResult getInfo() public AjaxResult getInfo()
{ {
LoginUser loginUser = SecurityUtils.getLoginUser(); LoginUser<SysUser> loginUser = SecurityUtils.getLoginUser();
SysUser user = loginUser.getSysUser(); SysUser user = loginUser.getUser();
// 角色集合 // 角色集合
Set<String> roles = permissionService.getRolePermission(user); Set<String> roles = permissionService.getRolePermission(user);
// 权限集合 // 权限集合

View File

@@ -19,7 +19,7 @@ import org.wfc.common.log.annotation.Log;
import org.wfc.common.log.enums.BusinessType; import org.wfc.common.log.enums.BusinessType;
import org.wfc.common.redis.service.RedisService; import org.wfc.common.redis.service.RedisService;
import org.wfc.common.security.annotation.RequiresPermissions; import org.wfc.common.security.annotation.RequiresPermissions;
import org.wfc.system.api.model.LoginUser; import org.wfc.common.core.domain.LoginUser;
import org.wfc.system.domain.SysUserOnline; import org.wfc.system.domain.SysUserOnline;
import org.wfc.system.service.ISysUserOnlineService; import org.wfc.system.service.ISysUserOnlineService;

View File

@@ -1,6 +1,6 @@
package org.wfc.system.service; package org.wfc.system.service;
import org.wfc.system.api.model.LoginUser; import org.wfc.common.core.domain.LoginUser;
import org.wfc.system.domain.SysUserOnline; import org.wfc.system.domain.SysUserOnline;
/** /**

View File

@@ -73,7 +73,7 @@ public class SysDictTypeServiceImpl implements ISysDictTypeService
@Override @Override
public List<SysDictData> selectDictDataByType(String dictType) public List<SysDictData> selectDictDataByType(String dictType)
{ {
List<SysDictData> dictDatas = DictUtils.getDictCache(dictType); List<SysDictData> dictDatas = DictUtils.getDictCache(dictType, SysDictData.class);
if (StringUtils.isNotEmpty(dictDatas)) if (StringUtils.isNotEmpty(dictDatas))
{ {
return dictDatas; return dictDatas;

View File

@@ -2,7 +2,7 @@ package org.wfc.system.service.impl;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.wfc.common.core.utils.StringUtils; import org.wfc.common.core.utils.StringUtils;
import org.wfc.system.api.model.LoginUser; import org.wfc.common.core.domain.LoginUser;
import org.wfc.system.domain.SysUserOnline; import org.wfc.system.domain.SysUserOnline;
import org.wfc.system.service.ISysUserOnlineService; import org.wfc.system.service.ISysUserOnlineService;