From 5e77738f9c0fbf98163f36afbd15eacbf76e79b8 Mon Sep 17 00:00:00 2001 From: zxq171 Date: Wed, 25 Dec 2019 11:58:52 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=A0=E9=99=A4es=E4=B8=ADtype=E5=AD=97?= =?UTF-8?q?=E6=AE=B5=EF=BC=8C=E6=96=B0=E5=A2=9E=E7=94=A8=E6=88=B7=E6=9D=83?= =?UTF-8?q?=E9=99=90=E7=BC=93=E5=AD=98util=E6=8A=BD=E7=A6=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../impl/SyChanmgfunVistUrlService.java | 15 ++-- .../util/SysFunvisturlRedisOperator.java | 51 ++++++++++++++ .../service/impl/SysUserServiceImpl.java | 25 +++---- .../sso/sysuser/util/UserRedisOperator.java | 70 +++++++++++++++++++ .../service/impl/SysUserRoleServiceImpl.java | 8 +-- .../util/SysUserRoleRedisOperator.java | 39 +++++++++++ .../interchan/logger/WebLoggerAspect.java | 1 - .../logger/entity/ESAsyncLogger.java | 5 -- 8 files changed, 179 insertions(+), 35 deletions(-) create mode 100644 inter-boot-sso/src/main/java/tech/mhuang/interchan/sso/sysfunvisturl/util/SysFunvisturlRedisOperator.java create mode 100644 inter-boot-sso/src/main/java/tech/mhuang/interchan/sso/sysuser/util/UserRedisOperator.java create mode 100644 inter-boot-sso/src/main/java/tech/mhuang/interchan/sso/sysuserrole/util/SysUserRoleRedisOperator.java diff --git a/inter-boot-sso/src/main/java/tech/mhuang/interchan/sso/sysfunvisturl/service/impl/SyChanmgfunVistUrlService.java b/inter-boot-sso/src/main/java/tech/mhuang/interchan/sso/sysfunvisturl/service/impl/SyChanmgfunVistUrlService.java index 0482ca4..e02d2e8 100644 --- a/inter-boot-sso/src/main/java/tech/mhuang/interchan/sso/sysfunvisturl/service/impl/SyChanmgfunVistUrlService.java +++ b/inter-boot-sso/src/main/java/tech/mhuang/interchan/sso/sysfunvisturl/service/impl/SyChanmgfunVistUrlService.java @@ -12,7 +12,6 @@ import tech.mhuang.core.util.StringUtil; import tech.mhuang.ext.interchan.auth.constant.AuthConstant; import tech.mhuang.ext.interchan.core.service.impl.BaseServiceImpl; import tech.mhuang.ext.interchan.protocol.InsertInto; -import tech.mhuang.ext.interchan.redis.commands.IRedisExtCommands; import tech.mhuang.ext.spring.util.DataUtil; import tech.mhuang.interchan.protocol.sso.sysfunvisturl.SyChanmgfunExcludeUrlDTO; import tech.mhuang.interchan.protocol.sso.sysfunvisturl.SyChanmgfunVistUrlmAddDTO; @@ -21,6 +20,7 @@ import tech.mhuang.interchan.sso.sysfunvisturl.entity.SyChanmgfunExcludeUrl; import tech.mhuang.interchan.sso.sysfunvisturl.entity.SyChanmgfunVistUrlm; import tech.mhuang.interchan.sso.sysfunvisturl.mapper.SyChanmgfunVistUrlmMapper; import tech.mhuang.interchan.sso.sysfunvisturl.service.ISyChanmgfunVistUrlService; +import tech.mhuang.interchan.sso.sysfunvisturl.util.SysFunvisturlRedisOperator; import tech.mhuang.interchan.sso.sysrole.entity.SysRole; import tech.mhuang.interchan.sso.sysuser.entity.SysUser; @@ -45,10 +45,10 @@ public class SyChanmgfunVistUrlService private BaseIdeable snowflake; @Autowired - private IRedisExtCommands redisExtCommands; + private SyChanmgfunVistUrlmMapper syChanmgfunVistUrlmMapper; @Autowired - private SyChanmgfunVistUrlmMapper syChanmgfunVistUrlmMapper; + private SysFunvisturlRedisOperator syChanmgfunRedisOperator; /** * @param funid @@ -133,7 +133,7 @@ public class SyChanmgfunVistUrlService if (CollectionUtil.isNotEmpty(urls)) { Map datas = urls.parallelStream().collect(Collectors.groupingBy(SyChanmgfunExcludeUrl::getType)); - this.redisExtCommands.hmset(AuthConstant.AUTH_DICT_KEY, datas); + syChanmgfunRedisOperator.cacheData(AuthConstant.AUTH_DICT_KEY, datas); } return vos; } @@ -158,9 +158,7 @@ public class SyChanmgfunVistUrlService */ @Override public void setVistUrlPower(String userid) { - //检查路径与权限问题 - String cacheKey = AuthConstant.USER_VIST_URL_CACHEKEY; - List vistUrls = this.redisExtCommands.hgetList(cacheKey, userid, SyChanmgfunVistUrlm.class); + List vistUrls = syChanmgfunRedisOperator.getVistUrls(userid); if (CollectionUtil.isEmpty(vistUrls)) { //不存在的时候设置权限 setVistUrlPowerNow(userid); @@ -175,8 +173,7 @@ public class SyChanmgfunVistUrlService Map params = urlms.parallelStream().collect(Collectors.toMap((k) -> { return userId.concat("-").concat(k.getUrl()); }, v -> v, (oldValue, newValue) -> oldValue)); - this.redisExtCommands.hmset(cacheKey, params); - + syChanmgfunRedisOperator.cacheData(cacheKey, params); } /** diff --git a/inter-boot-sso/src/main/java/tech/mhuang/interchan/sso/sysfunvisturl/util/SysFunvisturlRedisOperator.java b/inter-boot-sso/src/main/java/tech/mhuang/interchan/sso/sysfunvisturl/util/SysFunvisturlRedisOperator.java new file mode 100644 index 0000000..24cea4d --- /dev/null +++ b/inter-boot-sso/src/main/java/tech/mhuang/interchan/sso/sysfunvisturl/util/SysFunvisturlRedisOperator.java @@ -0,0 +1,51 @@ +package tech.mhuang.interchan.sso.sysfunvisturl.util; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; +import tech.mhuang.ext.interchan.auth.constant.AuthConstant; +import tech.mhuang.ext.interchan.redis.commands.IRedisExtCommands; +import tech.mhuang.interchan.sso.sysfunvisturl.entity.SyChanmgfunVistUrlm; + +import java.util.List; +import java.util.Map; + +/** + * @ClassName: SysUserRoleRedisOperator + * @Author: Ever + * @Description: 用户角色缓存类 + * @Date: 2019/12/24 19:32 + * @Version: 1.0 + */ +@Component +public class SysFunvisturlRedisOperator { + + private final IRedisExtCommands redisExtCommands; + + @Autowired + public SysFunvisturlRedisOperator(IRedisExtCommands redisExtCommands) { + this.redisExtCommands = redisExtCommands; + } + + /** + * 缓存权限 + * + * @param key key + * @param dataMap value + */ + public void cacheData(String key, Map dataMap) { + redisExtCommands.hmset(key, dataMap); + } + + /** + * 根据用户ID获取缓存URL + * + * @param userId 用户ID + * @return 缓存URL + */ + public List getVistUrls(String userId) { + //检查路径与权限问题 + String cacheKey = AuthConstant.USER_VIST_URL_CACHEKEY; + return redisExtCommands.hgetList(cacheKey, userId, SyChanmgfunVistUrlm.class); + } + +} diff --git a/inter-boot-sso/src/main/java/tech/mhuang/interchan/sso/sysuser/service/impl/SysUserServiceImpl.java b/inter-boot-sso/src/main/java/tech/mhuang/interchan/sso/sysuser/service/impl/SysUserServiceImpl.java index 9f66287..d5c5c28 100644 --- a/inter-boot-sso/src/main/java/tech/mhuang/interchan/sso/sysuser/service/impl/SysUserServiceImpl.java +++ b/inter-boot-sso/src/main/java/tech/mhuang/interchan/sso/sysuser/service/impl/SysUserServiceImpl.java @@ -15,7 +15,6 @@ import tech.mhuang.ext.interchan.protocol.InsertInto; import tech.mhuang.ext.interchan.protocol.Result; import tech.mhuang.ext.interchan.protocol.data.Page; import tech.mhuang.ext.interchan.protocol.data.PageVO; -import tech.mhuang.ext.interchan.redis.commands.IRedisExtCommands; import tech.mhuang.ext.spring.util.DataUtil; import tech.mhuang.interchan.protocol.sso.UserDTO; import tech.mhuang.interchan.protocol.sso.sysuser.*; @@ -29,6 +28,7 @@ import tech.mhuang.interchan.sso.sysuser.entity.SysUser; import tech.mhuang.interchan.sso.sysuser.mapper.SysUserMapper; import tech.mhuang.interchan.sso.sysuser.mapper.SysUserRecordMapper; import tech.mhuang.interchan.sso.sysuser.service.ISysUserService; +import tech.mhuang.interchan.sso.sysuser.util.UserRedisOperator; import tech.mhuang.interchan.sso.util.AES; import tech.mhuang.interchan.sso.util.MD5; @@ -48,11 +48,8 @@ public class SysUserServiceImpl extends BaseServiceImpl impleme private BaseIdeable snowflake; @Autowired - private IRedisExtCommands redisExtCommands; + UserRedisOperator userRedisOperator; - private final static String SYUSER_REDIS_MOBILE_TOUSERID_PREKEY = "syur_moblie_uid_"; - - private final static String SYUSER_REDIS_USERID_TO_USER_PREKEY = "syur_user_uid_"; private final static Logger logger = LoggerFactory.getLogger(SysUserServiceImpl.class); @@ -86,7 +83,7 @@ public class SysUserServiceImpl extends BaseServiceImpl impleme @Override public void saveUser(SysUserAddDTO sysUserAddDTO) { - String userId = redisExtCommands.hget(SYUSER_REDIS_MOBILE_TOUSERID_PREKEY, sysUserAddDTO.getMobilephone()); + String userId = userRedisOperator.getUserId(sysUserAddDTO.getMobilephone()); if (StringUtil.isNotBlank(userId)) { throw new BusinessException(Result.SYS_RESULT_FAILD, environment.getProperty("chan_mobile_repeat")); @@ -424,7 +421,7 @@ public class SysUserServiceImpl extends BaseServiceImpl impleme SysUser sysuser = null; try { //从缓存中加载用户信息 - sysuser = redisExtCommands.hget(SYUSER_REDIS_USERID_TO_USER_PREKEY, userId, SysUser.class); + sysuser = userRedisOperator.getSysUserByUserId(userId); } catch (Exception e) { logger.error("使用userId从缓存中获取用户信息失败,userId为:{}", userId, e); } @@ -439,7 +436,7 @@ public class SysUserServiceImpl extends BaseServiceImpl impleme */ private String getUserIdByMobileFromCache(String mobilephone) { try { - return redisExtCommands.hget(SYUSER_REDIS_MOBILE_TOUSERID_PREKEY, mobilephone); + return userRedisOperator.getUserId(mobilephone); } catch (Exception e) { logger.error("getUserIdByMobileFromCache 获取用户缓存信息失败,手机号为:{}", mobilephone, e); } @@ -452,7 +449,7 @@ public class SysUserServiceImpl extends BaseServiceImpl impleme * @return: String */ public String getSyuserRedisMobileToUserIdKey(String mobilephone) { - return SYUSER_REDIS_MOBILE_TOUSERID_PREKEY + mobilephone; + return UserRedisOperator.SYUSER_REDIS_MOBILE_TOUSERID_PREKEY + mobilephone; } /** @@ -461,7 +458,7 @@ public class SysUserServiceImpl extends BaseServiceImpl impleme * @return: String */ public String getSyuserRedisUserIdToUserKey(String userId) { - return SYUSER_REDIS_USERID_TO_USER_PREKEY + userId; + return UserRedisOperator.SYUSER_REDIS_USERID_TO_USER_PREKEY + userId; } /** @@ -471,8 +468,7 @@ public class SysUserServiceImpl extends BaseServiceImpl impleme */ public boolean setSyuserRedisMobileToUser(String mobilephone, String userId) { try { - return redisExtCommands.hset(SYUSER_REDIS_MOBILE_TOUSERID_PREKEY, - mobilephone, userId, Global.EXPIRE_THIRTY_DAYS); + return userRedisOperator.cacheUser(UserRedisOperator.SYUSER_REDIS_MOBILE_TOUSERID_PREKEY, mobilephone, userId, Global.EXPIRE_THIRTY_DAYS); } catch (Exception e) { logger.error("setSyuserRedisMobileToUser 设置缓存信息失败,手机号为:{}", mobilephone, e); return false; @@ -487,8 +483,7 @@ public class SysUserServiceImpl extends BaseServiceImpl impleme */ public boolean setSyuserRedisUserIdToUser(String userId, SysUser user) { try { - return redisExtCommands.hset(SYUSER_REDIS_USERID_TO_USER_PREKEY, - userId, user, Global.EXPIRE_THIRTY_DAYS); + return userRedisOperator.cacheUser(UserRedisOperator.SYUSER_REDIS_USERID_TO_USER_PREKEY, userId, user, Global.EXPIRE_THIRTY_DAYS); } catch (Exception e) { logger.error("setSyuserRedisUserIdToUser 设置缓存信息失败,用户ID为:{}", userId, e); return false; @@ -497,7 +492,7 @@ public class SysUserServiceImpl extends BaseServiceImpl impleme private SysUser getSyuserRedisUserId(String userId) { try { - return redisExtCommands.hget(SYUSER_REDIS_USERID_TO_USER_PREKEY, userId, SysUser.class); + return userRedisOperator.getSysUserByUserId(userId); } catch (Exception e) { logger.error("getSyuserRedisUserId 获取缓存失败,用户ID为:{}", userId, e); return null; diff --git a/inter-boot-sso/src/main/java/tech/mhuang/interchan/sso/sysuser/util/UserRedisOperator.java b/inter-boot-sso/src/main/java/tech/mhuang/interchan/sso/sysuser/util/UserRedisOperator.java new file mode 100644 index 0000000..b52e9c6 --- /dev/null +++ b/inter-boot-sso/src/main/java/tech/mhuang/interchan/sso/sysuser/util/UserRedisOperator.java @@ -0,0 +1,70 @@ +package tech.mhuang.interchan.sso.sysuser.util; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; +import tech.mhuang.ext.interchan.redis.commands.IRedisExtCommands; +import tech.mhuang.interchan.sso.sysuser.entity.SysUser; + +/** + * @ClassName: SysUserRoleRedisOperator + * @Author: Ever + * @Description: 用户缓存类 + * @Date: 2019/12/24 19:32 + * @Version: 1.0 + */ +@Component +public class UserRedisOperator { + + + private final IRedisExtCommands redisExtCommands; + + @Autowired + public UserRedisOperator(IRedisExtCommands redisExtCommands) { + this.redisExtCommands = redisExtCommands; + } + + /** + * 用户手机号RedisKey前缀 + */ + public static final String SYUSER_REDIS_MOBILE_TOUSERID_PREKEY = "syur_moblie_uid_"; + + /** + * 用户ID获取用户前缀 + */ + public static final String SYUSER_REDIS_USERID_TO_USER_PREKEY = "syur_user_uid_"; + + /** + * 根据手机号获取用户ID + * + * @param mobilePhone 手机号 + * @return 用户ID + */ + public String getUserId(String mobilePhone) { + return redisExtCommands.hget(SYUSER_REDIS_MOBILE_TOUSERID_PREKEY, mobilePhone); + } + + /** + * 根据用户ID获取用户 + * + * @param userId 用户ID + * @return 系统用户 + */ + public SysUser getSysUserByUserId(String userId) { + return redisExtCommands.hget(SYUSER_REDIS_USERID_TO_USER_PREKEY, userId, SysUser.class); + } + + /** + * 缓存用户 + * + * @param prefix 缓存前缀标识 + * @param key KEY + * @param value 用户ID VALUE + * @param seconds 缓存时间(秒) + * @return 是否成功 + */ + public boolean cacheUser(String prefix, String key, Object value, Long seconds) { + return redisExtCommands.hset(prefix, key, value, seconds); + } + + +} diff --git a/inter-boot-sso/src/main/java/tech/mhuang/interchan/sso/sysuserrole/service/impl/SysUserRoleServiceImpl.java b/inter-boot-sso/src/main/java/tech/mhuang/interchan/sso/sysuserrole/service/impl/SysUserRoleServiceImpl.java index c17d808..0bcdc5c 100644 --- a/inter-boot-sso/src/main/java/tech/mhuang/interchan/sso/sysuserrole/service/impl/SysUserRoleServiceImpl.java +++ b/inter-boot-sso/src/main/java/tech/mhuang/interchan/sso/sysuserrole/service/impl/SysUserRoleServiceImpl.java @@ -8,12 +8,12 @@ import tech.mhuang.core.id.BaseIdeable; import tech.mhuang.core.util.StringUtil; import tech.mhuang.ext.interchan.core.service.impl.BaseServiceImpl; import tech.mhuang.ext.interchan.protocol.InsertInto; -import tech.mhuang.ext.interchan.redis.commands.IRedisExtCommands; import tech.mhuang.ext.spring.util.DataUtil; import tech.mhuang.interchan.protocol.sso.sysuserrole.*; import tech.mhuang.interchan.sso.sysuserrole.entity.SysUserRole; import tech.mhuang.interchan.sso.sysuserrole.mapper.SysUserRoleMapper; import tech.mhuang.interchan.sso.sysuserrole.service.ISysUserRoleService; +import tech.mhuang.interchan.sso.sysuserrole.util.SysUserRoleRedisOperator; import java.util.Arrays; import java.util.Date; @@ -36,11 +36,9 @@ public class SysUserRoleServiceImpl extends BaseServiceImpl private BaseIdeable snowflake; @Autowired - private IRedisExtCommands redisExtCommands; + private SysUserRoleRedisOperator sysUserRoleRedisOperator; - public static final String SYSTEM_USER_ROLE_CACHE_KEY = "SYSTEM_USER_ROLE"; - @Autowired public void setMapper(SysUserRoleMapper sysUserRoleMapper) { this.setBaseMapper(sysUserRoleMapper); @@ -149,7 +147,7 @@ public class SysUserRoleServiceImpl extends BaseServiceImpl @Override public void setUserRoleToCache(String userId) { List userRoles = this.queryUserRole(userId); - redisExtCommands.hset(SYSTEM_USER_ROLE_CACHE_KEY, userId, userRoles); + sysUserRoleRedisOperator.cacheUserRole(userId, userRoles); } diff --git a/inter-boot-sso/src/main/java/tech/mhuang/interchan/sso/sysuserrole/util/SysUserRoleRedisOperator.java b/inter-boot-sso/src/main/java/tech/mhuang/interchan/sso/sysuserrole/util/SysUserRoleRedisOperator.java new file mode 100644 index 0000000..6a9534f --- /dev/null +++ b/inter-boot-sso/src/main/java/tech/mhuang/interchan/sso/sysuserrole/util/SysUserRoleRedisOperator.java @@ -0,0 +1,39 @@ +package tech.mhuang.interchan.sso.sysuserrole.util; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; +import tech.mhuang.ext.interchan.redis.commands.IRedisExtCommands; +import tech.mhuang.interchan.protocol.sso.sysuserrole.SysUserRoleDTO; + +import java.util.List; + +/** + * @ClassName: SysUserRoleRedisOperator + * @Author: Ever + * @Description: 用户角色缓存类 + * @Date: 2019/12/24 19:32 + * @Version: 1.0 + */ +@Component +public class SysUserRoleRedisOperator { + private final IRedisExtCommands redisExtCommands; + + @Autowired + public SysUserRoleRedisOperator(IRedisExtCommands redisExtCommands) { + this.redisExtCommands = redisExtCommands; + } + + public static final String SYSTEM_USER_ROLE_CACHE_KEY = "SYSTEM_USER_ROLE"; + + /** + * 缓存用户角色 + * + * @param userId 用户ID key + * @param userRoles 角色与用户 + */ + public void cacheUserRole(String userId, List userRoles) { + redisExtCommands.hset(SYSTEM_USER_ROLE_CACHE_KEY, userId, userRoles); + } + + +} diff --git a/inter-logger-common/src/main/java/tech/mhuang/interchan/logger/WebLoggerAspect.java b/inter-logger-common/src/main/java/tech/mhuang/interchan/logger/WebLoggerAspect.java index 279bee1..6b91ac6 100644 --- a/inter-logger-common/src/main/java/tech/mhuang/interchan/logger/WebLoggerAspect.java +++ b/inter-logger-common/src/main/java/tech/mhuang/interchan/logger/WebLoggerAspect.java @@ -133,7 +133,6 @@ public class WebLoggerAspect { esAsyncLogger.setEsOperatorLogger(esOperatorLogger); esAsyncLogger.setName(properties.getEsKey()); esAsyncLogger.setIndex(application); - esAsyncLogger.setType(application); if (StringUtil.isEmpty(esOperatorLogger.getId())) { esAsyncLogger.setOpType(ESAsyncLoggerOpType.INSERT); } else { diff --git a/inter-logger-common/src/main/java/tech/mhuang/interchan/logger/entity/ESAsyncLogger.java b/inter-logger-common/src/main/java/tech/mhuang/interchan/logger/entity/ESAsyncLogger.java index 0e41ba4..76b5bf4 100644 --- a/inter-logger-common/src/main/java/tech/mhuang/interchan/logger/entity/ESAsyncLogger.java +++ b/inter-logger-common/src/main/java/tech/mhuang/interchan/logger/entity/ESAsyncLogger.java @@ -25,11 +25,6 @@ public class ESAsyncLogger { */ private String index; - /** - * es记录的索引类型 - */ - private String type; - /** * es记录的日志 */ -- Gitee