spring-boot 实现接口转发服务,同时支持get 和 post等多种请求
(1)新建类:ProxyController.java
package com.taobao.product.controller;
import com.taobao.framework.HttpResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import java.net.URISyntaxException;
import java.util.Enumeration;
import org.springframework.http.MediaType;
import java.io.IOException;
import java.net.URI;
import java.util.Collections;
import java.util.List;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.client.HttpClients;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
/**
* @Author: sunkaiyuan
* @Date: 2023-05-23 9:02
* @Desperation: TODO
*/
@Api(tags = "代理服务")
@RestController
@RequestMapping("/proxy")
@Slf4j
public class ProxyController {
@ApiOperation(value = "接口转发")
@RequestMapping("/dahua/**")
public ResponseEntity<String> handleRequest(HttpServletRequest request) throws IOException, URISyntaxException, NoSuchAlgorithmException, KeyStoreException, KeyManagementException {
String method = request.getMethod();
String path = getPath(request);
// 去掉url中的/proxy/dahua
path = path.substring(request.getContextPath().length() + "/proxy/dahua".length());
URI targetUri = new URI("https://121.26.142.174:8443" + path);
System.out.println(targetUri);
HttpHeaders headers = getRequestHeaders(request);
HttpEntity<?> entity = new HttpEntity<>(headers);
RestTemplate restTemplate = new RestTemplate(getSecureHttpRequestFactory());
if (method.equalsIgnoreCase(HttpMethod.GET.name())) {
return restTemplate.exchange(targetUri, HttpMethod.GET, entity, String.class);
} else if (method.equalsIgnoreCase(HttpMethod.POST.name())) {
String requestBody = getRequestBody(request);
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<String> postEntity = new HttpEntity<>(requestBody, headers);
return restTemplate.exchange(targetUri, HttpMethod.POST, postEntity, String.class);
} else {
return ResponseEntity.badRequest().body("Unsupported request method: " + method);
}
}
private String getPath(HttpServletRequest request) {
String contextPath = request.getContextPath();
String servletPath = request.getServletPath();
String pathInfo = request.getPathInfo() != null ? request.getPathInfo() : "";
return contextPath + servletPath + pathInfo;
}
private HttpHeaders getRequestHeaders(HttpServletRequest request) {
HttpHeaders headers = new HttpHeaders();
Enumeration<String> headerNames = request.getHeaderNames();
while (headerNames.hasMoreElements()) {
String headerName = headerNames.nextElement();
List<String> headerValues = Collections.list(request.getHeaders(headerName));
headers.put(headerName, headerValues);
}
return headers;
}
private String getRequestBody(HttpServletRequest request) throws IOException {
return request.getReader().lines().reduce("", (accumulator, actual) -> accumulator + actual);
}
private HttpComponentsClientHttpRequestFactory getSecureHttpRequestFactory() throws NoSuchAlgorithmException, KeyStoreException, KeyManagementException {
TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
public X509Certificate[] getAcceptedIssuers() {
return null;
}
public void checkClientTrusted(X509Certificate[] certs, String authType) {
}
public void checkServerTrusted(X509Certificate[] certs, String authType) throws CertificateException {
}
} };
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, trustAllCerts, null);
SSLConnectionSocketFactory csf = new SSLConnectionSocketFactory(sslContext, NoopHostnameVerifier.INSTANCE);
HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
requestFactory.setHttpClient(HttpClients.custom().setSSLSocketFactory(csf).build());
return requestFactory;
}
}
(2)代码说明:
这是一个 Java 类,名称为 ProxyController
。代码中包含以下方法:
handleRequest(HttpServletRequest request)
这是一个公共方法,返回类型为 ResponseEntity<String>
,会抛出一些可能的异常。该方法的目的是根据传入的请求对象处理 HTTP 请求并返回相应的响应。该方法被注解 @RequestMapping("/dahua/**")
所标记,它表示当 URL 包含 /proxy/dahua
的时候,该方法才会被调用。该方法实现了将传入的请求转发到另一个 URL 并返回响应的功能。如果请求不是 GET 或 POST,则会返回一个错误响应。
getPath(HttpServletRequest request)
这是一个私有方法,返回类型为 String
。该方法接收一个请求对象,从中获取上下文路径、Servlet 路径和路径信息,并将它们组合成完整的 URL 路径并返回。
getRequestHeaders(HttpServletRequest request)
这是一个私有方法,返回类型为 HttpHeaders
。该方法接收一个请求对象,从中获取所有请求头,并将它们放入一个新的 HttpHeaders
对象中,然后返回该对象。
getRequestBody(HttpServletRequest request)
这是一个私有方法,返回类型为 String
,可能会抛出 IO 异常。该方法接收一个请求对象,从中获取请求体(如果有),将其转换为字符串,并返回该字符串。
getSecureHttpRequestFactory()
这是一个私有方法,返回类型为 HttpComponentsClientHttpRequestFactory
,可能会抛出一些异常。该方法的目的是创建一个安全的 HTTP 请求工厂,使我们可以通过 HTTPS 连接到另一个 URL。该方法使用 TrustManager
和 SSLContext
来实现信任所有证书,并将其设置为请求工厂中的 SSL 套接字工厂,然后返回该请求工厂。文章来源:https://www.toymoban.com/news/detail-703365.html
除此之外,代码还包含一些注解和变量声明。其中,@ApiOperation
注解表示该方法具有操作说明信息;@RequestMapping("/dahua/**")
表示当 URL 包含 /proxy/dahua
的时候,该方法才会被调用。变量 method
、path
、targetUri
、headers
、entity
、restTemplate
、postEntity
、requestFactory
等在代码中都有对应的使用。文章来源地址https://www.toymoban.com/news/detail-703365.html
到了这里,关于spring-boot 实现接口转发服务,同时支持get 和 post等多种请求的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!