您好,登录后才能下订单哦!
这篇文章主要讲解了“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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。