zoukankan      html  css  js  c++  java
  • 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>
  • 相关阅读:
    查询安装webpack4.0是否成功时提示无法找到的解决方法
    clientHeight—scrollHeight—offsetHeight三者的区别
    onunload事件不触发的探索
    你知道刷社保卡看病买药也能扫二维码支付吗?(以成都市社保卡为例)
    成都市新社保卡线上申请方法(无需线下办理,手机直接搞定)
    jquery实现移动端页面加载后,向上滚动指定距离无效引起的探索
    社保官网查询密码重置及注册(以成都为例)
    社保对线上购买保险时2个你不知道的影响(最后一个真的太坑爹,中招的人肯定不在少数)
    坑爹的京东E卡
    Mongodb(一)
  • 原文地址:https://www.cnblogs.com/zhonghan/p/4932734.html
Copyright © 2011-2022 走看看