欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

跨域:一种通过服务端解决跨域的实现 跨域 

程序员文章站 2022-07-06 11:35:00
...
有一种跨域的业务场景:
A域名页面,嵌套B域名页面,B域名页面又想访问A域名的数据,这时候就有跨域了,其中B域名页面请求的时候不会携带A域名要求的登录cookie信息

解决办法:B页面操作请求B域名,携带着B的cookie,然后有B的服务端来请求A获取数据



@RequestMapping("/proxy")
    @ResponseBody
    public String forwardSaveEOrder(HttpServletRequest request) {
        HttpClient qunarHttpClient = new DefaultHttpClient();
        qunarHttpClient.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY);

        HttpPost qunarHttpPost = new HttpPost(saveEOrderUrlForForwarding);
        
        qunarHttpPost.setHeader("Cookie", request.getHeader("Cookie"));
        List <NameValuePair> nvps = new ArrayList <NameValuePair>();   
        Enumeration<String> parameterNames = request.getParameterNames();
        while (parameterNames.hasMoreElements()) {
            String parameterName = parameterNames.nextElement();
            nvps.add(new BasicNameValuePair(parameterName, request.getParameter(parameterName)));  
        }
        try {
			qunarHttpPost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
		} catch (Exception e) {
			logger.error("",e);
		}
        
        try {
        	
            String result = EntityUtils.toString(qunarHttpClient.execute(qunarHttpPost).getEntity());
            return result;
        } catch (Exception e) {
            logger.error("error : while executing post, url : {}", saveEOrderUrlForForwarding,e);
            QMonitor.recordOne(forwardSaveEOrderExceptionMonitorKey);
            return "生单异常";
        } 

    }
相关标签: 跨域