Commit f6cdd0ef by chamberone
1 parent 106e7b20
......@@ -38,6 +38,7 @@ public class AuthFilter implements GlobalFilter, Ordered {
String url = exchange.getRequest().getPath().toString();
// 判断是否属于白名单中
if (authWhiteList.contains(url)) {
log.info("whiteList:{} ip:{}", url, getRemoteIP(exchange));
return chain.filter(exchange);
}
......@@ -71,11 +72,12 @@ public class AuthFilter implements GlobalFilter, Ordered {
} else {
log.info("用户:{} id:{} 访问{}", userDTO.getAccount(), userDTO.getId(), url);
// 获取当前的请求对象信息
ServerHttpRequest.Builder builder = exchange.getRequest().mutate();
// 向header中设置新的key,存储解析好的token对应基本信息
builder.header("userId", userDTO.getId());
// 向下游传递
return chain.filter(exchange.mutate().request(builder.build()).build());
exchange.getRequest().getHeaders().add("userId", userDTO.getId());
// ServerHttpRequest.Builder builder = exchange.getRequest().mutate();
// // 向header中设置新的key,存储解析好的token对应基本信息
// builder.header("userId", userDTO.getId());
// // 向下游传递
// return chain.filter(exchange.mutate().request(builder.build()).build());
}
return chain.filter(exchange);
......
......@@ -2,10 +2,13 @@ package com.dituhui.pea.gateway.handler;
import com.dituhui.pea.enums.StatusCodeEnum;
import com.dituhui.pea.exception.BusinessException;
import com.dituhui.pea.gateway.config.AuthFilter;
import com.dituhui.pea.pojo.WebResult;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.web.reactive.error.ErrorWebExceptionHandler;
......@@ -21,43 +24,43 @@ import reactor.core.publisher.Mono;
/**
* 网关异常通用处理器,只作用在webflux 环境下,优先级低于{@link ResponseStatusExceptionHandler}执行
*
* @author Administrator
*/
@Order(-1)
@Configuration
@RequiredArgsConstructor
@Slf4j
public class GlobalExceptionHandler implements ErrorWebExceptionHandler {
private static final Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);
private final ObjectMapper objectMapper;
private final ObjectMapper objectMapper;
@Override
public Mono<Void> handle(ServerWebExchange exchange, Throwable ex) {
ServerHttpResponse response = exchange.getResponse();
if (response.isCommitted()) {
return Mono.error(ex);
}
response.getHeaders().setContentType(MediaType.APPLICATION_JSON);
if (ex instanceof ResponseStatusException) {
response.setStatusCode(((ResponseStatusException) ex).getStatus());
}
return response.writeWith(Mono.fromSupplier(() -> {
DataBufferFactory bufferFactory = response.bufferFactory();
try {
// 判断是否为业务异常
if (ex instanceof BusinessException) {
StatusCodeEnum codeEnum = ((BusinessException) ex).codeEnum;
return bufferFactory.wrap(objectMapper.writeValueAsBytes(WebResult.failed(codeEnum)));
} else {
logger.error("网关异常:" + ex);
return bufferFactory.wrap(objectMapper.writeValueAsBytes(WebResult.failed(StatusCodeEnum.SYSTEM_UNKNOWN_ERROR)));
}
}
catch (JsonProcessingException e) {
logger.error("Error writing response", ex);
return bufferFactory.wrap(new byte[0]);
}
}));
}
@Override
public Mono<Void> handle(ServerWebExchange exchange, Throwable ex) {
ServerHttpResponse response = exchange.getResponse();
if (response.isCommitted()) {
return Mono.error(ex);
}
response.getHeaders().setContentType(MediaType.APPLICATION_JSON);
if (ex instanceof ResponseStatusException) {
response.setStatusCode(((ResponseStatusException) ex).getStatus());
}
return response.writeWith(Mono.fromSupplier(() -> {
DataBufferFactory bufferFactory = response.bufferFactory();
try {
// 判断是否为业务异常
if (ex instanceof BusinessException) {
StatusCodeEnum codeEnum = ((BusinessException) ex).codeEnum;
return bufferFactory.wrap(objectMapper.writeValueAsBytes(WebResult.failed(codeEnum)));
} else {
log.error("{} 访问网关异常:{} {}", exchange.getRequest().getPath(), ex, ex.getMessage());
return bufferFactory.wrap(
objectMapper.writeValueAsBytes(WebResult.failed(StatusCodeEnum.SYSTEM_UNKNOWN_ERROR)));
}
} catch (JsonProcessingException e) {
log.error("Error writing response", ex);
return bufferFactory.wrap(new byte[0]);
}
}));
}
}
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!