完善mongo登录逻辑

This commit is contained in:
WenG 2022-05-19 13:29:12 +08:00
parent 7b1e3a85f6
commit 22588cf143
4 changed files with 157 additions and 85 deletions

View File

@ -2,26 +2,19 @@ package ink.wgink.login.base.service.user;
import com.alibaba.fastjson.JSONObject;
import ink.wgink.exceptions.SearchException;
import ink.wgink.interfaces.consts.ISystemConstant;
import ink.wgink.interfaces.user.IUserDetailCheckService;
import ink.wgink.login.base.exceptions.UserAuthenticationException;
import ink.wgink.login.base.manager.ConfigManager;
import ink.wgink.pojo.bos.LoginUser;
import ink.wgink.login.base.util.UserLoginUtil;
import ink.wgink.service.user.enums.UserStateEnum;
import ink.wgink.service.user.pojo.pos.UserPO;
import ink.wgink.service.user.service.IUserService;
import ink.wgink.util.date.DateUtil;
import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import java.util.Map;
/**
* @ClassName: UserDetailServiceImpl
* @Description: User Detail
@ -65,79 +58,7 @@ public class UserDetailServiceImpl implements UserDetailsService, IUserDetailChe
if (DateUtil.isDateExpired(userPO.getUserExpiredDate())) {
throw new UserAuthenticationException("账号已经过期");
}
return createUserBO(userPO);
}
/**
* 创建UserBO
*
* @param userPO
* @return
*/
private LoginUser createUserBO(UserPO userPO) {
LoginUser loginUser = new LoginUser(userPO.getUserUsername(), userPO.getUserPassword(),
userPO.getUserType() == 1 ? true : false,
isAccountNonExpired(userPO),
isCredentialsNonExpired(userPO),
userPO.getUserState() == 0 ? true : false);
loginUser.setUserId(userPO.getUserId());
loginUser.setUserName(userPO.getUserName());
loginUser.setUserPhone(userPO.getUserPhone());
loginUser.setUserUKey(userPO.getUserUKey());
loginUser.setLoginType(userPO.getLoginType());
loginUser.setUserEmail(userPO.getUserEmail());
loginUser.setUserAvatar(userPO.getUserAvatar());
return loginUser;
}
/**
* 账号是否没有过期
*
* @param userPO
* @return
*/
private boolean isAccountNonExpired(UserPO userPO) {
if (StringUtils.equals(userPO.getUserUsername(), ISystemConstant.ADMIN)) {
return true;
}
if (!StringUtils.isBlank(userPO.getUserExpiredDate())) {
// 判断用户是否超时失效
DateTime expiredDateTime = DateTime.parse(userPO.getUserExpiredDate(), DateTimeFormat.forPattern(ISystemConstant.DATE_FORMATTER_YYYY_MM_DD_HH_MM_SS));
DateTime nowDateTime = DateTime.now();
if (expiredDateTime.isBefore(nowDateTime)) {
return false;
}
}
return true;
}
/**
* 密码是否没有过期
*
* @param userPO
* @return
*/
private boolean isCredentialsNonExpired(UserPO userPO) {
if (StringUtils.equals(userPO.getUserUsername(), ISystemConstant.ADMIN)) {
return true;
}
Map<String, String> config = ConfigManager.getInstance().getConfig();
String passwordValidity = config.get("passwordValidity");
// 自定义密码有效期
if (StringUtils.equals(passwordValidity, "custom")) {
Object passwordValidityDays = config.get("passwordValidityDays");
int validityDays = passwordValidityDays == null ? 0 : (Integer) passwordValidityDays;
if (validityDays <= 0) {
return true;
}
DateTime passwordModifiedDateTime = DateTime.parse(userPO.getGmtPasswordModified(), DateTimeFormat.forPattern(ISystemConstant.DATE_FORMATTER_YYYY_MM_DD_HH_MM_SS));
DateTime passwordExpiredDateTime = passwordModifiedDateTime.plusDays(validityDays);
DateTime nowDateTime = DateTime.now();
if (passwordExpiredDateTime.isBefore(nowDateTime)) {
return false;
}
}
return true;
return UserLoginUtil.createLoginUser(userPO);
}
}

View File

@ -0,0 +1,93 @@
package ink.wgink.login.base.util;
import ink.wgink.interfaces.consts.ISystemConstant;
import ink.wgink.login.base.manager.ConfigManager;
import ink.wgink.pojo.bos.LoginUser;
import ink.wgink.service.user.pojo.pos.UserPO;
import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import java.util.Map;
/**
* @Description: 用户登录
* @Author: WenG
* @Date: 2022/5/19 13:23
* @Version: 1.0
**/
public class UserLoginUtil {
/**
* 创建登录用户
*
* @param userPO
* @return
*/
public static LoginUser createLoginUser(UserPO userPO) {
LoginUser loginUser = new LoginUser(userPO.getUserUsername(), userPO.getUserPassword(),
userPO.getUserType() == 1 ? true : false,
isAccountNonExpired(userPO),
isCredentialsNonExpired(userPO),
userPO.getUserState() == 0 ? true : false);
loginUser.setUserId(userPO.getUserId());
loginUser.setUserName(userPO.getUserName());
loginUser.setUserPhone(userPO.getUserPhone());
loginUser.setUserUKey(userPO.getUserUKey());
loginUser.setLoginType(userPO.getLoginType());
loginUser.setUserEmail(userPO.getUserEmail());
loginUser.setUserAvatar(userPO.getUserAvatar());
return loginUser;
}
/**
* 账号是否没有过期
*
* @param userPO
* @return
*/
private static boolean isAccountNonExpired(UserPO userPO) {
if (StringUtils.equals(userPO.getUserUsername(), ISystemConstant.ADMIN)) {
return true;
}
if (!StringUtils.isBlank(userPO.getUserExpiredDate())) {
// 判断用户是否超时失效
DateTime expiredDateTime = DateTime.parse(userPO.getUserExpiredDate(), DateTimeFormat.forPattern(ISystemConstant.DATE_FORMATTER_YYYY_MM_DD_HH_MM_SS));
DateTime nowDateTime = DateTime.now();
if (expiredDateTime.isBefore(nowDateTime)) {
return false;
}
}
return true;
}
/**
* 密码是否没有过期
*
* @param userPO
* @return
*/
private static boolean isCredentialsNonExpired(UserPO userPO) {
if (StringUtils.equals(userPO.getUserUsername(), ISystemConstant.ADMIN)) {
return true;
}
Map<String, String> config = ConfigManager.getInstance().getConfig();
String passwordValidity = config.get("passwordValidity");
// 自定义密码有效期
if (StringUtils.equals(passwordValidity, "custom")) {
Object passwordValidityDays = config.get("passwordValidityDays");
int validityDays = passwordValidityDays == null ? 0 : (Integer) passwordValidityDays;
if (validityDays <= 0) {
return true;
}
DateTime passwordModifiedDateTime = DateTime.parse(userPO.getGmtPasswordModified(), DateTimeFormat.forPattern(ISystemConstant.DATE_FORMATTER_YYYY_MM_DD_HH_MM_SS));
DateTime passwordExpiredDateTime = passwordModifiedDateTime.plusDays(validityDays);
DateTime nowDateTime = DateTime.now();
if (passwordExpiredDateTime.isBefore(nowDateTime)) {
return false;
}
}
return true;
}
}

View File

@ -20,7 +20,7 @@
</dependency>
<dependency>
<groupId>ink.wgink</groupId>
<artifactId>service-user</artifactId>
<artifactId>login-base</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>provided</scope>
</dependency>

View File

@ -1,6 +1,7 @@
package ink.wgink.mongo.login.service.impl;
import ink.wgink.common.base.DefaultBaseService;
import ink.wgink.interfaces.consts.ISystemConstant;
import ink.wgink.interfaces.department.IDepartmentUserBaseService;
import ink.wgink.interfaces.group.IGroupUserBaseService;
import ink.wgink.interfaces.login.mongo.IMongoLoginService;
@ -8,9 +9,21 @@ import ink.wgink.interfaces.position.IPositionUserBaseService;
import ink.wgink.interfaces.role.IRoleMenuBaseService;
import ink.wgink.interfaces.role.IRolePermissionBaseService;
import ink.wgink.interfaces.role.IRoleUserBaseService;
import ink.wgink.login.base.util.UserLoginUtil;
import ink.wgink.pojo.bos.LoginUser;
import ink.wgink.pojo.bos.RoleGrantedAuthorityBO;
import ink.wgink.pojo.dtos.department.DepartmentSimpleDTO;
import ink.wgink.pojo.dtos.group.GroupSimpleDTO;
import ink.wgink.pojo.dtos.position.PositionSimpleDTO;
import ink.wgink.pojo.dtos.role.RoleSimpleDTO;
import ink.wgink.pojo.pos.DepartmentPO;
import ink.wgink.pojo.pos.GroupPO;
import ink.wgink.pojo.pos.PositionPO;
import ink.wgink.pojo.pos.RolePO;
import ink.wgink.pojo.pos.user.mongo.MongoUserPO;
import ink.wgink.service.user.pojo.pos.UserPO;
import ink.wgink.service.user.service.IUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
@ -51,10 +64,11 @@ public class MongoLoginServiceImpl extends DefaultBaseService implements IMongoL
@Override
public void update(String userId) {
UserPO userPO = userService.getPO(userId);
MongoUserPO mongoUserPO = new MongoUserPO();
BeanUtils.copyProperties(userPO, mongoUserPO);
UserLoginUtil.createLoginUser(userPO);
// 非系统管理员
if (!StringUtils.equalsIgnoreCase(userPO.getUserUsername(), ISystemConstant.ADMIN)) {
}
}
@ -73,4 +87,48 @@ public class MongoLoginServiceImpl extends DefaultBaseService implements IMongoL
return mongoTemplate.findOne(new Query().addCriteria(Criteria.where("userUsername").is(username)), MongoUserPO.class, COLLECTION_NAME);
}
/**
* 设置用户信息
*
* @param userPO
*/
private void setMongoUserInfo(MongoUserPO mongoUserPO) {
}
private boolean hasAdmin(List<String> roleIds) {
for (String roleId : roleIds) {
if (StringUtils.equals(ISystemConstant.ADMIN, roleId)) {
return true;
}
}
return false;
}
/**
* 删除重复的角色
*
* @param rolePOs
*/
private void removalDuplicateRole(List<RolePO> rolePOs) {
if (rolePOs == null || rolePOs.isEmpty()) {
return;
}
for (int i = 0; i < rolePOs.size(); i++) {
RolePO rolePO = rolePOs.get(i);
boolean isExist = false;
for (int j = i + 1; j < rolePOs.size(); j++) {
if (StringUtils.equals(rolePO.getRoleId(), rolePOs.get(j).getRoleId())) {
isExist = true;
break;
}
}
if (isExist) {
rolePOs.remove(i);
i--;
break;
}
}
}
}