在 WEB 应用中使用反向代理功能来将 HTTP 服务转换为 HTTPS 服务

在 WEB 应用中使用反向代理功能来将 HTTP 服务转换为 HTTPS 服务

💡 说明: 推荐使用 Servlet+OkHttp

使用 Servlet+httpclient

在 Java EE 项目中,可以通过创建一个自定义的 Servlet 来实现类似的代理功能。以下是一个简单的示例,展示了如何将 HTTP 服务代理到您的 Java EE 应用中:



  1. 添加依赖

在您的项目的pom.xml文件中,添加javax.servlet-apihttpclient依赖(如果尚未添加):

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>4.0.1</version>
    <scope>provided</scope>
</dependency>
<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.5.13</version>
</dependency> 
  1. 创建一个新的 Servlet

在您的 Java EE 项目中,创建一个新的 Servlet 类,用于处理代理请求:

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/proxy/*")
public class ProxyServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String path = req.getPathInfo(); // Get the path after /proxy/
        String targetUrl = "<http://example.com>" + path; // Replace with the actual HTTP service URL
        HttpClient httpClient = HttpClients.createDefault();
        HttpGet httpGet = new HttpGet(targetUrl);
        // Add any necessary headers here

        HttpResponse httpResponse = httpClient.execute(httpGet);
        resp.setStatus(httpResponse.getStatusLine().getStatusCode());
        resp.setContentType("text/html"); // Set the appropriate content type, if required
        resp.getWriter().write(EntityUtils.toString(httpResponse.getEntity()));
    }
} 

在这个示例中,我们创建了一个名为ProxyServlet的新 Servlet 类,它使用 Apache HttpClient 将请求代理到目标 HTTP 服务。您可以根据需要修改targetUrl变量以指向实际的 HTTP 服务 URL。

  1. 在 iframe 中嵌入代理服务

现在,您可以在 iframe 中使用您的 Java EE 应用作为代理来访问 HTTP 服务。例如:

<iframe src="<https://your-java-ee-app.com/proxy/some-path>" width="100%" height="100%"></iframe>

请注意,这个示例仅用作基本指南,您可能需要根据您的实际需求和项目结构进行调整。同样,您还需要考虑性能、安全性和错误处理等方面,以确保为您的用户提供稳定的服务。

使用 Servlet+OkHttp



也可以使用 OkHttp 库在 Java EE 项目中创建一个自定义 Servlet 来实现类似的代理功能。以下是一个简单的示例,展示了如何将 HTTP 服务代理到您的 Java EE 应用中:

  1. 添加依赖

在您的项目的pom.xml文件中,添加javax.servlet-apiokhttp依赖(如果尚未添加):

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>4.0.1</version>
    <scope>provided</scope>
</dependency>
<dependency>
    <groupId>com.squareup.okhttp3</groupId>
    <artifactId>okhttp</artifactId>
    <version>4.9.1</version>
</dependency> 
  1. 创建一个新的 Servlet

在您的 Java EE 项目中,创建一个新的 Servlet 类,用于处理代理请求:

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/proxy/*")
public class ProxyServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String path = req.getPathInfo(); // Get the path after /proxy/
        String targetUrl = "<http://example.com>" + path; // Replace with the actual HTTP service URL
        OkHttpClient httpClient = new OkHttpClient();

        Request httpRequest = new Request.Builder()
                .url(targetUrl)
                // Add any necessary headers here
                .build();

        try (Response httpResponse = httpClient.newCall(httpRequest).execute()) {
            resp.setStatus(httpResponse.code());
            resp.setContentType("text/html"); // Set the appropriate content type, if required
            resp.getWriter().write(httpResponse.body().string());
        }
    }
} 

在这个示例中,我们创建了一个名为ProxyServlet的新 Servlet 类,它使用 OkHttp 将请求代理到目标 HTTP 服务。您可以根据需要修改targetUrl变量以指向实际的 HTTP 服务 URL。

  1. 在 iframe 中嵌入代理服务

现在,您可以在 iframe 中使用您的 Java EE 应用作为代理来访问 HTTP 服务。例如:

<iframe src="<https://your-java-ee-app.com/proxy/some-path>" width="100%" height="100%"></iframe>

请注意,这个示例仅用作基本指南,您可能需要根据您的实际需求和项目结构进行调整。同样,您还需要考虑性能、安全性和错误处理等方面,以确保为您的用户提供稳定的服务。

SpringBoot RestTemplate



在 Java Spring 项目中,可以在应用层使用反向代理功能来将 HTTP 服务转换为 HTTPS 服务。这样,您的 Spring 应用将允许其他 HTTP 服务通过 HTTPS 进行访问,从而使您能够在 iframe 中嵌入这些服务。您可以使用RestTemplateWebClient类来实现这一功能。

下面是一个简单的示例,使用RestTemplate将 HTTP 服务代理到您的 Spring 应用:

  1. 添加依赖

在您的项目的pom.xml文件中,添加spring-web依赖(如果尚未添加):

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency> 
  1. 创建一个新的 Controller

在您的 Spring 项目中,创建一个新的 Controller,用于处理代理请求:

import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class ProxyController {

    private final RestTemplate restTemplate;

    public ProxyController(RestTemplate restTemplate) {
        this.restTemplate = restTemplate;
    }

    @GetMapping("/proxy/{path}")
    public ResponseEntity<String> proxy(@PathVariable String path) {
        String targetUrl = "<http://example.com/>" + path; // Replace with the actual HTTP service URL
        HttpHeaders headers = new HttpHeaders();
        // Add any necessary headers here

        HttpEntity<String> requestEntity = new HttpEntity<>(headers);
        ResponseEntity<String> response = restTemplate.exchange(targetUrl, HttpMethod.GET, requestEntity, String.class);
        return ResponseEntity.status(response.getStatusCode()).body(response.getBody());
    }
} 

在这个示例中,我们创建了一个名为ProxyController的新 Controller,它使用RestTemplate将请求代理到目标 HTTP 服务。您可以根据需要修改targetUrl变量以指向实际的 HTTP 服务 URL。

  1. 在 iframe 中嵌入代理服务

现在,您可以在 iframe 中使用您的 Spring 应用作为代理来访问 HTTP 服务。例如:

<iframe src="<https://your-spring-app.com/proxy/some-path>" width="100%" height="100%"></iframe>

请注意,这个示例仅用作基本指南,您可能需要根据您的实际需求和项目结构进行调整。同样,您还需要考虑性能、安全性和错误处理等方面,以确保为您的用户提供稳定的服务。

Struts2+httpclient



在 Struts2 项目中,您可以通过创建一个自定义的 Action 来实现类似的代理功能。以下是一个简单的示例,展示了如何将 HTTP 服务代理到您的 Struts2 应用中:

  1. 添加依赖

在您的项目的pom.xml文件中,添加struts2-corehttpclient依赖(如果尚未添加):

<dependency>
    <groupId>org.apache.struts</groupId>
    <artifactId>struts2-core</artifactId>
    <version>2.5.26</version>
</dependency>
<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.5.13</version>
</dependency> 
  1. 创建一个新的 Action

在您的 Struts2 项目中,创建一个新的 Action 类,用于处理代理请求:

import com.opensymphony.xwork2.ActionSupport;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.apache.struts2.ServletActionContext;

import javax.servlet.http.HttpServletResponse;

public class ProxyAction extends ActionSupport {

    private String path;

    public String execute() throws Exception {
        String targetUrl = "<http://example.com/>" + path; // Replace with the actual HTTP service URL
        HttpClient httpClient = HttpClients.createDefault();
        HttpGet httpGet = new HttpGet(targetUrl);
        // Add any necessary headers here

        HttpResponse httpResponse = httpClient.execute(httpGet);
        HttpServletResponse servletResponse = ServletActionContext.getResponse();
        servletResponse.setStatus(httpResponse.getStatusLine().getStatusCode());
        servletResponse.setContentType("text/html"); // Set the appropriate content type, if required
        servletResponse.getWriter().write(EntityUtils.toString(httpResponse.getEntity()));

        return NONE;
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }
} 

在这个示例中,我们创建了一个名为ProxyAction的新 Action 类,它使用 Apache HttpClient 将请求代理到目标 HTTP 服务。您可以根据需要修改targetUrl变量以指向实际的 HTTP 服务 URL。

  1. 配置 Struts2

在您的struts.xml配置文件中,添加一个新的 Action 映射:

<struts>
    <!-- Other configurations -->

    <package name="proxy" namespace="/" extends="struts-default">
        <action name="proxy_*" class="your.package.ProxyAction" method="execute">
            <param name="path">{1}</param>
            <result name="success" type="httpheader">
                <param name="status">200</param>
            </result>
        </action>
    </package>
</struts> 

在这个示例中,我们使用了通配符来捕获 URL 路径,并将其传递给ProxyAction

  1. 在 iframe 中嵌入代理服务

现在,您可以在 iframe 中使用您的 Struts2 应用作为代理来访问 HTTP 服务。例如:

<iframe src="<https://your-struts-app.com/proxy_some-path>" width="100%" height="100%"></iframe>

请注意,这个示例仅用作基本指南,您可能需要根据您的实际需求和项目结构进行调整。同样,您还需要考虑性能、安全性和错误处理等方面,以确保为您的用户提供稳定的服务。



项目使用注意

项目中使用的是 pdf 文件, 如下代码有做一些更改

代码在处理 PDF 文件时,使用了resp.getWriter().write(httpResponse.body().string());,这可能导致了 PDF 文件数据损坏。这是因为getWriter()string()方法都是用于处理字符数据的,而 PDF 文件是二进制数据。要解决这个问题,请使用OutputStream代替PrintWriter来发送二进制数据。

请尝试将以下代码:

resp.getWriter().write(httpResponse.body().string());

替换为:

try (InputStream inputStream = httpResponse.body().byteStream();
     OutputStream outputStream = resp.getOutputStream()) {

    byte[] buffer = new byte[4096];
    int bytesRead;
    while ((bytesRead = inputStream.read(buffer)) != -1) {
        outputStream.write(buffer, 0, bytesRead);
    }
} 

删除

resp.setCharacterEncoding("utf-8");

这段代码将从 HTTP 响应中读取二进制数据,并将其写入 Servlet 响应的输出流。这样,您就可以正确处理 PDF 文件,而不会导致数据损坏。