• HttpURLConnection 实现代理。


    public class ForwardProxyController extends AbstractController {
        private String sourceContextPath;
        private String targetContextHostUrl;
    
        @Override
        protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws IOException {
            Logger.info(this, "request : " + request.getPathInfo());
            try {
                HttpURLConnection connection = parse(request);
                response.setContentType(connection.getHeaderField("Content-Type"));
                IOUtils.copy(connection.getInputStream(), response.getOutputStream());
            }catch (Exception e) {
                response.getOutputStream().write("".getBytes());
            }
    
            return null;
        }
    
        private HttpURLConnection parse(HttpServletRequest request) throws IOException {
            Logger.info(this, "request : " + request.getRequestURI());
            String requestString = request.getRequestURI().replace(sourceContextPath, "");
            String queryString = request.getQueryString();
            String uri = targetContextHostUrl + requestString;
            if(StringUtils.isNotBlank(queryString)){
                uri = uri + "?" + queryString;
            }
    
            Logger.info(this, "target uri : " + uri);
            HttpURLConnection connection =  (HttpURLConnection) new URL(uri).openConnection();
            connection.setDoOutput(true);
            connection.setRequestMethod(request.getMethod());
            connection.setRequestProperty("Content-type", request.getContentType());
            connection.setRequestProperty("contentType", "utf-8");
    
            IOUtils.copy(request.getInputStream(), connection.getOutputStream());
    
            return connection;
        }
    }
    <bean id="reverseProxyController" class="com.lufax.operation.gw.ForwardProxyController" >
            <property name="sourceContextPath" value="${OPERATION_GW_CONTENT_PATH}" />
            <property name="targetContextHostUrl" value="${OPERATION_APP_HOST_URL}${OPERATION_APP_CONTENT_PATH}" />
        </bean>
    
        <bean id="forwardProxyController" class="com.lufax.operation.gw.ForwardProxyController" >
            <property name="sourceContextPath" value="${OPERATION_GW_CONTENT_PATH}" />
            <property name="targetContextHostUrl" value="${B_SYSTEM_HOST_URL}" />
        </bean>
    
        <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
            <property name="mappings">
                <value>
                    /bsfront/**=forwardProxyController
                    /service/**=reverseProxyController
                </value>
            </property>
            <property name="order" value="10000" />
        </bean>

    web.xml

        <servlet>
            <servlet-name>dynamic-velocity</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>/WEB-INF/operation-gw-servlet.xml</param-value>
            </init-param>
            <load-on-startup>1</load-on-startup>
        </servlet>
    
    
        <servlet-mapping>
            <servlet-name>dynamic-velocity</servlet-name>
            <url-pattern>/*</url-pattern>
        </servlet-mapping>
  • 相关阅读:
    Python 生成器相关知识
    openpyxl 模块学习记录
    Python 装饰器相关知识
    Python 闭包的相关知识
    Python 内置函数简单介绍
    Git提交的本地仓库在什么位置
    支付宝公钥,私钥加密解密问题
    字符转义
    pyhton 模拟浏览器实现
    大小端模式 大端存储 小端存储
  • 原文地址:https://www.cnblogs.com/zhonghan/p/4932734.html
Copyright © 2020-2023  润新知