|
@@ -2,22 +2,31 @@ package shop.alien.gateway.config;
|
|
|
|
|
|
import com.alibaba.cloud.commons.lang.StringUtils;
|
|
import com.alibaba.cloud.commons.lang.StringUtils;
|
|
import com.alibaba.fastjson.JSONObject;
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
|
+import com.fasterxml.jackson.databind.ObjectMapper;
|
|
import io.jsonwebtoken.Claims;
|
|
import io.jsonwebtoken.Claims;
|
|
import io.jsonwebtoken.ExpiredJwtException;
|
|
import io.jsonwebtoken.ExpiredJwtException;
|
|
import lombok.Data;
|
|
import lombok.Data;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
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;
|
|
import org.springframework.cloud.gateway.filter.GlobalFilter;
|
|
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.MediaType;
|
|
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;
|
|
import reactor.core.publisher.Flux;
|
|
import reactor.core.publisher.Flux;
|
|
import reactor.core.publisher.Mono;
|
|
import reactor.core.publisher.Mono;
|
|
|
|
+import shop.alien.entity.store.StoreUser;
|
|
|
|
+import shop.alien.gateway.mapper.StoreUserMapper;
|
|
|
|
+import shop.alien.util.common.JwtUtil;
|
|
|
|
|
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
import java.nio.charset.StandardCharsets;
|
|
import java.nio.charset.StandardCharsets;
|
|
import java.util.Arrays;
|
|
import java.util.Arrays;
|
|
import java.util.HashMap;
|
|
import java.util.HashMap;
|
|
@@ -38,6 +47,12 @@ public class JwtTokenFilter implements GlobalFilter, Ordered {
|
|
@Value("${jwt.skip-auth-urls}")
|
|
@Value("${jwt.skip-auth-urls}")
|
|
private String[] skipAuthUrls;
|
|
private String[] skipAuthUrls;
|
|
|
|
|
|
|
|
+ @Autowired
|
|
|
|
+ private BaseRedisService baseRedisService;
|
|
|
|
+
|
|
|
|
+ @Autowired
|
|
|
|
+ private StoreUserMapper storeUserMapper;
|
|
|
|
+
|
|
/**
|
|
/**
|
|
* 过滤器
|
|
* 过滤器
|
|
*
|
|
*
|
|
@@ -48,14 +63,24 @@ public class JwtTokenFilter implements GlobalFilter, Ordered {
|
|
@Override
|
|
@Override
|
|
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
|
|
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
|
|
String url = exchange.getRequest().getURI().getPath();
|
|
String url = exchange.getRequest().getURI().getPath();
|
|
|
|
+ log.info("====================>path: " + url);
|
|
|
|
+// if (url.startsWith("second/") || url.startsWith("store/")) {
|
|
|
|
+// url = url.substring(url.indexOf("/"), url.length() + 1);
|
|
|
|
+// }
|
|
//跳过不需要验证的路径
|
|
//跳过不需要验证的路径
|
|
if (null != skipAuthUrls && Arrays.asList(skipAuthUrls).contains(url)) {
|
|
if (null != skipAuthUrls && Arrays.asList(skipAuthUrls).contains(url)) {
|
|
return chain.filter(exchange);
|
|
return chain.filter(exchange);
|
|
}
|
|
}
|
|
- Map<String, Object> map = new HashMap<>();
|
|
|
|
- int errorType = 0;
|
|
|
|
|
|
+ if (url.startsWith("/store/webjars") || url.startsWith("/second/webjars")) {
|
|
|
|
+ return chain.filter(exchange);
|
|
|
|
+ }
|
|
|
|
+
|
|
//获取token
|
|
//获取token
|
|
String token = exchange.getRequest().getHeaders().getFirst("Authorization");
|
|
String token = exchange.getRequest().getHeaders().getFirst("Authorization");
|
|
|
|
+ log.info("====================>token值: " + token);
|
|
|
|
+
|
|
|
|
+ JSONObject map = new JSONObject();
|
|
|
|
+ int errorType = 0;
|
|
ServerHttpResponse resp = exchange.getResponse();
|
|
ServerHttpResponse resp = exchange.getResponse();
|
|
if (StringUtils.isBlank(token)) {
|
|
if (StringUtils.isBlank(token)) {
|
|
//没有token
|
|
//没有token
|
|
@@ -65,7 +90,41 @@ public class JwtTokenFilter implements GlobalFilter, Ordered {
|
|
try {
|
|
try {
|
|
JwtUtil.checkToken(token);
|
|
JwtUtil.checkToken(token);
|
|
Claims claims = JwtUtil.parseJWT(token);
|
|
Claims claims = JwtUtil.parseJWT(token);
|
|
- JSONObject js = JSONObject.parseObject(claims.get("sub").toString());
|
|
|
|
|
|
+ JSONObject tokenData = JSONObject.parseObject(claims.get("sub").toString());
|
|
|
|
+ String deviceType = tokenData.getString("userType");
|
|
|
|
+ String phone = tokenData.getString("phone");
|
|
|
|
+ String redisKey;
|
|
|
|
+ //区分
|
|
|
|
+ if ("web".equals(deviceType)) {
|
|
|
|
+ //管理端单设备登录
|
|
|
|
+// redisKey = deviceType + "_" + tokenInfo.getClaim("userName").asString();
|
|
|
|
+ //不限制
|
|
|
|
+ return chain.filter(exchange);
|
|
|
|
+ } else {
|
|
|
|
+ redisKey = deviceType + "_" + phone;
|
|
|
|
+ }
|
|
|
|
+ String redisVal = baseRedisService.getString(redisKey);
|
|
|
|
+ if (StringUtils.isEmpty(redisVal) || !token.equals(redisVal)) {
|
|
|
|
+ //判断程序是否为用户禁用
|
|
|
|
+ StoreUser storeUser = storeUserMapper.selectOne(new LambdaQueryWrapper<StoreUser>().eq(StoreUser::getPhone, phone));
|
|
|
|
+ if (storeUser.getStatus() == 1) {
|
|
|
|
+ map.put("msg", "你的账号已被禁用");
|
|
|
|
+ //别问, 问就是约定俗成
|
|
|
|
+ map.put("code", 777);
|
|
|
|
+ } else {
|
|
|
|
+ map.put("msg", "用户在别处登录");
|
|
|
|
+ //别问, 问就是约定俗成
|
|
|
|
+ map.put("code", 666);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ map.put("success", false);
|
|
|
|
+
|
|
|
|
+ exchange.getResponse().getHeaders().setContentType(MediaType.APPLICATION_JSON);
|
|
|
|
+ return exchange.getResponse()
|
|
|
|
+ .writeWith(Mono.just(exchange.getResponse()
|
|
|
|
+ .bufferFactory()
|
|
|
|
+ .wrap(map.toJSONString().getBytes())));
|
|
|
|
+ }
|
|
return chain.filter(exchange);
|
|
return chain.filter(exchange);
|
|
} catch (ExpiredJwtException e) {
|
|
} catch (ExpiredJwtException e) {
|
|
if (e.getMessage().contains("Allowed clock skew")) {
|
|
if (e.getMessage().contains("Allowed clock skew")) {
|