From 0750a66984bb12e672ce6e3bcfad12932a32fa08 Mon Sep 17 00:00:00 2001 From: Helius <wangdoubleone@gmail.com> Date: Wed, 17 Jun 2020 15:15:26 +0800 Subject: [PATCH] modify --- src/main/java/com/xcong/excoin/configurations/security/TokenFilter.java | 5 +++-- src/main/java/com/xcong/excoin/common/system/controller/LoginController.java | 4 ++-- src/main/java/com/xcong/excoin/common/LoginUserUtils.java | 15 +++++++++++++-- 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/xcong/excoin/common/LoginUserUtils.java b/src/main/java/com/xcong/excoin/common/LoginUserUtils.java index 8f2400a..117b8ff 100644 --- a/src/main/java/com/xcong/excoin/common/LoginUserUtils.java +++ b/src/main/java/com/xcong/excoin/common/LoginUserUtils.java @@ -1,7 +1,12 @@ package com.xcong.excoin.common; +import cn.hutool.core.util.StrUtil; +import com.alibaba.fastjson.JSONObject; +import com.xcong.excoin.common.contants.AppContants; import com.xcong.excoin.common.exception.GlobalException; import com.xcong.excoin.modules.member.entity.MemberEntity; +import com.xcong.excoin.utils.RedisUtils; +import com.xcong.excoin.utils.SpringContextHolder; import lombok.extern.slf4j.Slf4j; import org.springframework.http.HttpRequest; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; @@ -35,8 +40,14 @@ } public static void resetAppLoginUser(MemberEntity memberEntity) { - Authentication authentication = new UsernamePasswordAuthenticationToken(memberEntity, getAppLoginUserToken(), new ArrayList<>()); - SecurityContextHolder.getContext().setAuthentication(authentication); + String token = getAppLoginUserToken(); + RedisUtils redisUtils = SpringContextHolder.getBean(RedisUtils.class); + String jsonStr = redisUtils.getString(AppContants.PC_LOGIN_PREFIX + token); + if (StrUtil.isNotBlank(jsonStr)) { + redisUtils.set(AppContants.PC_LOGIN_PREFIX + token, JSONObject.toJSONString(memberEntity)); + } else { + redisUtils.set(AppContants.APP_LOGIN_PREFIX + token, JSONObject.toJSONString(memberEntity)); + } } /** diff --git a/src/main/java/com/xcong/excoin/common/system/controller/LoginController.java b/src/main/java/com/xcong/excoin/common/system/controller/LoginController.java index e2bb955..d8bfd99 100644 --- a/src/main/java/com/xcong/excoin/common/system/controller/LoginController.java +++ b/src/main/java/com/xcong/excoin/common/system/controller/LoginController.java @@ -90,13 +90,13 @@ redisUtils.del(AppContants.PC_LOGIN_PREFIX + redisUtils.getString(redisMember)); } } - redisUtils.set(redisToken, JSONObject.toJSONString(loginUserBean), applicationProperties.getRedisExpire()); + redisUtils.set(redisToken, JSONObject.toJSONString(loginUserBean.getMemberEntity()), applicationProperties.getRedisExpire()); redisUtils.set(redisMember, token); Map<String, Object> authInfo = new HashMap<>(); // 开启debug模式,则将加密后的token返回 if (applicationProperties.isDebug()) { authInfo.put("token", token); - authInfo.put("rsaToken", generateAsaToken(token)); + authInfo.put("rsaToken", AppContants.TOKEN_START_WITH + generateAsaToken(token)); authInfo.put("user", loginUserBean); } else { authInfo.put("token", token); diff --git a/src/main/java/com/xcong/excoin/configurations/security/TokenFilter.java b/src/main/java/com/xcong/excoin/configurations/security/TokenFilter.java index 8b98b0b..2284d12 100644 --- a/src/main/java/com/xcong/excoin/configurations/security/TokenFilter.java +++ b/src/main/java/com/xcong/excoin/configurations/security/TokenFilter.java @@ -9,6 +9,7 @@ import com.xcong.excoin.common.system.bean.LoginUserBean; import com.xcong.excoin.configurations.properties.ApplicationProperties; import com.xcong.excoin.configurations.properties.SecurityProperties; +import com.xcong.excoin.modules.member.entity.MemberEntity; import com.xcong.excoin.utils.RedisUtils; import com.xcong.excoin.utils.SpringContextHolder; import lombok.extern.slf4j.Slf4j; @@ -54,8 +55,8 @@ String loginStr = (String) redisUtils.get(redisKey); if (StrUtil.isNotBlank(loginStr)) { - LoginUserBean loginUser = JSONObject.parseObject(loginStr, LoginUserBean.class); - Authentication authentication = new UsernamePasswordAuthenticationToken(loginUser.getMemberEntity(), token, new ArrayList<>()); + MemberEntity loginUser = JSONObject.parseObject(loginStr, MemberEntity.class); + Authentication authentication = new UsernamePasswordAuthenticationToken(loginUser, token, new ArrayList<>()); SecurityContextHolder.getContext().setAuthentication(authentication); redisUtils.expire(redisKey, 300000); } else { -- Gitblit v1.9.1