From 0facef68a06fb36cc1ac12730b076cead36348f9 Mon Sep 17 00:00:00 2001 From: luofl <1442745593@qq.com> Date: 星期日, 23 二月 2025 16:12:02 +0800 Subject: [PATCH] Merge remote-tracking branch 'origin/master' --- springcloud_k8s_panzhihuazhihuishequ/zuul/src/main/java/com/panzhihua/zuul/filters/JWTAuthenticationTokenFilter.java | 95 +++++++++++++++++++++++++++++++++++------------ 1 files changed, 71 insertions(+), 24 deletions(-) diff --git a/springcloud_k8s_panzhihuazhihuishequ/zuul/src/main/java/com/panzhihua/zuul/filters/JWTAuthenticationTokenFilter.java b/springcloud_k8s_panzhihuazhihuishequ/zuul/src/main/java/com/panzhihua/zuul/filters/JWTAuthenticationTokenFilter.java index a0197ba..70ef665 100644 --- a/springcloud_k8s_panzhihuazhihuishequ/zuul/src/main/java/com/panzhihua/zuul/filters/JWTAuthenticationTokenFilter.java +++ b/springcloud_k8s_panzhihuazhihuishequ/zuul/src/main/java/com/panzhihua/zuul/filters/JWTAuthenticationTokenFilter.java @@ -9,7 +9,9 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import com.panzhihua.common.model.vos.sangeshenbian.SystemUserVo; import com.panzhihua.common.model.vos.user.SysAppConfigVO; +import com.panzhihua.common.service.sangeshenbian.SystemUserService; import com.panzhihua.common.utlis.*; import org.springframework.context.ApplicationContext; import org.springframework.data.redis.core.StringRedisTemplate; @@ -40,6 +42,7 @@ public class JWTAuthenticationTokenFilter implements Filter { private StringRedisTemplate stringRedisTemplate; private UserService userService; + private SystemUserService systemUserService; @Override public void init(FilterConfig filterConfig) throws ServletException { @@ -65,6 +68,7 @@ stringRedisTemplate = ctx.getBean(StringRedisTemplate.class); ValueOperations<String, String> valueOperations = stringRedisTemplate.opsForValue(); userService = ctx.getBean(UserService.class); + systemUserService = ctx.getBean(SystemUserService.class); String requestURI = request.getRequestURI(); boolean login = requestURI.contains("login"); boolean union = requestURI.contains("huacheng-union-applets"); @@ -187,6 +191,47 @@ } valueOperations.set(roleAppletKey, JSONArray.toJSONString(authorities), 24, TimeUnit.HOURS); } + } else if(21 == type){ + //三个身边 + String key = SecurityConstants.ROLE_USER + "sangeshenbian:" + userId; + Boolean aBoolean = stringRedisTemplate.hasKey(key); + R<SystemUserVo> r = systemUserService.getUserById(userId); + if (aBoolean) { + String roles = valueOperations.get(key); + authorities = JSONArray.parseArray(roles, SimpleGrantedAuthority.class); + } else { + SystemUserVo data = r.getData(); + if (!ObjectUtils.isEmpty(data)) { + authorities.add(new SimpleGrantedAuthority(data.getSystemRoleId().toString())); + valueOperations.set(key, JSONArray.toJSONString(authorities), 24, TimeUnit.HOURS); + } + } + + UsernamePasswordAuthenticationToken authentication = + new UsernamePasswordAuthenticationToken(userId, userId, authorities);// 主要使用权限 账户 密码 不重要 + SecurityContextHolder.getContext().setAuthentication(authentication); + safeboxRequestWrapper.addHeader(TokenConstant.TOKEN_LOGOUT, tokenHeader); + // 登录用户的所有信息 + String userKey = UserConstants.LOGIN_USER_INFO + "sangeshenbian:" + userId; + Boolean hasKeyLoginUserInfo = stringRedisTemplate.hasKey(userKey); + if (hasKeyLoginUserInfo) { + String userInfo = valueOperations.get(userKey); + byte[] encrypt = AES.encrypt(userInfo, Constants.AES_KEY); + String hexStr = AES.parseByte2HexStr(encrypt); + safeboxRequestWrapper.addHeader(TokenConstant.TOKEN_USERINFO, hexStr); + } else { + if (!R.isOk(r)) { + ResultUtil.responseJson(response, R.fail(HttpStatus.ERROR, "登录用户信息查询失败")); + return; + } + SystemUserVo data = r.getData(); + String userInfo = JSONObject.toJSONString(data); + valueOperations.set(userKey, userInfo, 24, TimeUnit.HOURS); + byte[] encrypt = AES.encrypt(userInfo, Constants.AES_KEY); + String hexStr = AES.parseByte2HexStr(encrypt); + safeboxRequestWrapper.addHeader(TokenConstant.TOKEN_USERINFO, hexStr); + } + } else { String key = SecurityConstants.ROLE_USER + userId; Boolean aBoolean = stringRedisTemplate.hasKey(key); @@ -204,32 +249,34 @@ valueOperations.set(key, JSONArray.toJSONString(authorities), 24, TimeUnit.HOURS); } } - } - UsernamePasswordAuthenticationToken authentication = - new UsernamePasswordAuthenticationToken(userId, userId, authorities);// 主要使用权限 账户 密码 不重要 - SecurityContextHolder.getContext().setAuthentication(authentication); - safeboxRequestWrapper.addHeader(TokenConstant.TOKEN_LOGOUT, tokenHeader); - // 登录用户的所有信息 - String userKey = UserConstants.LOGIN_USER_INFO + userId; - Boolean hasKeyLoginUserInfo = stringRedisTemplate.hasKey(userKey); - if (hasKeyLoginUserInfo) { - String userInfo = valueOperations.get(userKey); - byte[] encrypt = AES.encrypt(userInfo, Constants.AES_KEY); - String hexStr = AES.parseByte2HexStr(encrypt); - safeboxRequestWrapper.addHeader(TokenConstant.TOKEN_USERINFO, hexStr); - } else { - R<LoginUserInfoVO> r = userService.getUserInfoByUserId(userId); - if (!R.isOk(r)) { - ResultUtil.responseJson(response, R.fail(HttpStatus.ERROR, "登录用户信息查询失败")); - return; + + UsernamePasswordAuthenticationToken authentication = + new UsernamePasswordAuthenticationToken(userId, userId, authorities);// 主要使用权限 账户 密码 不重要 + SecurityContextHolder.getContext().setAuthentication(authentication); + safeboxRequestWrapper.addHeader(TokenConstant.TOKEN_LOGOUT, tokenHeader); + // 登录用户的所有信息 + String userKey = UserConstants.LOGIN_USER_INFO + userId; + Boolean hasKeyLoginUserInfo = stringRedisTemplate.hasKey(userKey); + if (hasKeyLoginUserInfo) { + String userInfo = valueOperations.get(userKey); + byte[] encrypt = AES.encrypt(userInfo, Constants.AES_KEY); + String hexStr = AES.parseByte2HexStr(encrypt); + safeboxRequestWrapper.addHeader(TokenConstant.TOKEN_USERINFO, hexStr); + } else { + R<LoginUserInfoVO> r = userService.getUserInfoByUserId(userId); + if (!R.isOk(r)) { + ResultUtil.responseJson(response, R.fail(HttpStatus.ERROR, "登录用户信息查询失败")); + return; + } + LoginUserInfoVO data = r.getData(); + String userInfo = JSONObject.toJSONString(data); + valueOperations.set(userKey, userInfo, 24, TimeUnit.HOURS); + byte[] encrypt = AES.encrypt(userInfo, Constants.AES_KEY); + String hexStr = AES.parseByte2HexStr(encrypt); + safeboxRequestWrapper.addHeader(TokenConstant.TOKEN_USERINFO, hexStr); } - LoginUserInfoVO data = r.getData(); - String userInfo = JSONObject.toJSONString(data); - valueOperations.set(userKey, userInfo, 24, TimeUnit.HOURS); - byte[] encrypt = AES.encrypt(userInfo, Constants.AES_KEY); - String hexStr = AES.parseByte2HexStr(encrypt); - safeboxRequestWrapper.addHeader(TokenConstant.TOKEN_USERINFO, hexStr); } + } else { // 小程序无需登录也可访问地址列表 List<String> noLoginUrl = new ArrayList<>(); -- Gitblit v1.7.1