Skip to content

Commit b639c46

Browse files
author
tangrong
committed
过滤
1 parent 1e78fd4 commit b639c46

2 files changed

Lines changed: 44 additions & 9 deletions

File tree

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package io.github.kimmking.gateway.filter;
2+
3+
import cn.hutool.core.convert.Convert;
4+
import io.netty.buffer.Unpooled;
5+
import io.netty.channel.ChannelFutureListener;
6+
import io.netty.channel.ChannelHandlerContext;
7+
import io.netty.handler.codec.http.*;
8+
9+
import static io.netty.handler.codec.http.HttpResponseStatus.OK;
10+
import static io.netty.handler.codec.http.HttpVersion.HTTP_1_1;
11+
12+
public class TokenFilter implements HttpRequestFilter{
13+
@Override
14+
public void filter(FullHttpRequest fullRequest, ChannelHandlerContext ctx) {
15+
HttpHeaders headers = fullRequest.trailingHeaders();
16+
17+
if (!headers.contains("token")) {
18+
FullHttpResponse response = null;
19+
byte[] bytes = "deny access".getBytes();
20+
response = new DefaultFullHttpResponse(HTTP_1_1, OK, Unpooled.wrappedBuffer(bytes));
21+
response.headers().set("Content-Type", "application/json");
22+
response.setStatus(HttpResponseStatus.FORBIDDEN);
23+
response.headers().setInt("Content-Length", Convert.toInt(bytes.length));
24+
if (fullRequest != null) {
25+
if (!HttpUtil.isKeepAlive(fullRequest)) {
26+
ctx.write(response).addListener(ChannelFutureListener.CLOSE);
27+
} else {
28+
ctx.write(response);
29+
}
30+
}
31+
}
32+
}
33+
}

02nio/nio02/src/main/java/io/github/kimmking/gateway/inbound/HttpInboundHandler.java

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.github.kimmking.gateway.inbound;
22

3+
import io.github.kimmking.gateway.filter.TokenFilter;
34
import io.github.kimmking.gateway.outbound.httpclient4.HttpOutboundHandler;
45
import io.github.kimmking.gateway.outbound.okhttp.HutoolhttpOutboundHandler;
56
import io.github.kimmking.gateway.router.HttpEndpointRouter;
@@ -34,19 +35,20 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) {
3435
try {
3536

3637
FullHttpRequest fullRequest = (FullHttpRequest) msg;
38+
TokenFilter tokenFilter = new TokenFilter();
39+
//过滤
40+
tokenFilter.filter(fullRequest,ctx);
3741

3842
String url = fullRequest.uri();
39-
HttpEndpointRouter router = new HttpEndpointRouter() {
40-
@Override
41-
public String route(List<String> endpoints, List<String> proxyServers) {
42-
if (endpoints.stream().anyMatch(e->e.startsWith("/user"))) {
43-
return proxyServers.get(0);
44-
} else if (endpoints.stream().anyMatch(e->e.startsWith("/order"))) {
45-
return proxyServers.get(1);
46-
}
47-
return null;
43+
HttpEndpointRouter router = (endpoints, proxyServers) -> {
44+
if (endpoints.stream().anyMatch(e->e.startsWith("/user"))) {
45+
return proxyServers.get(0);
46+
} else if (endpoints.stream().anyMatch(e->e.startsWith("/order"))) {
47+
return proxyServers.get(1);
4848
}
49+
return null;
4950
};
51+
//路由
5052
String proxy = router.route(Arrays.asList(url), Arrays.asList(this.proxyServer.split(",").clone()));
5153
HutoolhttpOutboundHandler handler = new HutoolhttpOutboundHandler(proxy);
5254
handler.handle(fullRequest, ctx);

0 commit comments

Comments
 (0)