Skip to content

Commit

Permalink
add little app gateway check function
Browse files Browse the repository at this point in the history
  • Loading branch information
pepsicola committed Nov 5, 2024
1 parent 48d4153 commit 82c1908
Showing 1 changed file with 17 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
package com.sl.gateway.filter;

import cn.hutool.core.map.MapUtil;
import com.itheima.auth.sdk.dto.AuthUserInfoDTO;
import com.sl.gateway.config.MyConfig;
import com.sl.gateway.properties.JwtProperties;
import com.sl.transport.common.constant.Constants;
import com.sl.transport.common.util.JwtUtils;
import com.sl.transport.common.util.ObjectUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Map;

/**
* 用户端token拦截处理
Expand All @@ -32,7 +36,19 @@ public GatewayFilter apply(Object config) {
@Override
public AuthUserInfoDTO check(String token) {
// 普通用户的token没有对接权限系统,需要自定实现
return null;
// 鉴权逻辑在用户端自行实现 网关统一放行
log.info("开始解析token {}", token);
Map<String, Object> claims = JwtUtils.checkToken(token, jwtProperties.getPublicKey());
if (ObjectUtil.isEmpty(claims)) {
//token失效
return null;
}

Long userId = MapUtil.get(claims, Constants.GATEWAY.USER_ID, Long.class);
//token解析成功,放行
AuthUserInfoDTO authUserInfoDTO = new AuthUserInfoDTO();
authUserInfoDTO.setUserId(userId);
return authUserInfoDTO;
}

@Override
Expand Down

0 comments on commit 82c1908

Please sign in to comment.