欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot過(guò)濾器實(shí)現(xiàn)項(xiàng)目?jī)?nèi)接口過(guò)濾詳解

 更新時(shí)間:2023年04月20日 15:45:46   作者:黑子桑  
這篇文章主要為大家詳細(xì)介紹了SpringBoot如何利用過(guò)濾器實(shí)現(xiàn)項(xiàng)目?jī)?nèi)接口過(guò)濾,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起了解一下

業(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)的接口判斷

處理步驟

1.獲取路由中心系統(tǒng)中接口映射

    private static List<String> 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ò)濾器初始化");
    }

2.獲取請(qǐng)求接口路徑

        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        String requestName = request.getRequestURI();

3.判斷是否是路由中心的接口

        if (URLS.contains(requestName)) {
		//系統(tǒng)接口
            chain.doFilter(servletRequest, servletResponse);
        } else {
		//業(yè)務(wù)系統(tǒng)接口 需要代理
//            代理請(qǐng)求
            ResponseEntity<String> 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")
@Slf4j
public class RouterFilter implements Filter {

    private static List<String> 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<String> 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)

spring boot實(shí)現(xiàn)超輕量級(jí)網(wǎng)關(guān)的方法(反向代理、轉(zhuǎn)發(fā))

到此這篇關(guān)于SpringBoot過(guò)濾器實(shí)現(xiàn)項(xiàng)目?jī)?nèi)接口過(guò)濾詳解的文章就介紹到這了,更多相關(guān)SpringBoot項(xiàng)目接口過(guò)濾內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論