完善基本权限鉴定
parent
90927048f1
commit
6afce02f78
|
@ -2,6 +2,8 @@ package com.health.common.domain;
|
||||||
|
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Administrator
|
* @author Administrator
|
||||||
*/
|
*/
|
||||||
|
@ -12,6 +14,7 @@ public class User {
|
||||||
private String username;
|
private String username;
|
||||||
private String password;
|
private String password;
|
||||||
private String emil;
|
private String emil;
|
||||||
|
private Date lastTime;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -11,8 +11,8 @@ import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* @author Administrator
|
||||||
* @description: 放行白名单配置
|
* @description: 放行白名单配置
|
||||||
* @author DongZl
|
|
||||||
*/
|
*/
|
||||||
@Configuration
|
@Configuration
|
||||||
@RefreshScope
|
@RefreshScope
|
||||||
|
|
|
@ -0,0 +1,83 @@
|
||||||
|
package com.health.gateway.filters;
|
||||||
|
|
||||||
|
import cn.hutool.core.date.DateUnit;
|
||||||
|
import cn.hutool.core.date.DateUtil;
|
||||||
|
import com.alibaba.fastjson.JSONObject;
|
||||||
|
import com.health.common.constants.TokenConstants;
|
||||||
|
import com.health.common.domain.User;
|
||||||
|
import com.health.common.utils.JwtUtils;
|
||||||
|
import com.health.common.utils.StringUtils;
|
||||||
|
import com.health.gateway.config.IgnoreWhiteConfig;
|
||||||
|
import com.health.gateway.utils.GatewayUtils;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
|
||||||
|
import org.springframework.cloud.gateway.filter.GlobalFilter;
|
||||||
|
import org.springframework.core.Ordered;
|
||||||
|
import org.springframework.data.redis.core.RedisTemplate;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.http.server.reactive.ServerHttpRequest;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
import org.springframework.web.server.ServerWebExchange;
|
||||||
|
import reactor.core.publisher.Mono;
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
public class AuthFilters implements GlobalFilter, Ordered {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private IgnoreWhiteConfig ignoreWhitesConfig;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private RedisTemplate<String, String> redisTemplate;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param exchange
|
||||||
|
* @param chain
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
|
||||||
|
List<String> whites = ignoreWhitesConfig.getWhites();
|
||||||
|
ServerHttpRequest request = exchange.getRequest();
|
||||||
|
String path = request.getURI().getPath();
|
||||||
|
if (StringUtils.matches(path, whites)) {
|
||||||
|
return chain.filter(exchange);
|
||||||
|
}
|
||||||
|
String token = request.getHeaders().getFirst(TokenConstants.TOKEN);
|
||||||
|
if (StringUtils.isEmpty(token)) {
|
||||||
|
return GatewayUtils.errorResponse(exchange, "token不能为空!", HttpStatus.UNAUTHORIZED);
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
JwtUtils.parseToken(token);
|
||||||
|
} catch (Exception e) {
|
||||||
|
return GatewayUtils.errorResponse(exchange, "token不合法!");
|
||||||
|
}
|
||||||
|
String userKey = JwtUtils.getUserKey(token);
|
||||||
|
Boolean hasKey = redisTemplate.hasKey(TokenConstants.LOGIN_TOKEN_KEY + userKey);
|
||||||
|
if (null == hasKey || !hasKey) {
|
||||||
|
return GatewayUtils.errorResponse(exchange, "token过期!");
|
||||||
|
}
|
||||||
|
String jsonStr = redisTemplate.opsForValue().get(TokenConstants.LOGIN_TOKEN_KEY + userKey);
|
||||||
|
User user = JSONObject.parseObject(jsonStr, User.class);
|
||||||
|
Date lastLoginTime = user.getLastTime();
|
||||||
|
long between = DateUtil.between(lastLoginTime, new Date(), DateUnit.MINUTE);
|
||||||
|
if (between >= 10) {
|
||||||
|
redisTemplate.expire(TokenConstants.LOGIN_TOKEN_KEY + userKey, 15, TimeUnit.MINUTES);
|
||||||
|
}
|
||||||
|
// 验证通过放行
|
||||||
|
return chain.filter(exchange);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public int getOrder() {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
|
@ -13,7 +13,7 @@ import org.springframework.web.server.ServerWebExchange;
|
||||||
import reactor.core.publisher.Mono;
|
import reactor.core.publisher.Mono;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author DongZl
|
* @author Administrator
|
||||||
* @description: 网关处理工具类
|
* @description: 网关处理工具类
|
||||||
*/
|
*/
|
||||||
@Log4j2
|
@Log4j2
|
||||||
|
|
Loading…
Reference in New Issue