Springboot2.0配置请求服务代理
程序员文章站
2022-05-18 10:01:32
...
Springboot2.0配置请求服务代理
注:下图内容源自博客https://blog.csdn.net/wohaqiyi/article/details/81327512该博主版本为2.0以下
借鉴方法思路实现功能发现org.springframework.boot.bind这个包早已删掉,
org.springframework.boot.bind.RelaxedPropertyResolver;
具体解决办法如下:
package com.siyeto.cweb.common.configuration;
import java.io.IOException;
import java.util.Properties;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.boot.context.properties.bind.BindResult;
import org.springframework.boot.context.properties.bind.Binder;
import org.springframework.boot.context.properties.source.ConfigurationPropertySource;
import org.springframework.boot.context.properties.source.ConfigurationPropertySources;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
@SuppressWarnings({ "unchecked", "rawtypes" })
@Configuration
public class SolrProxyServletConfiguration implements EnvironmentAware {
private Properties properties;
@Bean
public ServletRegistrationBean servletRegistrationBean(HttpServletRequest servletRequest,HttpServletResponse servletResponse) throws ServletException, IOException{
ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new MyProxyServlet(),properties.getProperty("dataServer-servlet-url"));
servletRegistrationBean.setName("dataServer");
servletRegistrationBean.addInitParameter("targetUri", properties.getProperty("dataServer-target-url"));
servletRegistrationBean.addInitParameter(MyProxyServlet.P_LOG, properties.getProperty("logging_enabled", "false"));
return servletRegistrationBean;
}
@Override
public void setEnvironment(Environment environment) {
/**
* springboot2.X 升级后改用ConfigurationPropertySource进行绑定
*/
Iterable<ConfigurationPropertySource> sources = ConfigurationPropertySources.get(environment);
Binder binder = new Binder(sources);
BindResult<Properties> bindResult = binder.bind("proxy.solr", Properties.class);
properties = bindResult.get();
}
}
新建类继承ProxyServlet重写doExecute方法
package com.siyeto.cweb.common.configuration;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.mitre.dsmiley.httpproxy.ProxyServlet;
import com.siyeto.cweb.common.utill.HMACSHA256;
import com.siyeto.cweb.common.utill.SpringContextUtils;
import com.siyeto.cweb.modules.management.service.CompanyService;
/**
* @Description: 自定义servlet
* @author: lzh
* @date: 2020年3月4日 上午9:44:29
*/
public class MyProxyServlet extends ProxyServlet {
private static final long serialVersionUID = 1L;
private static final String ALLOWED_HEADERS = "Origin, Accept, Content-Type, x-requested-with, X-Forwarded-Prefix, token, lang, id, timestamp";
/**
* proxyServlet.service(servletRequest, servletResponse);
* 需要重写doExecute()方法
*/
@Override
protected HttpResponse doExecute(HttpServletRequest servletRequest, HttpServletResponse servletResponse,
HttpRequest proxyRequest) throws IOException {
String companyId = servletRequest.getHeader("companyId");
String currentTimeMillis = System.currentTimeMillis()+"";
CompanyService companyService = (CompanyService) SpringContextUtils.getBean("companyService");
String key = companyService.selectCompanyKey(Long.valueOf(companyId));
HttpResponse response = super.doExecute(servletRequest, servletResponse, proxyRequest);
response.setHeader("Access-Control-Allow-Origin","*");
response.setHeader("Access-Control-Allow-Credentials","true");
response.setHeader("Access-Control-Allow-Methods","*");
response.setHeader("Access-Control-Allow-Headers",ALLOWED_HEADERS);
response.setHeader("token", HMACSHA256.encrytSHA256(currentTimeMillis,key));
response.setHeader("timestamp", currentTimeMillis);
response.setHeader("id", companyId);
response.setHeader("lang", servletRequest.getHeader("lang"));
return response;
}
}
因为需求需要添加请求头,所以仔细阅读ProxyServlet源码中的service方法解决问题