Commit 49255a85 authored by 赵啸非's avatar 赵啸非

添加用户登录鉴权

parent 1ac4cb18
package com.mortals.xhx.base.framework.security; package com.mortals.xhx.base.framework.interceptor;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
...@@ -8,6 +8,7 @@ import com.mortals.framework.service.ICacheService; ...@@ -8,6 +8,7 @@ import com.mortals.framework.service.ICacheService;
import com.mortals.framework.service.IUser; import com.mortals.framework.service.IUser;
import com.mortals.framework.util.DateUtils; import com.mortals.framework.util.DateUtils;
import com.mortals.framework.util.StringUtils; import com.mortals.framework.util.StringUtils;
import com.mortals.xhx.base.system.user.model.UserEntity;
import io.jsonwebtoken.Claims; import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts; import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm; import io.jsonwebtoken.SignatureAlgorithm;
...@@ -28,7 +29,7 @@ import java.util.Map; ...@@ -28,7 +29,7 @@ import java.util.Map;
* @author zxfei * @author zxfei
*/ */
//@Service @Service
@Order(1) @Order(1)
@Slf4j @Slf4j
public class AuthTokenServiceImpl implements IAuthTokenService { public class AuthTokenServiceImpl implements IAuthTokenService {
...@@ -51,7 +52,7 @@ public class AuthTokenServiceImpl implements IAuthTokenService { ...@@ -51,7 +52,7 @@ public class AuthTokenServiceImpl implements IAuthTokenService {
@Value("${spring.redis.database:}") @Value("${spring.redis.database:}")
private Integer db; private Integer db;
@Value("${token.database:1}") @Value("${token.database:0}")
private Integer portalDb; private Integer portalDb;
protected static final Long SECOND = 1l; protected static final Long SECOND = 1l;
...@@ -88,119 +89,8 @@ public class AuthTokenServiceImpl implements IAuthTokenService { ...@@ -88,119 +89,8 @@ public class AuthTokenServiceImpl implements IAuthTokenService {
String userStr = cacheService.get(userKey); String userStr = cacheService.get(userKey);
cacheService.select(db); cacheService.select(db);
if (StringUtils.isNotEmpty(userStr)) { if (StringUtils.isNotEmpty(userStr)) {
JSONObject userObj = JSON.parseObject(userStr); UserEntity userEntity = JSONObject.parseObject(userStr, UserEntity.class);
Long userId = userObj.getLongValue("id"); return userEntity;
Integer userType = userObj.getIntValue("userType");
Long customerId = userObj.getLongValue("customerId");
Long siteId = userObj.getLongValue("siteId");
Long loginTime = userObj.getLongValue("loginTime");
Long expireTime = userObj.getLongValue("expireTime");
String loginName = userObj.getString("loginName");
String realName = userObj.getString("realName");
String siteIds = userObj.getString("siteIds");
String menuUrl = userObj.getString("menuUrl");
Boolean isManager = userObj.getBoolean("manager");
Boolean isSystemUser = userObj.getBoolean("systemUser");
return new IUser() {
@Override
public Long getId() {
return userId;
}
@Override
public Long getDeptId() {
return null;
}
@Override
public String getDeptName() {
return null;
}
@Override
public Long getCustomerId() {
return customerId;
}
@Override
public Long getSiteId() {
return siteId;
}
@Override
public String getSiteIds() {
return siteIds;
}
@Override
public Long getCustomerJoinId() {
return null;
}
@Override
public String getCustomerNum() {
return null;
}
@Override
public String getLoginName() {
return loginName;
}
@Override
public String getRealName() {
return realName;
}
@Override
public boolean isAdmin() {
return userId == 1L;
}
@Override
public boolean isSystemUser() {
return isSystemUser;
}
@Override
public boolean isManager() {
return isManager;
}
@Override
public Integer getUserType() {
return userType;
}
@Override
public String getToken() {
return token;
}
@Override
public Long getLoginTime() {
return loginTime;
}
@Override
public Long getExpireTime() {
return expireTime;
}
@Override
public void setExpireTime(Long expireTime) {
}
@Override
public String getMenuUrl() {
return menuUrl;
}
};
} }
} catch (Exception e) { } catch (Exception e) {
log.error("解析jwt token异常!", e); log.error("解析jwt token异常!", e);
...@@ -274,7 +164,7 @@ public class AuthTokenServiceImpl implements IAuthTokenService { ...@@ -274,7 +164,7 @@ public class AuthTokenServiceImpl implements IAuthTokenService {
// 根据uuid将user缓存 // 根据uuid将user缓存
String userKey = getTokenKey(user.getToken()); String userKey = getTokenKey(user.getToken());
//设置有效时间 单位秒 //设置有效时间 单位秒
cacheService.setnx(userKey, user, expireTime * SECOND_MINUTE); cacheService.set(userKey, user, expireTime * SECOND_MINUTE);
} }
......
//package com.mortals.xhx.base.framework.security.filter;
//
//import com.alibaba.fastjson.JSONObject;
//import com.mortals.framework.util.StringUtils;
//import com.mortals.framework.web.interceptor.BaseInterceptor;
//import com.mortals.xhx.base.DeviceAuth;
//import com.mortals.xhx.busiz.security.SecurityUtils;
//import com.mortals.xhx.busiz.security.DeviceTokenService;
//import com.mortals.xhx.common.code.ApiRespCodeEnum;
//import com.mortals.xhx.module.device.model.DeviceEntity;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Component;
//import org.springframework.web.method.HandlerMethod;
//
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import java.lang.reflect.Method;
//
///**
// * 设备上报验证
// *
// * @author: zxfei
// * @date: 2022/4/11 15:50
// */
//@Component
//public class DeviceSubmitInterceptor extends BaseInterceptor {
//
// @Autowired
// private DeviceTokenService deviceTokenService;
//
// @Override
// public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
// if (handler instanceof HandlerMethod) {
// HandlerMethod handlerMethod = (HandlerMethod) handler;
// Method method = handlerMethod.getMethod();
// DeviceAuth annotation = method.getAnnotation(DeviceAuth.class);
// if (annotation != null) {
// //校验
// if (this.verifyDevice(request)) {
// JSONObject ret = new JSONObject();
// ret.put("code", ApiRespCodeEnum.TOKEN_FAILED.getValue());
// ret.put("msg", ApiRespCodeEnum.TOKEN_FAILED.getLabel());
// super.writeJsonResponse(response, 200, JSONObject.toJSONString(ret));
// return false;
// }
// }
// return true;
// } else {
// return super.preHandle(request, response, handler);
// }
// }
//
// public boolean verifyDevice(HttpServletRequest request) {
// DeviceEntity device = deviceTokenService.getLoginDevice(request);
// if (StringUtils.isNotNull(device) && StringUtils.isNull(SecurityUtils.getAuthentication())) {
// deviceTokenService.verifyToken(device);
// return true;
// }
// return false;
//
// }
//
//
//}
...@@ -3,6 +3,7 @@ package com.mortals.xhx.daemon.task; ...@@ -3,6 +3,7 @@ package com.mortals.xhx.daemon.task;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.mortals.framework.common.Rest; import com.mortals.framework.common.Rest;
import com.mortals.framework.exception.AppException; import com.mortals.framework.exception.AppException;
import com.mortals.framework.model.Context;
import com.mortals.framework.service.ITask; import com.mortals.framework.service.ITask;
import com.mortals.framework.service.ITaskExcuteService; import com.mortals.framework.service.ITaskExcuteService;
import com.mortals.xhx.base.system.user.model.UserEntity; import com.mortals.xhx.base.system.user.model.UserEntity;
...@@ -10,7 +11,9 @@ import com.mortals.xhx.base.system.user.service.UserService; ...@@ -10,7 +11,9 @@ import com.mortals.xhx.base.system.user.service.UserService;
import com.mortals.xhx.common.pdu.RespData; import com.mortals.xhx.common.pdu.RespData;
import com.mortals.xhx.common.pdu.user.UserPdu; import com.mortals.xhx.common.pdu.user.UserPdu;
import com.mortals.xhx.common.utils.BeanUtil; import com.mortals.xhx.common.utils.BeanUtil;
import com.mortals.xhx.common.utils.SyncTreeSiteThread;
import com.mortals.xhx.feign.user.IUserFeign; import com.mortals.xhx.feign.user.IUserFeign;
import com.mortals.xhx.module.site.service.SiteService;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.BeanUtilsBean; import org.apache.commons.beanutils.BeanUtilsBean;
import org.springframework.beans.BeanUtils; import org.springframework.beans.BeanUtils;
...@@ -31,6 +34,8 @@ public class SyncUserTaskImpl implements ITaskExcuteService { ...@@ -31,6 +34,8 @@ public class SyncUserTaskImpl implements ITaskExcuteService {
private IUserFeign userFeign; private IUserFeign userFeign;
@Autowired @Autowired
private UserService userService; private UserService userService;
@Autowired
private SiteService siteService;
@Override @Override
...@@ -50,6 +55,9 @@ public class SyncUserTaskImpl implements ITaskExcuteService { ...@@ -50,6 +55,9 @@ public class SyncUserTaskImpl implements ITaskExcuteService {
entity.initAttrValue(); entity.initAttrValue();
BeanUtils.copyProperties(user, entity, BeanUtil.getNullPropertyNames(user)); BeanUtils.copyProperties(user, entity, BeanUtil.getNullPropertyNames(user));
userService.save(entity); userService.save(entity);
Context context = new Context();
context.setUser(userEntity);
new Thread(new SyncTreeSiteThread(siteService, context)).start();
} }
}); });
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment