Browse Source

更新网关拦截 提示过期/禁用相关信息

pull/118/head
xiaoying 1 year ago
parent
commit
017d33da10
3 changed files with 61 additions and 9 deletions
  1. +42
    -9
      src/main/java/com/tuoheng/gateway/config/GatewayFilterConfig.java
  2. +17
    -0
      src/main/java/com/tuoheng/gateway/model/AuthoritiesDto.java
  3. +2
    -0
      src/main/java/com/tuoheng/gateway/model/ClientUserRoleDto.java

+ 42
- 9
src/main/java/com/tuoheng/gateway/config/GatewayFilterConfig.java View File

import com.alibaba.fastjson.serializer.SerializerFeature; import com.alibaba.fastjson.serializer.SerializerFeature;
import com.auth0.jwt.JWT; import com.auth0.jwt.JWT;
import com.auth0.jwt.interfaces.DecodedJWT; import com.auth0.jwt.interfaces.DecodedJWT;
import com.tuoheng.gateway.model.AuthoritiesDto;
import com.tuoheng.gateway.model.ClientUserRoleDto; import com.tuoheng.gateway.model.ClientUserRoleDto;
import com.tuoheng.gateway.utils.EncryptUtil; import com.tuoheng.gateway.utils.EncryptUtil;
import com.tuoheng.gateway.utils.GatewayUrlPathUtil; import com.tuoheng.gateway.utils.GatewayUrlPathUtil;


import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.*; import java.util.*;
import java.util.stream.Collectors;




@Configuration @Configuration
String token = getToken(exchange); String token = getToken(exchange);
String username = null; String username = null;
Long oUserId = null; Long oUserId = null;
List<String> authorityList = new ArrayList<>();
List<AuthoritiesDto> authorityList = new ArrayList<>();
List<ClientUserRoleDto> clientUserRoleDtoList = new ArrayList<>(); List<ClientUserRoleDto> clientUserRoleDtoList = new ArrayList<>();
if (!StringUtils.isBlank(token)) { if (!StringUtils.isBlank(token)) {
//token数据解析 //token数据解析
DecodedJWT decodedJWT = JWT.decode(token); DecodedJWT decodedJWT = JWT.decode(token);
username = decodedJWT.getClaim(USERNAME).asString(); username = decodedJWT.getClaim(USERNAME).asString();
oUserId = decodedJWT.getClaim(OUSERID).asLong(); oUserId = decodedJWT.getClaim(OUSERID).asLong();
authorityList = decodedJWT.getClaim(SCOPE).asList(String.class);
authorityList = decodedJWT.getClaim(SCOPE).asList(AuthoritiesDto.class);
String str = decodedJWT.getClaim(CLIENTROLELIST).asString(); String str = decodedJWT.getClaim(CLIENTROLELIST).asString();
clientUserRoleDtoList = JSONArray.parseArray(str, ClientUserRoleDto.class);;
clientUserRoleDtoList = JSONArray.parseArray(str, ClientUserRoleDto.class);
} }
//header里封装 Client-Id 信息 //header里封装 Client-Id 信息
String clientId = getClientId(exchange); String clientId = getClientId(exchange);
log.info("clientId is :{}", clientId); log.info("clientId is :{}", clientId);
if(!StringUtils.isEmpty(clientId)){
if (!StringUtils.isEmpty(clientId)) {
//校验过期/禁用给出提示信息
List<AuthoritiesDto> authoritiesDtos = authorityList.stream().filter(item -> item.getAuthority().equals(clientId)).limit(1).collect(Collectors.toList());
if (0 == authoritiesDtos.get(0).getStatus()) {
return disableClientIdMono(exchange);
}
List<ClientUserRoleDto> clientUserRoleDtos = clientUserRoleDtoList.stream().filter(item -> item.getClientId().equals(clientId)).limit(1).collect(Collectors.toList());
if (0 != clientUserRoleDtos.get(0).getStatus()) {
return expireClientIdMono(exchange);
}
String requestUrl = exchange.getRequest().getPath().value(); String requestUrl = exchange.getRequest().getPath().value();
//去除gateway path 前缀 //去除gateway path 前缀
String apiUrl = requestUrl.replace(GatewayUrlPathUtil.getPathByClientId(clientId),"");
String apiUrl = requestUrl.replace(GatewayUrlPathUtil.getPathByClientId(clientId), "");
log.info("requestUrl is :{}; apiUrl is :{}", requestUrl, apiUrl); log.info("requestUrl is :{}; apiUrl is :{}", requestUrl, apiUrl);
List<Integer> roleIds = GatewayUrlPathUtil.getRoleIdByApiUrlPermission(clientId, apiUrl, token); List<Integer> roleIds = GatewayUrlPathUtil.getRoleIdByApiUrlPermission(clientId, apiUrl, token);
log.info("roleIds is :{}", roleIds); log.info("roleIds is :{}", roleIds);
//return invalidClientIdMono(exchange); //return invalidClientIdMono(exchange);
if(roleIds.size() > 0){
if (roleIds.size() > 0) {
//说明这个url 需要一定的角色才可以访问 //说明这个url 需要一定的角色才可以访问
//在不是admin权限的情况下进行校验 //在不是admin权限的情况下进行校验
log.info("该接口存在权限..."); log.info("该接口存在权限...");
if(!authorityList.contains(ADMIN)){
if (!authorityList.contains(ADMIN)) {
//获取用户 client_id 对应的 roleId //获取用户 client_id 对应的 roleId
ClientUserRoleDto clientUserRoleDto = clientUserRoleDtoList.stream().filter(dto -> dto.getClientId().equals(clientId)) ClientUserRoleDto clientUserRoleDto = clientUserRoleDtoList.stream().filter(dto -> dto.getClientId().equals(clientId))
.findFirst().orElse(null); .findFirst().orElse(null);
if(Objects.isNull(clientUserRoleDto)){
if (Objects.isNull(clientUserRoleDto)) {
return forbiddenTokenMono(exchange); return forbiddenTokenMono(exchange);
} }
Integer roleId = clientUserRoleDto.getRoleId(); Integer roleId = clientUserRoleDto.getRoleId();
if(!roleIds.contains(roleId)){
if (!roleIds.contains(roleId)) {
return forbiddenTokenMono(exchange); return forbiddenTokenMono(exchange);
} }
} }
return buildReturnMono(json, exchange, HttpStatus.UNAUTHORIZED); return buildReturnMono(json, exchange, HttpStatus.UNAUTHORIZED);
} }


/**
* 401 禁用
*/
private Mono<Void> disableClientIdMono(ServerWebExchange exchange) {
JSONObject json = new JSONObject();
json.put("code", HttpStatus.UNAUTHORIZED.value());
json.put("msg", "该账号已被禁用,请联系系统管理员");
json.put("data", null);
return buildReturnMono(json, exchange, HttpStatus.UNAUTHORIZED);
}

/**
* 401 过期
*/
private Mono<Void> expireClientIdMono(ServerWebExchange exchange) {
JSONObject json = new JSONObject();
json.put("code", HttpStatus.UNAUTHORIZED.value());
json.put("msg", "系统有效期已过,请联系系统管理员");
json.put("data", null);
return buildReturnMono(json, exchange, HttpStatus.UNAUTHORIZED);
}

/** /**
* 403 未授权的token * 403 未授权的token
*/ */

+ 17
- 0
src/main/java/com/tuoheng/gateway/model/AuthoritiesDto.java View File

package com.tuoheng.gateway.model;

import lombok.Data;

/**
* @Author xiaoying
* @Date 2023/7/3 9:49
*/
@Data
public class AuthoritiesDto {

private String authority;

private Integer status;


}

+ 2
- 0
src/main/java/com/tuoheng/gateway/model/ClientUserRoleDto.java View File



private Integer roleId; private Integer roleId;


private Integer status;

} }

Loading…
Cancel
Save