ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

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

2022-05-17 11:34:01  阅读:254  来源: 互联网

标签:http ProxyServlet smiley springframework proxy import org servlet


转发网页访问

正常的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/

 

标签:http,ProxyServlet,smiley,springframework,proxy,import,org,servlet
来源: https://www.cnblogs.com/uoky/p/16280125.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有