由於業務需求,存在兩套項目,一套是路由中心,一套是業務系統.
現在存在問題是,路由中心集成了微信公眾號與小程式模組功能,業務系統部署了多套服務.
現在需要透過呼叫路由中心將介面重新路由到指定的業務系統中
將小程式,公眾號用戶資訊與業務系統做綁定
將路由中心的介面與業務系統的介面判斷出來
透過用戶資訊找到的業務系統服務,分發到對應的業務系統中
處理步驟
業務系統存在手機號碼,如果用戶註冊將手機號碼傳送給路由中心記錄
#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過濾器怎麼實現專案內介面過濾的詳細內容。更多資訊請關注PHP中文網其他相關文章!