|
|
@ -2,6 +2,7 @@ package com.daqing.financial.gateway.config; |
|
|
|
|
|
|
|
|
|
|
|
import com.alibaba.fastjson.JSONObject; |
|
|
|
import com.alibaba.fastjson.JSONObject; |
|
|
|
import com.daqing.financial.gateway.util.JwtUtil; |
|
|
|
import com.daqing.financial.gateway.util.JwtUtil; |
|
|
|
|
|
|
|
import com.daqing.financial.gateway.util.RedisUtil; |
|
|
|
import org.apache.commons.lang3.StringUtils; |
|
|
|
import org.apache.commons.lang3.StringUtils; |
|
|
|
import org.springframework.beans.factory.annotation.Value; |
|
|
|
import org.springframework.beans.factory.annotation.Value; |
|
|
|
import org.springframework.cloud.gateway.filter.GatewayFilterChain; |
|
|
|
import org.springframework.cloud.gateway.filter.GatewayFilterChain; |
|
|
@ -57,10 +58,11 @@ public class ApiGlobalFilter implements GlobalFilter, Ordered { |
|
|
|
//有数据
|
|
|
|
//有数据
|
|
|
|
}else { |
|
|
|
}else { |
|
|
|
//校验token
|
|
|
|
//校验token
|
|
|
|
Long userId = verifyJWT(token); |
|
|
|
//Long userId = verifyJWT(token);
|
|
|
|
if (userId == null){ |
|
|
|
String userId = RedisUtil.get("dq:token:"+token); |
|
|
|
|
|
|
|
if (userId == null || "".equals(userId)){ |
|
|
|
JSONObject message = new JSONObject(); |
|
|
|
JSONObject message = new JSONObject(); |
|
|
|
message.put("message", "请重新登录"); |
|
|
|
message.put("message", "登录已失效,请重新登录"); |
|
|
|
message.put("code", "401"); |
|
|
|
message.put("code", "401"); |
|
|
|
byte[] bits = message.toString().getBytes(StandardCharsets.UTF_8); |
|
|
|
byte[] bits = message.toString().getBytes(StandardCharsets.UTF_8); |
|
|
|
DataBuffer buffer = response.bufferFactory().wrap(bits); |
|
|
|
DataBuffer buffer = response.bufferFactory().wrap(bits); |
|
|
@ -69,7 +71,7 @@ public class ApiGlobalFilter implements GlobalFilter, Ordered { |
|
|
|
return response.writeWith(Mono.just(buffer)); |
|
|
|
return response.writeWith(Mono.just(buffer)); |
|
|
|
} |
|
|
|
} |
|
|
|
//将现在的request,添加当前身份
|
|
|
|
//将现在的request,添加当前身份
|
|
|
|
ServerHttpRequest mutableReq = exchange.getRequest().mutate().header("Authorization-UserId", String.valueOf(userId)).build(); |
|
|
|
ServerHttpRequest mutableReq = exchange.getRequest().mutate().header("Authorization-UserId", userId).build(); |
|
|
|
ServerWebExchange mutableExchange = exchange.mutate().request(mutableReq).build(); |
|
|
|
ServerWebExchange mutableExchange = exchange.mutate().request(mutableReq).build(); |
|
|
|
return chain.filter(mutableExchange); |
|
|
|
return chain.filter(mutableExchange); |
|
|
|
} |
|
|
|
} |
|
|
@ -83,6 +85,7 @@ public class ApiGlobalFilter implements GlobalFilter, Ordered { |
|
|
|
* @return userPhone |
|
|
|
* @return userPhone |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
private Long verifyJWT(String token){ |
|
|
|
private Long verifyJWT(String token){ |
|
|
|
|
|
|
|
String id = RedisUtil.get("dq:token:"+token); |
|
|
|
return JwtUtil.verifyToken(token); |
|
|
|
return JwtUtil.verifyToken(token); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|