当前位置: 首页 > news >正文

【请求代理】springboot单机服务基于过滤器Filter实现第三方服务器接口请求代理功能

springboot单机服务基于过滤器Filter实现第三方服务器接口请求代理功能

    • 一、前言
    • 二、解决思路
    • 三、基于gateway实现
    • 四、基于过滤器Filter实现
    • 五、问题总结

**注:本文源码获取或者更多资料,关注公众号:技术闲人**

一、前言

在项目开发时会遇到web端/接口请求第三方服务接口的场景,对web请求来说最后请求的服务地址是一个,避免跨域问题,在微服务场景经常使用gateway等网关服务实现,或者使用nginx代理组件实现,但是不同三方服务有不同的鉴权要求,但是后端服务最好有相同的鉴权;
在这里插入图片描述

二、解决思路

在非微服务架构和三方不同鉴权接口的服务场景下,通过过滤器Filter实现请求转发,并使用适配器设计模式,兼容不同的三方服务请求(鉴权),减少重复代码开发,也能监控所有的服务请求,并对所有请求做限流、统计等操作;

三、基于gateway实现

在没有spring-boot-starter-web依赖的场景下可以使用gateway,Spring MVC(基于Servlet的Web应用程序)和Spring Cloud Gateway(基于反应式编程的API网关),但是这两个组件是不兼容的。Spring Cloud Gateway是专为反应式编程设计的,使用Spring WebFlux作为底层框架,而Spring MVC则基于Servlet API。

gateway实现代码:

package com.sk.proxytest;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;@SpringBootApplication
public class ProxyTestApplication {public static void main(String[] args) {SpringApplication.run(ProxyTestApplication.class, args);}@Beanpublic RouteLocator myRoutes(RouteLocatorBuilder builder) {return builder.routes().route(p -> p.path("/test/**").uri("http://127.0.0.1:8089/api")).build();}}

四、基于过滤器Filter实现

本文主要使用过滤器Filter实现,既能控制代理请求,又能最少开发量;

GET请求结果
在这里插入图片描述
POST请求结果
在这里插入图片描述
实现源码:

ProxyFilter.java

package com.sk.proxytest.proxy;import com.sk.proxytest.proxy.bean.ProxyParam;
import com.sk.proxytest.proxy.bean.ProxyResult;
import com.sk.proxytest.proxy.strategy.ProxyHandleService;
import com.sk.proxytest.proxy.strategy.ProxyHandleStrategyFactory;
import com.sk.proxytest.proxy.strategy.ProxyStrategyContext;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;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.io.PrintWriter;
import java.util.Map;@Slf4j
@Configuration
@WebFilter(filterName = "ProxyFilter", urlPatterns = "/proxy/*")
public class ProxyFilter implements Filter {@Resourceprivate RestTemplate restTemplate;@Overridepublic void init(FilterConfig filterConfig) throws ServletException {Filter.super.init(filterConfig);}@Overridepublic void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain) throws IOException, ServletException {HttpServletRequest request = (HttpServletRequest) servletRequest;HttpServletResponse response = (HttpServletResponse) servletResponse;String proxyType = request.getHeader("proxy-type");ProxyStrategyContext proxyStrategyContext = new ProxyStrategyContext();ProxyHandleService proxyHandleService = ProxyHandleStrategyFactory.getProxyHandleStrategy(proxyType);proxyStrategyContext.setProxyHandleStrategy(proxyHandleService);ProxyResult proxyResult = proxyStrategyContext.handleProxy(new ProxyParam());boolean flag = true;if (null != proxyResult) {PrintWriter writer = null;try {String body = IOUtils.toString(request.getInputStream());HttpEntity<?> entity = new HttpEntity<>(body, proxyResult.getHeaders());String url = proxyResult.getProxyUrl() + getNewUrl(request);log.info("-----------new-url:{}", url);ResponseEntity<String> responseEntity = restTemplate.exchange(url, HttpMethod.resolve(request.getMethod()), entity, String.class);response.setStatus(responseEntity.getStatusCodeValue());writer = response.getWriter();writer.write(responseEntity.getBody());writer.flush();flag = false;} catch (Exception e) {log.error("------error:{}", e);} finally {if (writer != null) {writer.close();}}}if (flag) {chain.doFilter(request, response);}}@Overridepublic void destroy() {Filter.super.destroy();}//获取被代理的url和参数private String getNewUrl(HttpServletRequest request) {String proxyUrl = request.getRequestURI().replace("/proxy", "");Map<String, String[]> parameterMap = request.getParameterMap();int i = 0;for (Map.Entry<String, String[]> entry : parameterMap.entrySet()) {String key = entry.getKey();String value = entry.getValue()[0];if (i == 0) {proxyUrl = proxyUrl + "?" + key + "=" + value;} else {proxyUrl = proxyUrl + "&" + key + "=" + value;}}return proxyUrl;}
}

ProxyHandleService.java

package com.sk.proxytest.proxy.strategy;import com.sk.proxytest.proxy.bean.ProxyParam;
import com.sk.proxytest.proxy.bean.ProxyResult;public interface ProxyHandleService {ProxyResult proxyHandle(ProxyParam proxyParam);}

AlibabaProxyHandleStrategy.java

package com.sk.proxytest.proxy.strategy;import com.sk.proxytest.proxy.bean.ProxyParam;
import com.sk.proxytest.proxy.bean.ProxyResult;
import org.springframework.http.HttpHeaders;public class AlibabaProxyHandleStrategy implements ProxyHandleService {@Overridepublic ProxyResult proxyHandle(ProxyParam proxyParam) {HttpHeaders headers = new HttpHeaders();//TODO 根据三方服务登录接口获取鉴权信息String token = "token--------";headers.add("token", token);headers.add("Content-Type","application/json");//三方服务ip和portString ip = "127.0.0.1";String port = "8080";String proxyUrl = "http://" + ip + ":" + port;return new ProxyResult(headers, proxyUrl);}
}

ProxyHandleStrategyFactory.java

package com.sk.proxytest.proxy.strategy;import java.util.HashMap;
import java.util.Map;public class ProxyHandleStrategyFactory {private static Map<String, ProxyHandleService> proxyHandleServiceMap;static {proxyHandleServiceMap = new HashMap<>();proxyHandleServiceMap.put("alibaba", new AlibabaProxyHandleStrategy());}public static ProxyHandleService getProxyHandleStrategy(String proxyType){return proxyHandleServiceMap.get(proxyType);}}

ProxyStrategyContext.java

package com.sk.proxytest.proxy.strategy;import com.sk.proxytest.proxy.bean.ProxyParam;
import com.sk.proxytest.proxy.bean.ProxyResult;public class ProxyStrategyContext {private ProxyHandleService proxyHandleService;public void setProxyHandleStrategy(ProxyHandleService proxyHandleService){this.proxyHandleService = proxyHandleService;}public ProxyResult handleProxy(ProxyParam proxyParam){if(proxyHandleService != null){return proxyHandleService.proxyHandle(proxyParam);}return null;}}

五、问题总结

在单机服务中,gateway过于重,并且与springMVC有冲突,nginx代理服务不能同一鉴权,或者同一鉴权太过于麻烦,过滤器Filter+适配器模式正好满足我们业务场景需求;
功能实现的方式选择还是要考虑业务场景。

http://www.lryc.cn/news/409457.html

相关文章:

  • .NET Core异步编程与多线程解析:提升性能与响应能力的关键技术
  • Photoshop(PS) 抠图简单教程
  • 项目管理中的常用工件(二):可视化工件
  • Git入门与实战:版本控制的艺术
  • [Mysql-DML数据操作语句]
  • Tableau入门|数据可视化与仪表盘搭建
  • API 技术开发分享:连接电商平台数据获取的桥梁
  • 区块链如何助力数字版权保护和内容创作者的权益?
  • 记一次老旧项目的整体技术升级
  • 2024年最受欢迎的五大上网审计设备和软件
  • sed利用脚本处理文件
  • 泰山派RK3566开发板800x1280MIPI屏设备树补丁
  • informer中的indexer机制的实现分析与源码解读
  • 英特尔宣布针对对Llama 3.1进行优化 以提升所有产品的性能
  • Python3网络爬虫开发实战(1)爬虫基础
  • Redis的五种数据类型与命令
  • RocketMQ的详细讲解(四种mq的对比(activeMq、rabbitmq、rocketmq、kafka))
  • 除了GPT,还有哪些好用的AI工具?
  • 04 | 深入浅出索引(上)
  • Linux的yum源安装MySQL5.7
  • 基于深度学习的音频自监督学习
  • 用uniapp 及socket.io做一个简单聊天app1
  • 在Postman中引用JS库
  • 学习笔记-系统框图简化求传递函数公式例题
  • postgrsql——事务概述
  • 1.Spring Boot 简介(Spring MVC+Mybatis-plus)
  • 《计算机网络》(学习笔记)
  • 指针函数和函数指针
  • Elasticsearch跨集群搜索
  • 基于FPGA的数字信号处理(19)--行波进位加法器