1. 程式人生 > 其它 >smiley-http-proxy-servlet 轉發https至 http網頁訪問

smiley-http-proxy-servlet 轉發https至 http網頁訪問

轉發網頁訪問

正常的smiley-http-proxy-servlet 寫法,

可以轉發https介面到http,http介面到http,http訪問轉發至http網頁,

但是沒有辦法轉發https到http的訪問,會提示當前網頁已更改地址之類的,總之就是網頁無響應,

1. 原始碼直接copy到專案裡,調整原始碼

  結果程式碼copy進來了,但是並沒有被代理,搜尋了一大圈,沒結果就不搜了

2. 擴大了搜尋範圍

  在網上搜索了很久都沒有找到相關 https 網頁訪問轉發至http網頁訪問的,所以就擴大了搜尋範圍

  搜尋了一篇,可以動態代理目標地址的博文,關係不大,我這邊也就只有一個網址,隨便試試,簡單調一下,居然ok了?!

原博文:https://www.cnblogs.com/changxy-codest/p/13093132.html

調整後更新如下:

import org.apache.http.client.utils.URIUtils;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URI;

public class
ProxyServlet extends org.mitre.dsmiley.httpproxy.ProxyServlet { private static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(ProxyServlet.class); @Override protected void service(HttpServletRequest servletRequest, HttpServletResponse servletResponse) throws ServletException, IOException { servletRequest.setAttribute(ATTR_TARGET_URI,
null); URI uri = null; try { uri = new URI(targetUri); } catch (java.net.URISyntaxException e) { logger.error("建立URI物件出錯, targetUri[{}]", targetUri, e); } servletRequest.setAttribute(ATTR_TARGET_HOST, null); super.targetHost = URIUtils.extractHost(uri); super.service(servletRequest, servletResponse); } }

下面都是預設的一般配置:

//import org.mitre.dsmiley.httpproxy.ProxyServlet;
import xxxx.xxx.xx.ProxyServlet;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ProxyServletConfiguration {

    @Value("${proxy.forward.servlet_url_one}")
    private String proxyUrl;

    @Value("${proxy.forward.target_url_one}")
    private String targetUrl;

    @Bean
    public org.springframework.boot.web.servlet.ServletRegistrationBean servletRegistrationBean(){
        org.springframework.boot.web.servlet.ServletRegistrationBean servletRegistrationBean = new  org.springframework.boot.web.servlet.ServletRegistrationBean(new ProxyServlet(), proxyUrl);
        //這個setName必須要設定,並且多個的時候,名字需要不一樣
        servletRegistrationBean.setName("forward1");
        servletRegistrationBean.addInitParameter("targetUri", targetUrl);
        servletRegistrationBean.addInitParameter(ProxyServlet.P_LOG, "true");
        return servletRegistrationBean;
    }

//        @Bean
//    public org.springframework.boot.web.servlet.FilterRegistrationBean registration(org.springframework.web.filter.HiddenHttpMethodFilter filter) {
//        FilterRegistrationBean registration = new FilterRegistrationBean(filter);
//        registration.setEnabled(false);
//        return registration;
//    }
}
 <!--ProxyFilter的引入依賴-->
        <dependency>
            <groupId>org.mitre.dsmiley.httpproxy</groupId>
            <artifactId>smiley-http-proxy-servlet</artifactId>
            <version>1.12.1</version>
        </dependency>
# 埠號及ssl配置
server:
  port: 10000
  ssl:
    key-store: classpath:xxx
    key-store-password: xxx
    key-password: xxx
    key-store-type: xxx
    enabled: true
    key-alias: xxxx
proxy:
  forward:
    servlet_url_one: /*
    target_url_one: http://tarhetUrlIP:port/