|
|
@ -98,9 +98,25 @@ public class SecurityUtil { |
|
|
return user; |
|
|
return user; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public Worker findWorkerByUsername(String username) { |
|
|
|
|
|
String key = "workername::" + username; |
|
|
|
|
|
// 读取缓存
|
|
|
|
|
|
String res = redisTemplate.get(key); |
|
|
|
|
|
if (StrUtil.isNotBlank(res)) { |
|
|
|
|
|
return new Gson().fromJson(res, Worker.class); |
|
|
|
|
|
} |
|
|
|
|
|
Worker worker = workerDao.findByUsername(username); |
|
|
|
|
|
// 缓存
|
|
|
|
|
|
redisTemplate.set(key, new Gson().toJson(worker), 15L, TimeUnit.DAYS); |
|
|
|
|
|
return worker; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
public User findUserByMobile(String mobile) { |
|
|
public User findUserByMobile(String mobile) { |
|
|
return userToDTO(userDao.findByMobile(mobile)); |
|
|
return userToDTO(userDao.findByMobile(mobile)); |
|
|
} |
|
|
} |
|
|
|
|
|
public Worker findWorkerByMobile(String mobile) { |
|
|
|
|
|
return workerDao.findByMobile(mobile); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
public User findUserByEmail(String email) { |
|
|
public User findUserByEmail(String email) { |
|
|
return userToDTO(userDao.findByEmail(email)); |
|
|
return userToDTO(userDao.findByEmail(email)); |
|
|
@ -145,6 +161,25 @@ public class SecurityUtil { |
|
|
return user; |
|
|
return user; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public Worker checkWorkerPassword(String username, String password) { |
|
|
|
|
|
Worker worker; |
|
|
|
|
|
// 校验用户名
|
|
|
|
|
|
if (NameUtil.mobile(username)) { |
|
|
|
|
|
worker = findWorkerByMobile(username); |
|
|
|
|
|
} else { |
|
|
|
|
|
worker = findWorkerByUsername(username); |
|
|
|
|
|
} |
|
|
|
|
|
if (worker == null) { |
|
|
|
|
|
return null; |
|
|
|
|
|
} |
|
|
|
|
|
// 校验密码
|
|
|
|
|
|
Boolean isValid = new BCryptPasswordEncoder().matches(password, worker.getPassword()); |
|
|
|
|
|
if (!isValid) { |
|
|
|
|
|
return null; |
|
|
|
|
|
} |
|
|
|
|
|
return worker; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
public String getToken(String username, Boolean saveLogin) { |
|
|
public String getToken(String username, Boolean saveLogin) { |
|
|
if (StrUtil.isBlank(username)) { |
|
|
if (StrUtil.isBlank(username)) { |
|
|
throw new HiverException("username不能为空"); |
|
|
throw new HiverException("username不能为空"); |
|
|
@ -221,6 +256,20 @@ public class SecurityUtil { |
|
|
return findUserByUsername(authentication.getName()); |
|
|
return findUserByUsername(authentication.getName()); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* 获取当前登录用户 包含所有信息 |
|
|
|
|
|
* |
|
|
|
|
|
* @return |
|
|
|
|
|
*/ |
|
|
|
|
|
public Worker getCurrWorker() { |
|
|
|
|
|
Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); |
|
|
|
|
|
if (authentication == null || !authentication.isAuthenticated() || authentication.getName() == null |
|
|
|
|
|
|| authentication instanceof AnonymousAuthenticationToken) { |
|
|
|
|
|
throw new HiverException("未检测到登录用户"); |
|
|
|
|
|
} |
|
|
|
|
|
return findWorkerByUsername(authentication.getName()); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* 获取当前登录用户部分基本信息 id、username、nickname、mobile、email、departmentId、type、permissions(角色和菜单名) |
|
|
* 获取当前登录用户部分基本信息 id、username、nickname、mobile、email、departmentId、type、permissions(角色和菜单名) |
|
|
* |
|
|
* |
|
|
@ -464,7 +513,7 @@ public class SecurityUtil { |
|
|
if (worker == null) { |
|
|
if (worker == null) { |
|
|
throw new HiverException("worker不能为空"); |
|
|
throw new HiverException("worker不能为空"); |
|
|
} |
|
|
} |
|
|
if (WorkerConstant.USER_STATUS_DISABLE.equals(worker.getWorkerStatus())) { |
|
|
if (WorkerConstant.WORKER_STATUS_DISABLE.equals(worker.getWorkerStatus())) { |
|
|
throw new HiverException("账户被禁用,请联系管理员"); |
|
|
throw new HiverException("账户被禁用,请联系管理员"); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|