From d9adbadd9fa14cb7174f6167c438a45e8176fd26 Mon Sep 17 00:00:00 2001 From: SZH <szh_hello@163.com> Date: 星期四, 23 二月 2023 15:22:04 +0800 Subject: [PATCH] 数据库连接池移除druid atomikos分布式事务使用com.mysql.cj.jdbc.MysqlXADataSource --- safePlatfrom-out-web/src/main/java/com/gkhy/safePlatform/config/security/TokenAuthenticationFilter.java | 67 +++++++++++++++++++++++++-------- 1 files changed, 50 insertions(+), 17 deletions(-) diff --git a/safePlatfrom-out-web/src/main/java/com/gkhy/safePlatform/config/security/TokenAuthenticationFilter.java b/safePlatfrom-out-web/src/main/java/com/gkhy/safePlatform/config/security/TokenAuthenticationFilter.java index a6ba791..1316c2a 100644 --- a/safePlatfrom-out-web/src/main/java/com/gkhy/safePlatform/config/security/TokenAuthenticationFilter.java +++ b/safePlatfrom-out-web/src/main/java/com/gkhy/safePlatform/config/security/TokenAuthenticationFilter.java @@ -2,14 +2,17 @@ import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; -import com.gkhy.safePlatform.account.rpc.apimodel.UserAccountService; -import com.gkhy.safePlatform.commons.co.CacheAuthority; -import com.gkhy.safePlatform.commons.co.CacheUser; +import com.gkhy.safePlatform.account.rpc.apimodel.AccountAuthService; +import com.gkhy.safePlatform.account.rpc.apimodel.AccountAuthService; +import com.gkhy.safePlatform.commons.co.ContextCacheAuthority; +import com.gkhy.safePlatform.commons.co.ContextCacheUser; import com.gkhy.safePlatform.commons.enums.RedisKeyEnum; import com.gkhy.safePlatform.commons.enums.ResultCodes; import com.gkhy.safePlatform.commons.exception.BusinessException; +import com.gkhy.safePlatform.commons.utils.RPCUtils; import com.gkhy.safePlatform.commons.utils.StringUtils; import com.gkhy.safePlatform.commons.vo.ResultVO; +import com.gkhy.safePlatform.config.redis.RedisUtils; import org.apache.dubbo.config.annotation.DubboReference; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; @@ -37,7 +40,9 @@ @Autowired private TokenConfig tokenConfig; @DubboReference(check = false) - private UserAccountService userAccountService; + private AccountAuthService userAccountService; + @Autowired + private RedisUtils redisUtils; @@ -54,7 +59,7 @@ chain.doFilter(req, resp); } catch (BusinessException e) { // 返回异常 - this.writeJSON(req, resp, new ResultVO<>(e.getError())); + this.writeJSON(req, resp, new ResultVO<>(e.getCode(),e.getMessage())); } } @@ -74,38 +79,66 @@ // 这里是验证获取权限信息 // 1.从redis中获取对应该用户的权限信息 String accessTokenKey = RedisKeyEnum.authKey(RedisKeyEnum.AUTH_TOKEN, loginUserId); - String o = userAccountService.getValueByKeyFromRedis(accessTokenKey); + Object o = redisUtils.get(accessTokenKey); // 2.token是否存在 if (o == null) { // 是否存在 - throw new BusinessException(ResultCodes.CLIENT_CREDENTIALS_SIGN_INVALID); + throw new BusinessException(ResultCodes.CLIENT_CREDENTIALS_TOKEN_INVALID); }else{ Long userId = Long.valueOf(loginUserId); - CacheUser cacheUser = JSONObject.parseObject(o, CacheUser.class); - assert userId.equals(cacheUser.getUserId()); - if ( !authToken.equals(cacheUser.getAccessToken())) { + ContextCacheUser contextCacheUser = JSONObject.parseObject(o.toString(), ContextCacheUser.class); + assert userId.equals(contextCacheUser.getUid()); + if ( !authToken.equals(contextCacheUser.getAccessToken())) { throw new BusinessException(ResultCodes.CLIENT_CREDENTIALS_TOKEN_INVALID); } // 3.redis获取权限 String authoritiesKey = RedisKeyEnum.authKey(RedisKeyEnum.AUTH_AUTHORITIES, userId); - String oo = userAccountService.getValueByKeyFromRedis(authoritiesKey); + Object oo = redisUtils.get(authoritiesKey); List<GrantedAuthority> authorities = new ArrayList<>(); // 4.redis中是否存在 if (oo != null) { // 5.存在 - List<CacheAuthority> cacheAuthorities = JSONArray.parseArray(oo, CacheAuthority.class); - for (CacheAuthority cacheAuthority: cacheAuthorities) { + List<ContextCacheAuthority> cacheAuthorities = JSONArray.parseArray(oo.toString(), ContextCacheAuthority.class); + for (ContextCacheAuthority cacheAuthority: cacheAuthorities) { authorities.add(new SimpleGrantedAuthority(cacheAuthority.getAuthority())); } }else { // 6.不存在=>数据库查询 - String roleCode = userAccountService.getUserRoleCodeByUserId(userId); + ResultVO<String> rpcResultRole = userAccountService.getUserRoleCodeByUserId(userId); + + if (rpcResultRole == null) { + throw new BusinessException(ResultCodes.RPC_RESULT_NULL); + } + if (!ResultCodes.OK.getCode().equals(rpcResultRole.getCode())) { + throw new BusinessException(rpcResultRole.getCode(), rpcResultRole.getMsg()); + } + if (rpcResultRole.getData() == null) { + throw new BusinessException(ResultCodes.RPC_DATA_NULL); + } + if (!(rpcResultRole.getData() instanceof String)) { + throw new BusinessException(ResultCodes.RPC_DATA_TYPE_NOT_MATCH); + } // role - authorities.add(new SimpleGrantedAuthority("ROLE_" + roleCode)); + authorities.add(new SimpleGrantedAuthority("ROLE_" + rpcResultRole.getData().toString())); // permission - List<String> permissions = userAccountService.getUserPermissionByUserId(userId); + ResultVO<List<String>> rpcResultPermission = userAccountService.getUserPermissionByUserId(userId); + + if (rpcResultPermission == null) { + throw new BusinessException(ResultCodes.RPC_RESULT_NULL); + } + if (!ResultCodes.OK.getCode().equals(rpcResultPermission.getCode())) { + throw new BusinessException(rpcResultRole.getCode(), rpcResultRole.getMsg()); + } + if (rpcResultPermission.getData() == null) { + throw new BusinessException(ResultCodes.RPC_DATA_NULL); + } + if (!(rpcResultPermission.getData() instanceof List)) { + throw new BusinessException(ResultCodes.RPC_DATA_TYPE_NOT_MATCH); + } + + List<String> permissions = RPCUtils.castList(rpcResultPermission.getData(), String.class); for (String permission : permissions) { SimpleGrantedAuthority simpleGrantedAuthority = new SimpleGrantedAuthority(permission); authorities.add(simpleGrantedAuthority); @@ -113,7 +146,7 @@ } // security对象中存入登陆者信息 - return new UsernamePasswordAuthenticationToken(userId,authToken,authorities); + return new UsernamePasswordAuthenticationToken(contextCacheUser,authToken,authorities); } -- Gitblit v1.9.2