您好,登錄后才能下訂單哦!
這篇文章主要講解了“SpringBoot過濾器怎么實現項目內接口過濾”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“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(); }
感謝各位的閱讀,以上就是“SpringBoot過濾器怎么實現項目內接口過濾”的內容了,經過本文的學習后,相信大家對SpringBoot過濾器怎么實現項目內接口過濾這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。