前言
? ? ? ? nginx可以實現(xiàn)反向代理,但是有時候需要使用java代碼來實現(xiàn),經(jīng)過摸索,發(fā)現(xiàn)有開源的項目可以實現(xiàn),所以簡單記錄一下如何使用
一、引入依賴
? ? ? ? 沒啥好說
<dependency>
<groupId>org.mitre.dsmiley.httpproxy</groupId>
<artifactId>smiley-http-proxy-servlet</artifactId>
<version>1.12.1</version>
</dependency>
二、重寫Servlet
? ? ? ? 該項目的核心類是ProxyServlet,主要操作都在這個類中實現(xiàn)了,我們可以繼承該類,重寫其中的方法,自定義實現(xiàn)一些功能。
? ? ? ? 這里我們繼承ProxyServlet,重寫了createHttpClient方法,使其跳過ssl認(rèn)證
@Slf4j
public class CustomProxyServlet extends ProxyServlet {
/**
* 重寫HttpClient,跳過ssl認(rèn)證
*
* @return {@link HttpClient}
*/
@Override
protected HttpClient createHttpClient() {
HttpClientBuilder clientBuilder = getHttpClientBuilder()
.setDefaultRequestConfig(buildRequestConfig())
.setDefaultSocketConfig(buildSocketConfig());
clientBuilder.setMaxConnTotal(maxConnections);
clientBuilder.setMaxConnPerRoute(maxConnections);
if(! doHandleCompression) {
clientBuilder.disableContentCompression();
}
if (useSystemProperties){
clientBuilder.useSystemProperties();
}
SSLContext sslContext = this.getSslContext();
SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(sslContext, NoopHostnameVerifier.INSTANCE);
clientBuilder.setSSLSocketFactory(sslSocketFactory);
return super.buildHttpClient(clientBuilder);
}
/**
* 獲取sslContext
* @return {@link SSLContext}
*/
public SSLContext getSslContext() {
SSLContext sslContext = null;
try {
sslContext = SSLContextBuilder.create()
.loadTrustMaterial(TrustAllStrategy.INSTANCE)
.build();
} catch (NoSuchAlgorithmException | KeyManagementException | KeyStoreException e) {
log.error("獲取sslContext失敗", e);
}
return sslContext;
}
}
三、添加配置
? ? ? ? 添加配置類配置一下代理信息
代理信息配置
@Data
public class ProxyProperties {
/**
* 映射
*/
private String mapping;
/**
* 目標(biāo)url
*/
private String targetUrl;
}
添加配置,控制是否啟代理
@Data
@ConfigurationProperties(prefix = "proxy")
public class ProxyConfig {
/**
* 啟用日志
*/
private boolean enableLog;
/**
* 配置
*/
private List<ProxyProperties> configs;
}
自動裝配,獲取自定義配置信息,通過for循環(huán)配置多個servlet文章來源:http://www.zghlxwxcb.cn/news/detail-696113.html
import lombok.RequiredArgsConstructor;
import org.mitre.dsmiley.httpproxy.ProxyServlet;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.servlet.ServletContextInitializer;
import org.springframework.context.annotation.Configuration;
import javax.servlet.ServletContext;
import javax.servlet.ServletRegistration;
import java.util.List;
@Configuration
@RequiredArgsConstructor
@EnableConfigurationProperties(ProxyConfig.class)
public class ProxyAutoConfiguration implements ServletContextInitializer {
private final ProxyConfig proxyConfig;
/**
* Configure the given {@link ServletContext} with any servlets, filters, listeners
* context-params and attributes necessary for initialization.
*
* @param servletContext the {@code ServletContext} to initialize
*/
@Override
public void onStartup(ServletContext servletContext) {
List<ProxyProperties> configs = proxyConfig.getConfigs();
for (int i = 0; i < configs.size(); i++) {
ProxyProperties properties = configs.get(i);
//定義多個servlet
ServletRegistration initServlet = servletContext.addServlet("ProxyServlet"+i, CustomProxyServlet.class);
initServlet.addMapping(properties.getMapping());
initServlet.setInitParameter(ProxyServlet.P_TARGET_URI, properties.getTargetUrl());
initServlet.setInitParameter(ProxyServlet.P_FORWARDEDFOR, "false");
initServlet.setInitParameter(ProxyServlet.P_LOG, Boolean.toString(proxyConfig.isEnableLog()));
}
}
}
寫在后面的話
? ? ? ? 百度使用方法,大部分只能代理一個地址,而且不支持跳過ssl認(rèn)證,所以這里記錄一下,希望對你有用。文章來源地址http://www.zghlxwxcb.cn/news/detail-696113.html
到了這里,關(guān)于java使用smiley-http-proxy-servlet實現(xiàn)反向代理,跳過SSL認(rèn)證的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!