黑袜帅主玩奴Gay.2022_波多野结衣av一台道_观看出轨少妇视频在线观看_无码视频网站亚洲精华液国产_精品自在拍精选

spring boot過(guò)濾器實(shí)現(xiàn)項(xiàng)目?jī)?nèi)接口過(guò)濾 當(dāng)前速讀
來(lái)源:博客園     時(shí)間:2023-04-20 19:06:11


(資料圖片)

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)簽:

廣告

X 關(guān)閉

廣告

X 關(guān)閉