SpringBoot過濾器實現項目內接口過濾詳解
業務
由於業務需求,存在兩套項目,一套是路由中心,一套是業務系統.
現在存在問題是,路由中心集成瞭微信公眾號與小程序模塊功能,業務系統部署瞭多套服務.
現在需要通過調用路由中心將接口重新路由到指定的業務系統中
需要處理的問題
- 將小程序,公眾號用戶信息與業務系統做綁定
- 將路由中心的接口與業務系統的接口判斷出來
- 通過用戶信息找到的業務系統服務,分發到對應的業務系統中
公眾號用戶信息與業務系統做綁定
處理步驟
業務系統存在手機號,如果用戶註冊將手機號發送給路由中心記錄
將路由中心的接口與業務系統的接口判斷
處理步驟
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項目接口過濾內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!
推薦閱讀:
- CorsFilter 過濾器解決跨域的處理
- Java servlet通過事件驅動進行高性能長輪詢詳解
- java web項目Session獲取不到問題及解決
- Java中使用Filter過濾器的方法
- Java精品項目瑞吉外賣之登陸的完善與退出功能篇