diff --git a/02nio/nio02/src/main/java/io/github/kimmking/gateway/filter/MyHttpRequestFilter.java b/02nio/nio02/src/main/java/io/github/kimmking/gateway/filter/MyHttpRequestFilter.java new file mode 100644 index 00000000..d44dccde --- /dev/null +++ b/02nio/nio02/src/main/java/io/github/kimmking/gateway/filter/MyHttpRequestFilter.java @@ -0,0 +1,37 @@ +package io.github.kimmking.gateway.filter; + +import com.sun.tools.javac.util.StringUtils; +import io.netty.buffer.ByteBuf; +import io.netty.channel.ChannelHandlerContext; +import io.netty.handler.codec.http.FullHttpRequest; +import io.netty.util.internal.StringUtil; +import sun.security.provider.MD5; +import sun.security.rsa.RSASignature; + +import java.math.BigInteger; +import java.nio.charset.Charset; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; + +public class MyHttpRequestFilter implements HttpRequestFilter{ + private static final Charset DEFAULT_CHARACTER=Charset.forName("utf-8"); + @Override + public void filter(FullHttpRequest fullRequest, ChannelHandlerContext ctx) { + ByteBuf content = fullRequest.content(); + String md5 = this.getMd5(content.array()); + fullRequest.headers().add("request-signature",md5); + } + private String getMd5(byte[] bytes){ + byte[] secretBytes = null; + try { + secretBytes = MessageDigest.getInstance("md5").digest(bytes); + } catch (NoSuchAlgorithmException e) { + throw new RuntimeException("没有这个md5算法!"); + } + String md5code = new BigInteger(1, secretBytes).toString(16); + for (int i = 0; i < 32 - md5code.length(); i++) { + md5code = "0" + md5code; + } + return md5code; + } +} diff --git a/02nio/nio02/src/main/java/io/github/kimmking/gateway/outbound/httpclient4/HttpOutboundHandler.java b/02nio/nio02/src/main/java/io/github/kimmking/gateway/outbound/httpclient4/HttpOutboundHandler.java index c20c9be5..9f51da62 100644 --- a/02nio/nio02/src/main/java/io/github/kimmking/gateway/outbound/httpclient4/HttpOutboundHandler.java +++ b/02nio/nio02/src/main/java/io/github/kimmking/gateway/outbound/httpclient4/HttpOutboundHandler.java @@ -4,6 +4,7 @@ import io.github.kimmking.gateway.filter.HeaderHttpResponseFilter; import io.github.kimmking.gateway.filter.HttpRequestFilter; import io.github.kimmking.gateway.filter.HttpResponseFilter; +import io.github.kimmking.gateway.filter.MyHttpRequestFilter; import io.github.kimmking.gateway.router.HttpEndpointRouter; import io.github.kimmking.gateway.router.RandomHttpEndpointRouter; import io.netty.buffer.Unpooled; @@ -37,9 +38,10 @@ public class HttpOutboundHandler { private CloseableHttpAsyncClient httpclient; private ExecutorService proxyService; private List backendUrls; - + private MyHttpClient myHttpClient = new MyHttpClient(); HttpResponseFilter filter = new HeaderHttpResponseFilter(); HttpEndpointRouter router = new RandomHttpEndpointRouter(); + MyHttpRequestFilter myHttpRequestFilter = new MyHttpRequestFilter(); public HttpOutboundHandler(List backends) { @@ -75,7 +77,9 @@ private String formatUrl(String backend) { public void handle(final FullHttpRequest fullRequest, final ChannelHandlerContext ctx, HttpRequestFilter filter) { String backendUrl = router.route(this.backendUrls); final String url = backendUrl + fullRequest.uri(); - filter.filter(fullRequest, ctx); + //filter.filter(fullRequest, ctx); + //替换成自己的过滤器 + myHttpRequestFilter.filter(fullRequest,ctx); proxyService.submit(()->fetchGet(fullRequest, ctx, url)); } @@ -93,16 +97,16 @@ public void completed(final HttpResponse endpointResponse) { } catch (Exception e) { e.printStackTrace(); } finally { - + } } - + @Override public void failed(final Exception ex) { httpGet.abort(); ex.printStackTrace(); } - + @Override public void cancelled() { httpGet.abort(); diff --git a/02nio/nio02/src/main/java/io/github/kimmking/gateway/outbound/httpclient4/MyHttpClient.java b/02nio/nio02/src/main/java/io/github/kimmking/gateway/outbound/httpclient4/MyHttpClient.java new file mode 100644 index 00000000..e5ec3656 --- /dev/null +++ b/02nio/nio02/src/main/java/io/github/kimmking/gateway/outbound/httpclient4/MyHttpClient.java @@ -0,0 +1,71 @@ +package io.github.kimmking.gateway.outbound.httpclient4; + +import org.apache.http.client.config.RequestConfig; +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.entity.StringEntity; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClients; +import org.apache.http.util.EntityUtils; + +import java.io.IOException; +import java.nio.charset.Charset; +import java.util.HashMap; +import java.util.Map; +import java.util.Set; + +public class MyHttpClient { + +// public static void main(String[] args) throws IOException { +// String url = "http://localhost:8801"; +// String body = "hello"; +// String charSet = "utf8"; +// MyHttpClient myHttpClient = new MyHttpClient(); +// Map headers = new HashMap<>(); +// headers.put("Content-Type","text/html;charset=utf-8"); +// String result = myHttpClient.doPost(url,body,charSet,headers); +// System.out.println(result); +// } + + public String doPost(String url, String requestBody, String charSet, Map header) throws IOException { + MyAHttpClient myAHttpClient = new MyAHttpClient(); + return myAHttpClient.doPost(url,requestBody,charSet,header); + } +} + + +class MyAHttpClient{ + + private CloseableHttpClient closeableHttpClient; + public MyAHttpClient(){ + super(); + } + + public CloseableHttpClient getHttpClient(){ + return HttpClients.custom().build(); + } + + public String doPost(String url, String requestBody, String charSet, Map header) throws IOException { + closeableHttpClient=this.getHttpClient(); + HttpPost httpPost = this.getHttpPostRequest(url,requestBody,charSet,header); + CloseableHttpResponse closeableHttpResponse = closeableHttpClient.execute(httpPost); + String responseString = EntityUtils.toString(closeableHttpResponse.getEntity(), Charset.forName(charSet)); + closeableHttpClient.close(); + return responseString; + } + + private HttpPost getHttpPostRequest(String url,String requestBody,String charSet,Map header){ + HttpPost httpPost = new HttpPost(url); + if(header!=null && !header.isEmpty()){ + Set> entrySet = header.entrySet(); + for (Map.Entry entry:entrySet) { + httpPost.addHeader(entry.getKey(),String.valueOf(entry.getValue())); + } + } + StringEntity stringEntity = new StringEntity(requestBody,Charset.forName(charSet)); + httpPost.setEntity(stringEntity); + RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(1000).setConnectTimeout(1000).setSocketTimeout(1000).build(); + httpPost.setConfig(requestConfig); + return httpPost; + } +}