|
@@ -0,0 +1,176 @@
|
|
|
+package com.stellvoy.authsdk.gateway.util;
|
|
|
+
|
|
|
+
|
|
|
+import cn.hutool.core.collection.CollUtil;
|
|
|
+import cn.hutool.core.collection.CollectionUtil;
|
|
|
+import cn.hutool.core.exceptions.ValidateException;
|
|
|
+import cn.hutool.core.util.StrUtil;
|
|
|
+import cn.hutool.json.JSONObject;
|
|
|
+import cn.hutool.json.JSONUtil;
|
|
|
+import cn.hutool.jwt.JWT;
|
|
|
+import cn.hutool.jwt.JWTValidator;
|
|
|
+import com.stellvoy.auth.common.domain.PrivilegeRoleDTO;
|
|
|
+import com.stellvoy.common.domain.R;
|
|
|
+import com.stellvoy.common.domain.dto.LoginUserDTO;
|
|
|
+import com.stellvoy.common.exceptions.ForbiddenException;
|
|
|
+import com.stellvoy.common.exceptions.UnauthorizedException;
|
|
|
+import com.stellvoy.common.utils.StringUtils;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.data.redis.core.BoundHashOperations;
|
|
|
+import org.springframework.data.redis.core.StringRedisTemplate;
|
|
|
+import org.springframework.scheduling.annotation.Scheduled;
|
|
|
+import org.springframework.util.AntPathMatcher;
|
|
|
+
|
|
|
+import java.util.*;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+import static com.stellvoy.auth.common.constants.AuthErrorInfo.Code.EXPIRED_TOKEN_CODE;
|
|
|
+import static com.stellvoy.auth.common.constants.AuthErrorInfo.Code.INVALID_TOKEN_CODE;
|
|
|
+import static com.stellvoy.auth.common.constants.AuthErrorInfo.Msg.*;
|
|
|
+import static com.stellvoy.auth.common.constants.JwtConstants.*;
|
|
|
+
|
|
|
+@Slf4j
|
|
|
+public class AuthUtil {
|
|
|
+
|
|
|
+ private Map<String, PrivilegeRoleDTO> privileges = new HashMap<>();
|
|
|
+
|
|
|
+ private Set<String> paths = new HashSet<>();
|
|
|
+
|
|
|
+ private int privilegeVersion;
|
|
|
+
|
|
|
+ private final AntPathMatcher antPathMatcher = new AntPathMatcher();
|
|
|
+ private final JwtSignerHolder jwtSignerHolder;
|
|
|
+ private final StringRedisTemplate stringRedisTemplate;
|
|
|
+ private final BoundHashOperations<String, String, String> hashOps;
|
|
|
+
|
|
|
+ public AuthUtil(JwtSignerHolder jwtSignerHolder, StringRedisTemplate stringRedisTemplate) {
|
|
|
+ this.jwtSignerHolder = jwtSignerHolder;
|
|
|
+ this.stringRedisTemplate = stringRedisTemplate;
|
|
|
+ this.hashOps = stringRedisTemplate.boundHashOps(AUTH_PRIVILEGE_KEY);
|
|
|
+ }
|
|
|
+
|
|
|
+ public R<LoginUserDTO> parseToken(String token) {
|
|
|
+
|
|
|
+ if(StringUtils.isBlank(token)){
|
|
|
+ return R.error(INVALID_TOKEN_CODE, INVALID_TOKEN);
|
|
|
+ }
|
|
|
+ JWT jwt = null;
|
|
|
+ try {
|
|
|
+ jwt = JWT.of(token).setSigner(jwtSignerHolder.getJwtSigner());
|
|
|
+ } catch (Exception e) {
|
|
|
+ return R.error(INVALID_TOKEN_CODE, INVALID_TOKEN);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!jwt.verify()) {
|
|
|
+
|
|
|
+ return R.error(INVALID_TOKEN_CODE, INVALID_TOKEN);
|
|
|
+ }
|
|
|
+
|
|
|
+ try {
|
|
|
+ JWTValidator.of(jwt).validateDate();
|
|
|
+ } catch (ValidateException e) {
|
|
|
+ return R.error(EXPIRED_TOKEN_CODE, EXPIRED_TOKEN);
|
|
|
+ }
|
|
|
+
|
|
|
+ Object userPayload = jwt.getPayload(PAYLOAD_USER_KEY);
|
|
|
+ if (userPayload == null) {
|
|
|
+
|
|
|
+ return R.error(INVALID_TOKEN_CODE, INVALID_TOKEN_PAYLOAD);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ LoginUserDTO userDTO;
|
|
|
+ try {
|
|
|
+ userDTO = ((JSONObject)userPayload).toBean(LoginUserDTO.class);
|
|
|
+ } catch (RuntimeException e) {
|
|
|
+
|
|
|
+ return R.error(INVALID_TOKEN_CODE, INVALID_TOKEN_PAYLOAD);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ return R.ok(userDTO);
|
|
|
+ }
|
|
|
+
|
|
|
+ public void checkAuth(String antPath, R<LoginUserDTO> r){
|
|
|
+
|
|
|
+ String matchPath = findMatchPath(antPath);
|
|
|
+ if(matchPath == null){
|
|
|
+
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ if(!r.success()){
|
|
|
+
|
|
|
+ throw new UnauthorizedException(r.getCode(), r.getMsg());
|
|
|
+ }
|
|
|
+
|
|
|
+ PrivilegeRoleDTO pathPrivilege = findPathPrivilege(matchPath);
|
|
|
+
|
|
|
+
|
|
|
+ Set<Long> requiredRoles = pathPrivilege.getRoles();
|
|
|
+ if (!CollectionUtil.contains(requiredRoles, r.getData().getRoleId())) {
|
|
|
+
|
|
|
+ throw new ForbiddenException(FORBIDDEN);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private String findMatchPath(String antPath){
|
|
|
+ String matchPath = null;
|
|
|
+ for (String pathPattern : paths) {
|
|
|
+ if(antPathMatcher.match(pathPattern, antPath)){
|
|
|
+ matchPath = pathPattern;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return matchPath;
|
|
|
+ }
|
|
|
+
|
|
|
+ private PrivilegeRoleDTO findPathPrivilege(String path){
|
|
|
+ return privileges.get(path);
|
|
|
+ }
|
|
|
+
|
|
|
+ private List<PrivilegeRoleDTO> loadPrivileges(){
|
|
|
+ List<String> values = hashOps.values();
|
|
|
+ if(CollUtil.isEmpty(values)){
|
|
|
+ return Collections.emptyList();
|
|
|
+ }
|
|
|
+ return values.stream()
|
|
|
+ .map(json -> JSONUtil.toBean(json, PrivilegeRoleDTO.class))
|
|
|
+ .collect(Collectors.toList());
|
|
|
+ }
|
|
|
+
|
|
|
+ private int currentVersion() {
|
|
|
+ String version = stringRedisTemplate.opsForValue().get(AUTH_PRIVILEGE_VERSION_KEY);
|
|
|
+ if(StrUtil.isEmpty(version)){
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ return Integer.parseInt(version);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @Scheduled(fixedDelay = 20000)
|
|
|
+ public void refreshTask(){
|
|
|
+
|
|
|
+ int currentVersion = currentVersion();
|
|
|
+ if (currentVersion == this.privilegeVersion) {
|
|
|
+
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ List<PrivilegeRoleDTO> privilegeRoleDTOS = loadPrivileges();
|
|
|
+ if(CollUtil.isEmpty(privilegeRoleDTOS)){
|
|
|
+
|
|
|
+ this.privilegeVersion = currentVersion;
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ Map<String, PrivilegeRoleDTO> map = new HashMap<>();
|
|
|
+ for (PrivilegeRoleDTO p : privilegeRoleDTOS) {
|
|
|
+ map.put(p.getAntPath(), p);
|
|
|
+ this.privileges = map;
|
|
|
+ }
|
|
|
+ this.paths = map.keySet();
|
|
|
+
|
|
|
+ this.privilegeVersion = currentVersion;
|
|
|
+ }
|
|
|
+}
|