SpringBoot 反向代理

1,063 阅读1分钟

摘要:Springboot的反向代理主要是为了不引入SpringCloud Gateway而实现流量在不同应用之间的转发,并进行数据过滤或者权限添加等操作

核心代码如下

pom.xml

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.mitre.dsmiley.httpproxy</groupId>
        <artifactId>smiley-http-proxy-servlet</artifactId>
        <version>1.12.1</version>
    </dependency>
</dependencies>

application.yml

proxy:
  enabled: true
  show-log: true
  proxy-attributes:
    - servletUrl: /baidu/*
      targetUrl: http://apis.juhe.cn

ProxyProperties

该文件用作与application.yml文件的配置关联起来

@ConfigurationProperties(prefix = ProxyProperties.PREFIX, ignoreUnknownFields = false)
public class ProxyProperties {

    public static final String PREFIX = "proxy";

    public static final String CONDITIONAL = PREFIX + "." + "enabled";

    /** 是否开启 */
    private Boolean enabled = Boolean.FALSE;
    /** 是否打印日志 */
    private Boolean showLog = Boolean.FALSE;

    /** 代理的属性 */
    private List<ProxyAttribute> proxyAttributes;

    public Boolean getEnabled() {
        return enabled;
    }

    public void setEnabled(Boolean enabled) {
        this.enabled = enabled;
    }

    public Boolean getShowLog() {
        return showLog;
    }

    public void setShowLog(Boolean showLog) {
        this.showLog = showLog;
    }

    public List<ProxyAttribute> getProxyAttributes() {
        return proxyAttributes;
    }

    public void setProxyAttributes(List<ProxyAttribute> proxyAttributes) {
        this.proxyAttributes = proxyAttributes;
    }

    /**
     * 代理的属性配置
     */
    public static class ProxyAttribute{

        /** 路由路径 */
        private String servletUrl;

        /** 实际转发的目的地 */
        private String targetUrl;

        public String getServletUrl() {
            return servletUrl;
        }

        public void setServletUrl(String servletUrl) {
            this.servletUrl = servletUrl;
        }

        public String getTargetUrl() {
            return targetUrl;
        }

        public void setTargetUrl(String targetUrl) {
            this.targetUrl = targetUrl;
        }
    }

}

ProxyAutoConfiguration

该类是一个自动配置类,后面可以考虑写一个类似Springboot自动装配的jar包;该类主要作用是把Servlet注入到Spring的管理中,

@Configuration
@EnableConfigurationProperties(value = ProxyProperties.class)
@ConditionalOnProperty(value = ProxyProperties.CONDITIONAL, havingValue = "true")
public class ProxyAutoConfiguration implements ServletContextInitializer {

    @Autowired
    private ProxyProperties proxyProperties;

    @Override
    public void onStartup(ServletContext servletContext) {
        if(!Boolean.TRUE.equals(proxyProperties.getEnabled()) || CollectionUtils.isEmpty(proxyProperties.getProxyAttributes())){
            return;
        }
        for (int i = 0; i < proxyProperties.getProxyAttributes().size(); i++) {
            ProxyProperties.ProxyAttribute proxyAttribute = proxyProperties.getProxyAttributes().get(i);
            ServletRegistration initServlet = servletContext.addServlet(proxyAttribute.getServletUrl(), ProxyServlet.class);
            initServlet.addMapping(proxyAttribute.getServletUrl());
            initServlet.setInitParameter(ProxyServlet.P_TARGET_URI, proxyAttribute.getTargetUrl());
            initServlet.setInitParameter(ProxyServlet.P_LOG, proxyProperties.getShowLog().toString());
            initServlet.setInitParameter(ProxyServlet.P_PRESERVECOOKIES, Boolean.TRUE.toString());
        }
    }

}

访问案例

image.png

高级使用