(資料圖片)
spring boot過(guò)濾器實(shí)現(xiàn)項(xiàng)目?jī)?nèi)接口過(guò)濾業(yè)務(wù)由于業(yè)務(wù)需求,存在兩套項(xiàng)目,一套是路由中心,一套是業(yè)務(wù)系統(tǒng).現(xiàn)在存在問(wèn)題是,路由中心集成了微信公眾號(hào)與小程序模塊功能,業(yè)務(wù)系統(tǒng)部署了多套服務(wù).現(xiàn)在需要通過(guò)調(diào)用路由中心將接口重新路由到指定的業(yè)務(wù)系統(tǒng)中
需要處理的問(wèn)題將小程序,公眾號(hào)用戶信息與業(yè)務(wù)系統(tǒng)做綁定將路由中心的接口與業(yè)務(wù)系統(tǒng)的接口判斷出來(lái)通過(guò)用戶信息找到的業(yè)務(wù)系統(tǒng)服務(wù),分發(fā)到對(duì)應(yīng)的業(yè)務(wù)系統(tǒng)中公眾號(hào)用戶信息與業(yè)務(wù)系統(tǒng)做綁定處理步驟業(yè)務(wù)系統(tǒng)存在手機(jī)號(hào),如果用戶注冊(cè)將手機(jī)號(hào)發(fā)送給路由中心記錄將路由中心的接口與業(yè)務(wù)系統(tǒng)的接口判斷處理步驟獲取路由中心系統(tǒng)中接口映射private static List URLS = new ArrayList<>(); @Resource private WebApplicationContext applicationContext; @Override public void init(FilterConfig filterConfig) throws ServletException { applicationContext.getBean(RequestMappingHandlerMapping.class).getHandlerMethods().forEach((k, v) -> { k.getPatternsCondition().getPatterns().stream().forEach(s-> URLS.add(s)); }); log.info("過(guò)濾器初始化"); }
獲取請(qǐng)求接口路徑HttpServletRequest request = (HttpServletRequest) servletRequest; HttpServletResponse response = (HttpServletResponse) servletResponse; String requestName = request.getRequestURI();
判斷是否是路由中心的接口if (URLS.contains(requestName)) {//系統(tǒng)接口 chain.doFilter(servletRequest, servletResponse); } else {//業(yè)務(wù)系統(tǒng)接口 需要代理// 代理請(qǐng)求 ResponseEntity redirect = routerService.redirect(request, response, "xxx", "xxx"); //設(shè)置將字符以"UTF-8"編碼輸出到客戶端瀏覽器 response.setCharacterEncoding("UTF-8"); //通過(guò)設(shè)置響應(yīng)頭控制瀏覽器以UTF-8的編碼顯示數(shù)據(jù),如果不加這句話,那么瀏覽器顯示的將是亂碼 response.setHeader("content-type", "application/json; charset=utf-8"); response.getWriter().write(redirect.getBody()); }
全部代碼import com.jyw.router.miniapp.service.IRouterService;import lombok.extern.slf4j.Slf4j;import org.springframework.context.annotation.Configuration;import org.springframework.http.ResponseEntity;import org.springframework.web.context.WebApplicationContext;import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;import javax.annotation.Resource;import javax.servlet.*;import javax.servlet.annotation.WebFilter;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import java.io.IOException;import java.util.ArrayList;import java.util.List;/** * @author 沈峻 * @ClassName RouterFilter * @Description TODO * @Date 2023/4/20 12:23 **/@Configuration@WebFilter(urlPatterns = "/*", filterName = "Router")@Slf4jpublic class RouterFilter implements Filter { private static List URLS = new ArrayList<>(); @Resource private WebApplicationContext applicationContext; @Resource private IRouterService routerService; @Override public void init(FilterConfig filterConfig) throws ServletException { applicationContext.getBean(RequestMappingHandlerMapping.class).getHandlerMethods().forEach((k, v) -> { k.getPatternsCondition().getPatterns().stream().forEach(s-> URLS.add(s)); }); log.info("過(guò)濾器初始化"); } @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest) servletRequest; HttpServletResponse response = (HttpServletResponse) servletResponse; String requestName = request.getRequestURI(); /*順手解決跨域問(wèn)題*/ response.setHeader("Access-Control-Allow-Origin", "*"); response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE"); response.setHeader("Access-Control-Max-Age", "3600"); response.setHeader("Access-Control-Allow-Headers", "x-requested-with"); log.info(requestName); if (URLS.contains(requestName)) { chain.doFilter(servletRequest, servletResponse); } else {// 代理請(qǐng)求 ResponseEntity redirect = routerService.redirect(request, response, "http://192.168.2.18/api", "/router"); //設(shè)置將字符以"UTF-8"編碼輸出到客戶端瀏覽器 response.setCharacterEncoding("UTF-8"); //通過(guò)設(shè)置響應(yīng)頭控制瀏覽器以UTF-8的編碼顯示數(shù)據(jù),如果不加這句話,那么瀏覽器顯示的將是亂碼 response.setHeader("content-type", "application/json; charset=utf-8"); response.getWriter().write(redirect.getBody()); } log.info("--------------------------------------------------------"); } @Override public void destroy() { Filter.super.destroy(); }
代理轉(zhuǎn)發(fā)實(shí)現(xiàn)https://www.cnblogs.com/shenjun980326/p/spring-boot-route.html
標(biāo)簽:
springboot過(guò)濾器實(shí)現(xiàn)項(xiàng)目?jī)?nèi)接口過(guò)濾業(yè)務(wù)由于業(yè)務(wù)需求,存在兩套項(xiàng)目...
1、全成都購(gòu)車通享補(bǔ)貼及成都經(jīng)開(kāi)區(qū)(龍泉驛區(qū))購(gòu)車專享補(bǔ)貼補(bǔ)貼方式...
電網(wǎng)檢修公告尊敬的客戶,檢修期間供電將會(huì)暫時(shí)中斷,由此帶來(lái)的不...
人民網(wǎng)岳西4月18日電(高飛躍)綠樹(shù)掩映下的民居錯(cuò)落有致,寬闊平整的...
我省出臺(tái)多項(xiàng)舉措保障新就業(yè)形態(tài)勞動(dòng)者權(quán)益包括保障休息時(shí)間、依法...
1、蒲松齡先生的《聊齋志異》,用筆精確簡(jiǎn)約,他寓意的地方不著痕跡...
4月19日,市八屆人大常委會(huì)召開(kāi)第二十一次主任會(huì)議,市人大常委會(huì)主...
4月19日,據(jù)港交所文件,騰訊公司在4月13日出售了約5115 55萬(wàn)股快...
新湖南,主流新媒體,移動(dòng)新門戶。-分享自@新湖南
該產(chǎn)品是根據(jù)古建筑所用立柱專門生產(chǎn)的一種原木加工設(shè)備。優(yōu)點(diǎn)是速...
匯通能源(600605)04月20日在投資者關(guān)系平臺(tái)上答復(fù)了投資者關(guān)心的問(wèn)題。
NBA季后賽首輪G2,太陽(yáng)123-109擊敗快船。太陽(yáng)球員布克賽后接受了采...
1、航空煤油。2、航空燃油是指一些專門為飛行器而設(shè)的燃油品種,質(zhì)...
張大大否認(rèn)做醫(yī)美張大大搜狐娛樂(lè)訊4月19日晚,張大大轉(zhuǎn)發(fā)網(wǎng)友質(zhì)疑“...
1、利用融媒體,為投資者提供更受歡迎的內(nèi)容:一直以來(lái),九方財(cái)富都十...
智通財(cái)經(jīng)APP訊,新天綠色能源(00956)發(fā)布公告,吳會(huì)江因工作安排調(diào)...
食用枸杞有害處嗎人們?nèi)粘K幱没蚍玫亩酁殍坭降墓麑?shí)枸杞子,正常...
順絡(luò)電子(002138)04月20日在投資者關(guān)系平臺(tái)上答復(fù)了投資者關(guān)心的問(wèn)題。
蘋果也要跨界玩理財(cái)了!近日官網(wǎng)發(fā)布消息稱,與高盛合作的AppleCard...
1、不銹鋼的種類有很多,不銹鋼是指不受空氣、水等物質(zhì)腐蝕而制成的...
1、* eng這是類型為英文的語(yǔ)言文件,用Dreamweaver和editplus應(yīng)該...
人口大洗牌!長(zhǎng)沙人口增量躍居全國(guó)第一,強(qiáng)省會(huì)成為“制勝法寶”,武...
鄭源的歌是誰(shuí)給他寫(xiě)的,鄭源的歌好聽(tīng)嗎很多人還不知道,現(xiàn)在讓我們...
豐原藥業(yè)(SZ000153,收盤價(jià):9 44元)4月19日晚間發(fā)布公告稱,計(jì)...
華騏環(huán)保4月20日公布2022年年度分紅預(yù)案:以1 32億股股本為基數(shù),...
1、《潞令》是清代小說(shuō)家蒲松齡創(chuàng)作的文言短篇小說(shuō)。2、選自《聊齋...
技術(shù)分析在眾多投資者眼中一直被視為制勝的法寶,其有效性在實(shí)際操...
東方網(wǎng)記者傅文婧4月19日?qǐng)?bào)道:人才是推進(jìn)科技創(chuàng)新中心建設(shè)的關(guān)鍵核...
關(guān)于G7易流G7易流公司是一家專注于物流運(yùn)輸行業(yè)的領(lǐng)先企業(yè)。公司成...
上證報(bào)中國(guó)證券網(wǎng)訊(記者韓宋輝)記者19日從中國(guó)人壽壽險(xiǎn)公司獲悉...
廣告
X 關(guān)閉
廣告
X 關(guān)閉