smiley-http-proxy-servlet 转发https至 http网页访问

转发 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/

 

posted @ 2022-05-17 11:22  人间春风意  阅读(502)  评论(0编辑  收藏  举报