首頁 > 軟體

SpringBoot過濾器實現專案內介面過濾詳解

2023-08-28 18:05:39

業務

由於業務需求,存在兩套專案,一套是路由中心,一套是業務系統.

現在存在問題是,路由中心整合了微信公眾號與小程式模組功能,業務系統部署了多套服務.

現在需要通過呼叫路由中心將介面重新路由到指定的業務系統中

需要處理的問題

  • 將小程式,公眾號使用者資訊與業務系統做繫結
  • 將路由中心的介面與業務系統的介面判斷出來
  • 通過使用者資訊找到的業務系統服務,分發到對應的業務系統中

公眾號使用者資訊與業務系統做繫結

處理步驟

業務系統存在手機號,如果使用者註冊將手機號傳送給路由中心記錄

將路由中心的介面與業務系統的介面判斷

處理步驟

1.獲取路由中心繫統中介面對映

    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("過濾器初始化");
    }

2.獲取請求介面路徑

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

3.判斷是否是路由中心的介面

        if (URLS.contains(requestName)) {
		//系統介面
            chain.doFilter(servletRequest, servletResponse);
        } else {
		//業務系統介面 需要代理
//            代理請求
            ResponseEntity<String> redirect = routerService.redirect(request, response, "xxx", "xxx");
            //設定將字元以"UTF-8"編碼輸出到使用者端瀏覽器
            response.setCharacterEncoding("UTF-8");
            //通過設定響應頭控制瀏覽器以UTF-8的編碼顯示資料,如果不加這句話,那麼瀏覽器顯示的將是亂碼
            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("過濾器初始化");
    }

    @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();

        /*順手解決跨域問題*/
        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 {
//            代理請求
            ResponseEntity<String> redirect = routerService.redirect(request, response, "http://192.168.2.18/api", "/router");


            //設定將字元以"UTF-8"編碼輸出到使用者端瀏覽器
            response.setCharacterEncoding("UTF-8");
            //通過設定響應頭控制瀏覽器以UTF-8的編碼顯示資料,如果不加這句話,那麼瀏覽器顯示的將是亂碼
            response.setHeader("content-type", "application/json; charset=utf-8");
            response.getWriter().write(redirect.getBody());
        }
        log.info("--------------------------------------------------------");
    }

    @Override
    public void destroy() {
        Filter.super.destroy();
    }

代理轉發實現

spring boot實現超輕量級閘道器的方法(反向代理、轉發)

到此這篇關於SpringBoot過濾器實現專案內介面過濾詳解的文章就介紹到這了,更多相關SpringBoot專案介面過濾內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com