這篇文章給大家介紹利用springboot怎么實現(xiàn)一個代理分發(fā)服務(wù)功能,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。
成都創(chuàng)新互聯(lián)公司提供成都做網(wǎng)站、網(wǎng)站制作、網(wǎng)頁設(shè)計,品牌網(wǎng)站制作,一元廣告等致力于企業(yè)網(wǎng)站建設(shè)與公司網(wǎng)站制作,10多年的網(wǎng)站開發(fā)和建站經(jīng)驗,助力企業(yè)信息化建設(shè),成功案例突破近千家,是您實現(xiàn)網(wǎng)站建設(shè)的好選擇.技術(shù):說道反向代理,可能首先想到的就是nginx。不過在我們的需求中,對于轉(zhuǎn)發(fā)過程有更多需求:
需要操作session,根據(jù)session的取值決定轉(zhuǎn)發(fā)行為
需要修改Http報文,增加Header或是QueryString
第一點決定了我們的實現(xiàn)必定是基于Servlet的。springboot提供的ProxyServlet就可以滿足我們的要求,ProxyServlet直接繼承自HttpServlet,采用異步的方式調(diào)用內(nèi)部服務(wù)器,因此效率上不會有什么問題,并且各種可重載的函數(shù)也提供了比較強(qiáng)大的定制機(jī)制。
引入依賴
<dependency> <groupId>org.mitre.dsmiley.httpproxy</groupId> <artifactId>smiley-http-proxy-servlet</artifactId> <version>1.11</version> </dependency>
構(gòu)建一個配置類
@Configuration public class ProxyServletConfiguration { private final static String REPORT_URL = "/newReport_proxy/*"; @Bean public ServletRegistrationBean proxyServletRegistration() { List<String> list = new ArrayList<>(); list.add(REPORT_URL); //如果需要匹配多個url則定義好放到list中即可 ServletRegistrationBean registrationBean = new ServletRegistrationBean(); registrationBean.setServlet(new ThreeProxyServlet()); registrationBean.setUrlMappings(list); //設(shè)置默認(rèn)網(wǎng)址以及參數(shù) Map<String, String> params = ImmutableMap.of("targetUri", "null", "log", "true"); registrationBean.setInitParameters(params); return registrationBean; } }
編寫代理邏輯
public class ThreeProxyServlet extends ProxyServlet { private static final long serialVersionUID = -9125871545605920837L; private final Logger logger = LoggerFactory.getLogger(ThreeProxyServlet.class); public String proxyHttpAddr; public String proxyName; private ResourceBundle bundle =null; @Override public void init() throws ServletException { bundle = ResourceBundle.getBundle("prop"); super.init(); } @Override protected void service(HttpServletRequest servletRequest, HttpServletResponse servletResponse) throws ServletException, IOException { // 初始切換路徑 String requestURI = servletRequest.getRequestURI(); proxyName = requestURI.split("/")[2]; //根據(jù)name匹配域名到properties文件中獲取 proxyHttpAddr = bundle.getString(proxyName); String url = proxyHttpAddr; if (servletRequest.getAttribute(ATTR_TARGET_URI) == null) { servletRequest.setAttribute(ATTR_TARGET_URI, url); } if (servletRequest.getAttribute(ATTR_TARGET_HOST) == null) { URL trueUrl = new URL(url); servletRequest.setAttribute(ATTR_TARGET_HOST, new HttpHost(trueUrl.getHost(), trueUrl.getPort(), trueUrl.getProtocol())); } String method = servletRequest.getMethod(); // 替換多余路徑 String proxyRequestUri = this.rewriteUrlFromRequest(servletRequest); Object proxyRequest; if (servletRequest.getHeader("Content-Length") == null && servletRequest.getHeader("Transfer-Encoding") == null) { proxyRequest = new BasicHttpRequest(method, proxyRequestUri); } else { proxyRequest = this.newProxyRequestWithEntity(method, proxyRequestUri, servletRequest); } this.copyRequestHeaders(servletRequest, (HttpRequest)proxyRequest); setXForwardedForHeader(servletRequest, (HttpRequest)proxyRequest); HttpResponse proxyResponse = null; try { proxyResponse = this.doExecute(servletRequest, servletResponse, (HttpRequest)proxyRequest); int statusCode = proxyResponse.getStatusLine().getStatusCode(); servletResponse.setStatus(statusCode, proxyResponse.getStatusLine().getReasonPhrase()); this.copyResponseHeaders(proxyResponse, servletRequest, servletResponse); if (statusCode == 304) { servletResponse.setIntHeader("Content-Length", 0); } else { this.copyResponseEntity(proxyResponse, servletResponse, (HttpRequest)proxyRequest, servletRequest); } } catch (Exception var11) { this.handleRequestException((HttpRequest)proxyRequest, var11); } finally { if (proxyResponse != null) { EntityUtils.consumeQuietly(proxyResponse.getEntity()); } } } @Override protected HttpResponse doExecute(HttpServletRequest servletRequest, HttpServletResponse servletResponse, HttpRequest proxyRequest) throws IOException { HttpResponse response = null; // 攔截校驗 可自定義token過濾 //String token = servletRequest.getHeader("ex_proxy_token"); // 代理服務(wù)鑒權(quán)邏輯 this.getAuthString(proxyName,servletRequest,proxyRequest); //執(zhí)行代理轉(zhuǎn)發(fā) try { response = super.doExecute(servletRequest, servletResponse, proxyRequest); } catch (IOException e) { e.printStackTrace(); } return response; } }
上邊的配置簡單介紹一下,對于/newReport_proxy/* 這樣的寫法,意思就是當(dāng)你的請求路徑以newReport_proxy 開頭,比如http://localhost:8080/newReport_proxy/test/get1 這樣的路徑,它請求的真實路徑是https://www.baidu.com/test/get1 。主要就是將newReport_proxy 替換成對應(yīng)的被代理路徑而已,* 的意思就是實際請求代理項目中接口的路徑,這種配置對get 、post 請求都有效。
按如上配置,在執(zhí)行代理轉(zhuǎn)發(fā)的時候需要對轉(zhuǎn)發(fā)的代理服務(wù)器的接口進(jìn)行鑒權(quán),具體鑒權(quán)方案調(diào)用就是 "this.getAuthString(proxyName,servletRequest,proxyRequest);”這段代碼。代理服務(wù)的鑒權(quán)邏輯根據(jù)入?yún)?token值之后按算法計算一個值,之后進(jìn)行放到header中傳遞。那么這就遇到了一個問題,就是當(dāng)前端采用requestBody的方式進(jìn)行調(diào)用請求時服務(wù)1進(jìn)行代理轉(zhuǎn)發(fā)的時候會出現(xiàn)錯誤:
一直卡在執(zhí)行 doExecute()方法。一頓操作debug后定位到一個點,也就是最后進(jìn)行觸發(fā)進(jìn)行執(zhí)行代理服務(wù)調(diào)用的點:
在上圖位置拋了異常,上圖中i的值為-1,說明這個sessionBuffer中沒有數(shù)據(jù)了,讀取不到了所以返回了-1。那么這個sessionBuffer是個什么東西呢?這個東西翻譯過來指的是會話輸入緩沖區(qū),會阻塞連接。 與InputStream類相似,也提供讀取文本行的方法。也就是通過這個類將對應(yīng)請求的數(shù)據(jù)流發(fā)送給目標(biāo)服務(wù)。這個位置出錯說明這個要發(fā)送的數(shù)據(jù)流沒有了,那么在什么時候?qū)⒄埱蟮臄?shù)據(jù)流信息給弄沒了呢?那就是我們加點鑒權(quán)邏輯,鑒權(quán)邏輯需要獲取requestBody中的參數(shù),去該參數(shù)是從request對象中通過流讀取的。這個問題我們也見過通常情況下,HttpServletRequst 中的 body 內(nèi)容只會讀取一次,但是可能某些情境下可能會讀取多次,由于 body 內(nèi)容是以流的形式存在,所以第一次讀取完成后,第二次就無法讀取了,一個典型的場景就是 Filter 在校驗完成 body 的內(nèi)容后,業(yè)務(wù)方法就無法繼續(xù)讀取流了,導(dǎo)致解析報錯。
思路:用裝飾器來修飾一下 request,使其可以包裝讀取的內(nèi)容,供多次讀取。其實spring boot提供了一個簡單的封裝器ContentCachingRequestWrapper,從源碼上看這個封裝器并不實用,沒有封裝http的底層流ServletInputStream信息,所以在這個場景下還是不能重復(fù)獲取對應(yīng)的流信息。
參照ContentCachingRequestWrapper類實現(xiàn)一個stream緩存
public class CacheStreamHttpRequest extends HttpServletRequestWrapper { private static final Logger LOGGER = LoggerFactory.getLogger(CacheStreamHttpRequest.class); private final ByteArrayOutputStream cachedContent; private Map<String, String[]> cachedForm; @Nullable private ServletInputStream inputStream; public CacheStreamHttpRequest(HttpServletRequest request) { super(request); this.cachedContent = new ByteArrayOutputStream(); this.cachedForm = new HashMap<>(); cacheData(); } @Override public ServletInputStream getInputStream() throws IOException { this.inputStream = new RepeatReadInputStream(cachedContent.toByteArray()); return this.inputStream; } @Override public String getCharacterEncoding() { String enc = super.getCharacterEncoding(); return (enc != null ? enc : WebUtils.DEFAULT_CHARACTER_ENCODING); } @Override public BufferedReader getReader() throws IOException { return new BufferedReader(new InputStreamReader(getInputStream(), getCharacterEncoding())); } @Override public String getParameter(String name) { String value = null; if (isFormPost()) { String[] values = cachedForm.get(name); if (null != values && values.length > 0) { value = values[0]; } } if (StringUtils.isEmpty(value)) { value = super.getParameter(name); } return value; } @Override public Map<String, String[]> getParameterMap() { if (isFormPost() && !CollectionUtils.sizeIsEmpty(cachedForm)) { return cachedForm; } return super.getParameterMap(); } @Override public Enumeration<String> getParameterNames() { if (isFormPost() && !CollectionUtils.sizeIsEmpty(cachedForm)) { return Collections.enumeration(cachedForm.keySet()); } return super.getParameterNames(); } @Override public String[] getParameterValues(String name) { if (isFormPost() && !CollectionUtils.sizeIsEmpty(cachedForm)) { return cachedForm.get(name); } return super.getParameterValues(name); } private void cacheData() { try { if (isFormPost()) { this.cachedForm = super.getParameterMap(); } else { ServletInputStream inputStream = super.getInputStream(); IOUtils.copy(inputStream, this.cachedContent); } } catch (IOException e) { LOGGER.warn("[RepeatReadHttpRequest:cacheData], error: {}", e.getMessage()); } } private boolean isFormPost() { String contentType = getContentType(); return (contentType != null && (contentType.contains(MediaType.APPLICATION_FORM_URLENCODED_VALUE) || contentType.contains(MediaType.MULTIPART_FORM_DATA_VALUE)) && HttpMethod.POST.matches(getMethod())); } private static class RepeatReadInputStream extends ServletInputStream { private final ByteArrayInputStream inputStream; public RepeatReadInputStream(byte[] bytes) { this.inputStream = new ByteArrayInputStream(bytes); } @Override public int read() throws IOException { return this.inputStream.read(); } @Override public int readLine(byte[] b, int off, int len) throws IOException { return this.inputStream.read(b, off, len); } @Override public boolean isFinished() { return this.inputStream.available() == 0; } @Override public boolean isReady() { return true; } @Override public void setReadListener(ReadListener listener) { } } }
如上類核心邏輯是通過cacheData() 方法進(jìn)行將 request對象緩存,存儲到ByteArrayOutputStream類中,當(dāng)在調(diào)用request對象獲取getInputStream()方法時從ByteArrayOutputStream類中寫回InputStream核心代碼:
@Override public ServletInputStream getInputStream() throws IOException { this.inputStream = new RepeatReadInputStream(cachedContent.toByteArray()); return this.inputStream; }
使用這個封裝后的request時需要配合Filter對原有的request進(jìn)行替換,注冊Filter并在調(diào)用鏈中將原有的request換成該封裝類。代碼:
//chain.doFilter(request, response); //換掉原來的request對象 用new RepeatReadHttpRequest((HttpServletRequest) request) 因為后者流中由緩存攔截器httprequest替換 可重復(fù)獲取inputstream chain.doFilter(new RepeatReadHttpRequest((HttpServletRequest) request), response);
關(guān)于利用springboot怎么實現(xiàn)一個代理分發(fā)服務(wù)功能就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
網(wǎng)站題目:利用springboot怎么實現(xiàn)一個代理分發(fā)服務(wù)功能-創(chuàng)新互聯(lián)
路徑分享:http://muchs.cn/article22/idocc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計公司、標(biāo)簽優(yōu)化、網(wǎng)站策劃、自適應(yīng)網(wǎng)站、企業(yè)建站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容