Java教程

spring boot过滤器实现项目内接口过滤

本文主要是介绍spring boot过滤器实现项目内接口过滤,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

spring boot过滤器实现项目内接口过滤

业务

由于业务需求,存在两套项目,一套是路由中心,一套是业务系统.
现在存在问题是,路由中心集成了微信公众号与小程序模块功能,业务系统部署了多套服务.
现在需要通过调用路由中心将接口重新路由到指定的业务系统中

image

需要处理的问题

  1. 将小程序,公众号用户信息与业务系统做绑定
  2. 将路由中心的接口与业务系统的接口判断出来
  3. 通过用户信息找到的业务系统服务,分发到对应的业务系统中

公众号用户信息与业务系统做绑定

处理步骤

  1. 业务系统存在手机号,如果用户注册将手机号发送给路由中心记录

将路由中心的接口与业务系统的接口判断

处理步骤

  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("过滤器初始化");
    }
  1. 获取请求接口路径
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        String requestName = request.getRequestURI();
  1. 判断是否是路由中心的接口
        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();
    }

代理转发实现

https://www.cnblogs.com/shenjun980326/p/spring-boot-route.html

这篇关于spring boot过滤器实现项目内接口过滤的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!