提交 4c39c366 authored 作者: 李秋林's avatar 李秋林

1、切换redis数据源;2、token解析改为link项目方式,通用token

上级 a1d3e20c
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
<parent> <parent>
<groupId>com.wangxiaolu</groupId> <groupId>com.wangxiaolu</groupId>
<artifactId>wangxiaolu-promotion-parent</artifactId> <artifactId>wangxiaolu-promotion-parent</artifactId>
<version>0.0.1</version> <version>0.0.2</version>
</parent> </parent>
<groupId>com.wangxiaolu</groupId> <groupId>com.wangxiaolu</groupId>
......
...@@ -9,4 +9,6 @@ import com.alibaba.fastjson.JSONObject; ...@@ -9,4 +9,6 @@ import com.alibaba.fastjson.JSONObject;
*/ */
public interface UserDataService { public interface UserDataService {
JSONObject getUserByToken(String token); JSONObject getUserByToken(String token);
boolean hasKeyToken(String tokenKey);
} }
...@@ -2,6 +2,7 @@ package com.promotion.gateway.data.impl; ...@@ -2,6 +2,7 @@ package com.promotion.gateway.data.impl;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.promotion.gateway.data.UserDataService; import com.promotion.gateway.data.UserDataService;
import com.wangxiaolu.promotion.common.redis.RedisKeys;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.RedisTemplate;
...@@ -26,4 +27,9 @@ public class UserDataServiceImpl implements UserDataService { ...@@ -26,4 +27,9 @@ public class UserDataServiceImpl implements UserDataService {
String val = vo.get(token); String val = vo.get(token);
return JSONObject.parseObject(val); return JSONObject.parseObject(val);
} }
@Override
public boolean hasKeyToken(String tokenKey) {
return redisTemplate.hasKey(RedisKeys.UserKeys.WXL_LINK_TOKEN.getKey() + tokenKey);
}
} }
package com.promotion.gateway.filter; package com.promotion.gateway.filter;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.StringUtils; import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.promotion.gateway.data.UserDataService; import com.promotion.gateway.data.UserDataService;
import com.wangxiaolu.promotion.common.redis.RedisKeys; import com.wangxiaolu.promotion.common.util.JwtTokenUtils;
import com.wangxiaolu.promotion.result.basedata.R; import com.wangxiaolu.promotion.result.basedata.R;
import com.wangxiaolu.promotion.result.basedata.RCode; import com.wangxiaolu.promotion.result.basedata.RCode;
import com.wangxiaolu.promotion.result.basedata.StatusCode; import com.wangxiaolu.promotion.result.basedata.StatusCode;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.gateway.filter.GatewayFilterChain; import org.springframework.cloud.gateway.filter.GatewayFilterChain;
...@@ -15,7 +15,6 @@ import org.springframework.cloud.gateway.filter.GlobalFilter; ...@@ -15,7 +15,6 @@ import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered; import org.springframework.core.Ordered;
import org.springframework.core.io.buffer.DataBuffer; import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse; import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange; import org.springframework.web.server.ServerWebExchange;
...@@ -25,7 +24,6 @@ import reactor.core.publisher.Mono; ...@@ -25,7 +24,6 @@ import reactor.core.publisher.Mono;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Objects;
/** /**
* @author : liqiulin * @author : liqiulin
...@@ -43,7 +41,7 @@ public class AuthGlobalFilter implements GlobalFilter, Ordered { ...@@ -43,7 +41,7 @@ public class AuthGlobalFilter implements GlobalFilter, Ordered {
private final List<String> whiteUrls = Arrays.asList("/login", "/enroll", "/sms/send/ver_code"); private final List<String> whiteUrls = Arrays.asList("/login", "/enroll", "/sms/send/ver_code");
@Autowired @Autowired
UserDataService userDataService; private UserDataService userDataService;
@Override @Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) { public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
...@@ -62,13 +60,25 @@ public class AuthGlobalFilter implements GlobalFilter, Ordered { ...@@ -62,13 +60,25 @@ public class AuthGlobalFilter implements GlobalFilter, Ordered {
} }
// 3、验证token // 3、验证token
JSONObject userJson = userDataService.getUserByToken(RedisKeys.UserKeys.TEMPORARY_TOKEN.getKey() + token); Claims claims = JwtTokenUtils.parseToken(token);
if (Objects.isNull(userJson)) { if (claims == null)
log.info("访问URL:{},token登录错误:{}", url,token); {
return loginError(exchange,RCode.NOT_LOGIN_TIMEOUT_ERROR); return loginError(exchange,RCode.NOT_LOGIN_TIMEOUT_ERROR);
} }
ServerHttpRequest httpRequest = exchange.getRequest().mutate().header("loginId", userJson.getString("id")).header("loginQcId", userJson.getString("qcId")).build(); String userkey = JwtTokenUtils.getUserKey(claims);
return chain.filter(exchange.mutate().request(httpRequest).build()); boolean islogin = userDataService.hasKeyToken(userkey);
if (!islogin)
{
return loginError(exchange,RCode.NOT_LOGIN_TIMEOUT_ERROR);
}
String userid = JwtTokenUtils.getUserId(claims);
String username = JwtTokenUtils.getUserName(claims);
if (StringUtils.isBlank(userid) || StringUtils.isBlank(username))
{
return loginError(exchange,RCode.NOT_LOGIN_TIMEOUT_ERROR);
}
return chain.filter(exchange);
} }
@Override @Override
......
...@@ -5,10 +5,10 @@ spring: ...@@ -5,10 +5,10 @@ spring:
username: NvqGbJXH username: NvqGbJXH
password: D9Y@FR,84B*$MD^A36&m password: D9Y@FR,84B*$MD^A36&m
redis: redis:
port: 21101 port: 6379
host: bj-crs-oyzhz3c6.sql.tencentcdb.com host: 192.168.100.40
database: 0 database: 0
password: u)R3jrHk(qwt~mv$Tg=U password: QjL6H5nH
main: main:
web-application-type: reactive web-application-type: reactive
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论