nginx 虽然简单,但是总要多装一个服务,多个风险。话不多说,直接上代码!!
1.引入jar包
<dependency>
<groupId>org.mitre.dsmiley.httpproxy</groupId>
<artifactId>smiley-http-proxy-servlet</artifactId>
<version>1.7</version>
</dependency>
2.在yml添加配置
#代理文件服务为例
proxy:
#上传
uploading:
servlet_url: /uploading/*
target_url: http://192.168.0.142/file
#下载
download:
servlet_url: /download/*
target_url: http://192.168.0.142/files
3.配置类
package com.kc.framework.config;
import org.mitre.dsmiley.httpproxy.ProxyServlet;
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;
@Configuration
public class SolrProxyServletConfig{
// 上传
@Value("${proxy.uploading.servlet_url}")
private String servletUrlUploading;
@Value("${proxy.uploading.target_url}")
private String targetUrlUploading;
// 下载
@Value("${proxy.download.servlet_url}")
private String servletUrlDownload;
@Value("${proxy.download.target_url}")
private String targetUrlDownload;
@Bean
@SuppressWarnings(value={"unchecked", "rawtypes"})
public ServletRegistrationBean proxyServletRegistrationDownload(){
ServletRegistrationBean registrationBean = new ServletRegistrationBean(new ProxyServlet(), servletUrlDownload);
//这里名字必须不一样
registrationBean.setName("suitDownload");
registrationBean.addInitParameter("targetUri", targetUrlDownload);
registrationBean.addInitParameter(ProxyServlet.P_LOG, "true");
return registrationBean;
}
@Bean
@SuppressWarnings(value={"unchecked", "rawtypes"})
public ServletRegistrationBean proxyServletRegistrationUploading(){
ServletRegistrationBean registrationBean = new ServletRegistrationBean(new ProxyServlet(), servletUrlUploading);
//这里名字必须不一样
registrationBean.setName("suitUploading");
registrationBean.addInitParameter("targetUri", targetUrlUploading);
registrationBean.addInitParameter(ProxyServlet.P_LOG, "true");
return registrationBean;
}
}
网友评论