springboot实现请求转发

1.在pom.xml新增依赖

<dependency>
  <groupId>org.mitre.dsmiley.httpproxy</groupId>
  <artifactId>smiley-http-proxy-servlet</artifactId>
  <version>1.12</version>
</dependency>

2.新增ProxyServletConfig类

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.mitre.dsmiley.httpproxy.ProxyServlet;

@Configuration
public class ProxyServletConfig {

    @Value("${proxy.bbmax.sourceUrl}")
    private String servletUrl;

    @Value("${proxy.bbmax.targetUrl}")
    private String targetUrl;

    @Value("${proxy.bbmax.log}")
    private String log;

    @Bean
    @SuppressWarnings(value = {"unchecked", "rawtypes"})
    public ServletRegistrationBean proxyServletRegistration() {
        ServletRegistrationBean registrationBean = new ServletRegistrationBean(new ProxyServlet(), servletUrl);
        registrationBean.setName("fingerprintGateway");
        registrationBean.addInitParameter("targetUri", targetUrl);
        registrationBean.addInitParameter(ProxyServlet.P_LOG, log);
        return registrationBean;
    }
}

3.配置application.yml

server:
  port: 9093
  max-http-header-size: 100KB
  tomcat:
    threads:
      max: 1000
    max-connections: 1000
  ssl:
    key-store: classpath:6072302_www.bbmax.cc.jks
    key-store-type: jks
    key-store-password: password

proxy:
  bbmax:
    sourceUrl: /bbmaxcc/*
    targetUrl: http://ip:9090/bbmaxcc
    log: true

4.请求测试

启动9093和9090服务,在浏览器访问:http://localhost:9093/bbmaxcc,在9093服务中查看是否有请求进入

如果觉得我的文章对你有用,请随意赞赏