update user log

木子李·De 3 years ago
parent 6c451caaa5
commit 67f377bbfb

@ -5,6 +5,7 @@ import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import javax.validation.constraints.NotBlank;
import java.util.List;
@ApiModel(value = "用户登录")
@Data
@ -17,4 +18,13 @@ public class GaeaUserDto {
@ApiModelProperty(value = "密码")
@NotBlank
private String password;
/** 真实用户 */
private String realName;
/** 登录成功后的 */
private String token;
/** 用户所拥有的权限合集 */
private List<String> authorities;
}

@ -18,4 +18,9 @@ import java.util.List;
@Mapper
public interface AccessUserMapper extends GaeaBaseMapper<AccessUser> {
/**
* @param loginName
* @return
*/
List<String> queryAuthoritiesByLoginName(@Param("loginName")String loginName);
}

@ -40,5 +40,5 @@ public interface AccessUserService extends GaeaBaseService<AccessUserParam, Acce
* @param gaeaUserDto
* @return
*/
Map login(GaeaUserDto gaeaUserDto);
GaeaUserDto login(GaeaUserDto gaeaUserDto);
}

@ -1,6 +1,7 @@
package com.anjiplus.template.gaea.business.modules.accessuser.service.impl;
import com.alibaba.fastjson.JSONObject;
import com.anji.plus.gaea.bean.TreeNode;
import com.anji.plus.gaea.cache.CacheHelper;
import com.anji.plus.gaea.exception.BusinessExceptionBuilder;
@ -9,6 +10,7 @@ import com.anji.plus.gaea.curd.mapper.GaeaBaseMapper;
import com.anji.plus.gaea.utils.GaeaUtils;
import com.anji.plus.gaea.utils.JwtBean;
import com.anjiplus.template.gaea.business.code.ResponseCode;
import com.anjiplus.template.gaea.business.constant.BusinessConstant;
import com.anjiplus.template.gaea.business.modules.accessrole.dao.AccessRoleMapper;
import com.anjiplus.template.gaea.business.modules.accessrole.dao.entity.AccessRole;
import com.anjiplus.template.gaea.business.modules.accessuser.controller.dto.AccessUserDto;
@ -127,32 +129,48 @@ public class AccessUserServiceImpl implements AccessUserService {
}
@Override
public Map login(GaeaUserDto gaeaUserDto) {
public GaeaUserDto login(GaeaUserDto gaeaUserDto) {
String loginName = gaeaUserDto.getLoginName();
String password = gaeaUserDto.getPassword();
//1.判断用户是否存在
// 1.判断用户是否存在
LambdaQueryWrapper<AccessUser> wrapper = Wrappers.lambdaQuery();
wrapper.eq(AccessUser::getLoginName, loginName);
AccessUser accessUser = accessUserMapper.selectOne(wrapper);
if (null == accessUser || !accessUser.getPassword().equals(MD5Util.encrypt(password))) {
if (null == accessUser) {
throw BusinessExceptionBuilder.build(ResponseCode.LOGIN_ERROR);
}
// 2.密码错误
if (!accessUser.getPassword().equals(MD5Util.encrypt(password))) {
throw BusinessExceptionBuilder.build(ResponseCode.USER_PASSWORD_ERROR);
}
Map<String, String> map = Maps.newHashMap();
//将登录信息缓存,默认一小时
if (cacheHelper.exist(loginName)) {
map.put("token", cacheHelper.stringGet(loginName));
map.put("loginName", loginName);
} else {
String uuid = GaeaUtils.UUID();
String token = jwtBean.createToken(loginName, uuid);
cacheHelper.stringSetExpire(loginName, token, 3600);
map.put("token", token);
map.put("loginName", loginName);
// 3.如果该用户登录未过期,这里允许一个用户在多个终端登录
String tokenKey = String.format(BusinessConstant.GAEA_SECURITY_LOGIN_TOKEN, loginName);
String userKey = String.format(BusinessConstant.GAEA_SECURITY_LOGIN_USER, loginName);
String token = "";
GaeaUserDto gaeaUser = new GaeaUserDto();
if (cacheHelper.exist(tokenKey) && cacheHelper.exist(userKey)) {
token = cacheHelper.stringGet(tokenKey);
gaeaUser = JSONObject.parseObject(cacheHelper.stringGet(userKey), GaeaUserDto.class);
return gaeaUser;
}
return map;
// 4.生成用户token
String uuid = GaeaUtils.UUID();
token = jwtBean.createToken(loginName, uuid);
cacheHelper.stringSetExpire(tokenKey, token, 3600);
// 5.缓存用户权限主信息
List<String> authorities = accessUserMapper.queryAuthoritiesByLoginName(loginName);
gaeaUser.setLoginName(loginName);
gaeaUser.setRealName(accessUser.getRealName());
gaeaUser.setToken(token);
gaeaUser.setAuthorities(authorities);
String gaeaUserStr = JSONObject.toJSONString(gaeaUser);
cacheHelper.stringSetExpire(userKey, gaeaUserStr, 3600);
return gaeaUser;
}
}
Loading…
Cancel
Save