修改每个服务的bootstrap.yml文件用于本地测试,将鉴权代码注释用于本地测试

This commit is contained in:
wen 2021-12-27 17:36:58 +08:00
parent 6776fa3071
commit 45f1ecffce
15 changed files with 62 additions and 65 deletions

View File

@ -14,10 +14,10 @@ spring:
nacos: nacos:
discovery: discovery:
# 服务注册地址 # 服务注册地址
server-addr: 172.31.183.135:8848 server-addr: 127.0.0.1:8848
config: config:
# 配置中心地址 # 配置中心地址
server-addr: 172.31.183.135:8848 server-addr: 127.0.0.1:8848
# 配置文件格式 # 配置文件格式
file-extension: yml file-extension: yml
# 共享配置 # 共享配置

View File

@ -14,10 +14,10 @@ spring:
nacos: nacos:
discovery: discovery:
# 服务注册地址 # 服务注册地址
server-addr: 172.31.183.135:8848 server-addr: 127.0.0.1:8848
config: config:
# 配置中心地址 # 配置中心地址
server-addr: 172.31.183.135:8848 server-addr: 127.0.0.1:8848
# 配置文件格式 # 配置文件格式
file-extension: yml file-extension: yml
# 共享配置 # 共享配置

View File

@ -1,11 +1,9 @@
package com.xhpc.gateway.filter; package com.xhpc.gateway.filter;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xhpc.common.core.constant.CacheConstants; import com.xhpc.common.core.constant.CacheConstants;
import com.xhpc.common.core.constant.Constants; import com.xhpc.common.core.constant.Constants;
import com.xhpc.common.core.domain.R; import com.xhpc.common.core.domain.R;
import com.xhpc.common.core.utils.ServletUtils;
import com.xhpc.common.core.utils.StringUtils; import com.xhpc.common.core.utils.StringUtils;
import com.xhpc.common.redis.service.RedisService; import com.xhpc.common.redis.service.RedisService;
import com.xhpc.gateway.config.properties.IgnoreWhiteProperties; import com.xhpc.gateway.config.properties.IgnoreWhiteProperties;
@ -16,7 +14,6 @@ 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.DataBufferFactory; import org.springframework.core.io.buffer.DataBufferFactory;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.http.server.reactive.ServerHttpRequest; import org.springframework.http.server.reactive.ServerHttpRequest;
@ -25,8 +22,6 @@ import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange; import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono; import reactor.core.publisher.Mono;
import javax.annotation.Resource;
/** /**
* 网关鉴权 * 网关鉴权
* *
@ -34,6 +29,7 @@ import javax.annotation.Resource;
*/ */
@Component @Component
public class AuthFilter implements GlobalFilter, Ordered { public class AuthFilter implements GlobalFilter, Ordered {
private static final Logger log = LoggerFactory.getLogger(AuthFilter.class); private static final Logger log = LoggerFactory.getLogger(AuthFilter.class);
private final static long EXPIRE_TIME = Constants.TOKEN_EXPIRE * 60; private final static long EXPIRE_TIME = Constants.TOKEN_EXPIRE * 60;
@ -42,45 +38,46 @@ public class AuthFilter implements GlobalFilter, Ordered {
@Autowired @Autowired
private IgnoreWhiteProperties ignoreWhite; private IgnoreWhiteProperties ignoreWhite;
@Resource(name = "stringRedisTemplate") // @Resource(name = "stringRedisTemplate")
private ValueOperations<String, String> sops; // private ValueOperations<String, String> sops;
@Autowired @Autowired
private RedisService redisService; private RedisService redisService;
@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();
// 跳过不需要验证的路径 // // 跳过不需要验证的路径
if (StringUtils.matches(url, ignoreWhite.getWhites())) { // if (StringUtils.matches(url, ignoreWhite.getWhites())) {
return chain.filter(exchange); // return chain.filter(exchange);
} // }
String token = getToken(exchange.getRequest()); // String token = getToken(exchange.getRequest());
if (StringUtils.isBlank(token)) { // if (StringUtils.isBlank(token)) {
return setUnauthorizedResponse(exchange, "令牌不能为空"); // return setUnauthorizedResponse(exchange, "令牌不能为空");
} // }
String userStr = sops.get(getTokenKey(token)); // String userStr = sops.get(getTokenKey(token));
if (StringUtils.isNull(userStr)) { // if (StringUtils.isNull(userStr)) {
userStr = redisService.getCacheObject(CacheConstants.LOGIN_TOKEN_KEY + token); // userStr = redisService.getCacheObject(CacheConstants.LOGIN_TOKEN_KEY + token);
if (StringUtils.isNull(userStr)) { // if (StringUtils.isNull(userStr)) {
return setUnauthorizedResponse(exchange, "登录状态已过期"); // return setUnauthorizedResponse(exchange, "登录状态已过期");
} // }
} // }
JSONObject obj = JSONObject.parseObject(userStr); // JSONObject obj = JSONObject.parseObject(userStr);
String userid = obj.getString("userid"); // String userid = obj.getString("userid");
String username = obj.getString("username"); // String username = obj.getString("username");
if (StringUtils.isBlank(userid) || StringUtils.isBlank(username)) { // if (StringUtils.isBlank(userid) || StringUtils.isBlank(username)) {
return setUnauthorizedResponse(exchange, "令牌验证失败"); // return setUnauthorizedResponse(exchange, "令牌验证失败");
} // }
//
// // 设置过期时间
// redisService.expire(getTokenKey(token), EXPIRE_TIME);
// // 设置用户信息到请求
// ServerHttpRequest mutableReq = exchange.getRequest().mutate().header(CacheConstants.DETAILS_USER_ID, userid)
// .header(CacheConstants.DETAILS_USERNAME, ServletUtils.urlEncode(username)).build();
// ServerWebExchange mutableExchange = exchange.mutate().request(mutableReq).build();
// 设置过期时间 return chain.filter(exchange);
redisService.expire(getTokenKey(token), EXPIRE_TIME);
// 设置用户信息到请求
ServerHttpRequest mutableReq = exchange.getRequest().mutate().header(CacheConstants.DETAILS_USER_ID, userid)
.header(CacheConstants.DETAILS_USERNAME, ServletUtils.urlEncode(username)).build();
ServerWebExchange mutableExchange = exchange.mutate().request(mutableReq).build();
return chain.filter(mutableExchange);
} }
private Mono<Void> setUnauthorizedResponse(ServerWebExchange exchange, String msg) { private Mono<Void> setUnauthorizedResponse(ServerWebExchange exchange, String msg) {

View File

@ -16,10 +16,10 @@ spring:
nacos: nacos:
discovery: discovery:
# 服务注册地址 # 服务注册地址
server-addr: 172.31.183.135:8848 server-addr: 127.0.0.1:8848
config: config:
# 配置中心地址 # 配置中心地址
server-addr: 172.31.183.135:8848 server-addr: 127.0.0.1:8848
# 配置文件格式 # 配置文件格式
file-extension: yml file-extension: yml
# 共享配置 # 共享配置
@ -35,7 +35,7 @@ spring:
datasource: datasource:
ds1: ds1:
nacos: nacos:
server-addr: 172.31.183.135:8848 server-addr: 127.0.0.1:8848
dataId: sentinel-ruoyi-gateway dataId: sentinel-ruoyi-gateway
groupId: DEFAULT_GROUP groupId: DEFAULT_GROUP
data-type: json data-type: json

View File

@ -14,10 +14,10 @@ spring:
nacos: nacos:
discovery: discovery:
# 服务注册地址 # 服务注册地址
server-addr: 172.31.183.135:8848 server-addr: 127.0.0.1:8848
config: config:
# 配置中心地址 # 配置中心地址
server-addr: 172.31.183.135:8848 server-addr: 127.0.0.1:8848
# 配置文件格式 # 配置文件格式
file-extension: yml file-extension: yml
# 共享配置 # 共享配置

View File

@ -14,10 +14,10 @@ spring:
nacos: nacos:
discovery: discovery:
# 服务注册地址 # 服务注册地址
server-addr: 172.31.183.135:8848 server-addr: 127.0.0.1:8848
config: config:
# 配置中心地址 # 配置中心地址
server-addr: 172.31.183.135:8848 server-addr: 127.0.0.1:8848
# 配置文件格式 # 配置文件格式
file-extension: yml file-extension: yml
# 共享配置 # 共享配置

View File

@ -14,10 +14,10 @@ spring:
nacos: nacos:
discovery: discovery:
# 服务注册地址 # 服务注册地址
server-addr: 172.31.183.135:8848 server-addr: 127.0.0.1:8848
config: config:
# 配置中心地址 # 配置中心地址
server-addr: 172.31.183.135:8848 server-addr: 127.0.0.1:8848
# 配置文件格式 # 配置文件格式
file-extension: yml file-extension: yml
# 共享配置 # 共享配置

View File

@ -14,10 +14,10 @@ spring:
nacos: nacos:
discovery: discovery:
# 服务注册地址 # 服务注册地址
server-addr: 172.31.183.135:8848 server-addr: 127.0.0.1:8848
config: config:
# 配置中心地址 # 配置中心地址
server-addr: 172.31.183.135:8848 server-addr: 127.0.0.1:8848
# 配置文件格式 # 配置文件格式
file-extension: yml file-extension: yml
# 共享配置 # 共享配置

View File

@ -14,10 +14,10 @@ spring:
nacos: nacos:
discovery: discovery:
# 服务注册地址 # 服务注册地址
server-addr: 172.31.183.135:8848 server-addr: 127.0.0.1:8848
config: config:
# 配置中心地址 # 配置中心地址
server-addr: 172.31.183.135:8848 server-addr: 127.0.0.1:8848
# 配置文件格式 # 配置文件格式
file-extension: yml file-extension: yml
# 共享配置 # 共享配置

View File

@ -14,10 +14,10 @@ spring:
nacos: nacos:
discovery: discovery:
# 服务注册地址 # 服务注册地址
server-addr: 172.31.183.135:8848 server-addr: 127.0.0.1:8848
config: config:
# 配置中心地址 # 配置中心地址
server-addr: 172.31.183.135:8848 server-addr: 127.0.0.1:8848
# 配置文件格式 # 配置文件格式
file-extension: yml file-extension: yml
# 共享配置 # 共享配置

View File

@ -14,10 +14,10 @@ spring:
nacos: nacos:
discovery: discovery:
# 服务注册地址 # 服务注册地址
server-addr: 172.31.183.135:8848 server-addr: 127.0.0.1:8848
config: config:
# 配置中心地址 # 配置中心地址
server-addr: 172.31.183.135:8848 server-addr: 127.0.0.1:8848
# 配置文件格式 # 配置文件格式
file-extension: yml file-extension: yml
# 共享配置 # 共享配置

View File

@ -14,10 +14,10 @@ spring:
nacos: nacos:
discovery: discovery:
# 服务注册地址 # 服务注册地址
server-addr: 172.31.183.135:8848 server-addr: 127.0.0.1:8848
config: config:
# 配置中心地址 # 配置中心地址
server-addr: 172.31.183.135:8848 server-addr: 127.0.0.1:8848
# 配置文件格式 # 配置文件格式
file-extension: yml file-extension: yml
# 共享配置 # 共享配置

View File

@ -18,10 +18,10 @@ spring:
nacos: nacos:
discovery: discovery:
# 服务注册地址 # 服务注册地址
server-addr: 172.31.183.135:8848 server-addr: 127.0.0.1:8848
config: config:
# 配置中心地址 # 配置中心地址
server-addr: 172.31.183.135:8848 server-addr: 127.0.0.1:8848
# 配置文件格式 # 配置文件格式
file-extension: yml file-extension: yml
# 共享配置 # 共享配置

View File

@ -14,10 +14,10 @@ spring:
nacos: nacos:
discovery: discovery:
# 服务注册地址 # 服务注册地址
server-addr: 172.31.183.135:8848 server-addr: 127.0.0.1:8848
config: config:
# 配置中心地址 # 配置中心地址
server-addr: 172.31.183.135:8848 server-addr: 127.0.0.1:8848
# 配置文件格式 # 配置文件格式
file-extension: yml file-extension: yml
# 共享配置 # 共享配置

View File

@ -16,10 +16,10 @@ spring:
nacos: nacos:
discovery: discovery:
# 服务注册地址 # 服务注册地址
server-addr: 172.31.183.135:8848 server-addr: 127.0.0.1:8848
config: config:
# 配置中心地址 # 配置中心地址
server-addr: 172.31.183.135:8848 server-addr: 127.0.0.1:8848
# 配置文件格式 # 配置文件格式
file-extension: yml file-extension: yml
# 共享配置 # 共享配置