POST跨域解决方案

       最近,公司的WEB应用越来越多,总是要进入不同的网址去打开不同的WEB应用,总是显得麻烦。于是打算做一个公共的登陆页面,根据选择不同的应用登陆到对应的WEB里面。

      听起来似乎很容易,也可以用很多种方式,可以是共享SESSION、利用COOKIE、GET OR POST以及SQL SERVER 等等等。 GET首先因为安全问题不用考虑,因为某些原因我选择了POST 方式来进行LOGIN。我使用双重验证,在公共登陆页面,进行验证一次,如果无误,再加密POST到相应的WEB应用程序,WEB应用接收到数据后,再进行一次比对验证。加密方式可以选择公钥私钥或者MD5,就算POST被截取了,也没很明显的安全隐患。

      利用GOOGLE 搜索得到以下几种方案:

      1. Response.Redirect:这种方法最普遍的使用,但它只能使用GET形式 ,是没有办法做到POST的。

      2.Server.Transfer:这种方式是POST非GET,但是遗憾的是这种方式只能在同一个应用程序里面进行,如果向第三方应用程序发送一个请求,这也将是无法正常工作的。

      3. AJAX POST:ajax本身实际上是通过XMLHttpRequest对象来进行数据的交互,而浏览器出于安全考虑,不允许js代码进行跨域操作。有人说可以利用Access-Control-Allow-Origin解决,反正我是没成功。

      4. HTML5 postMessage:很好,HTML5解决了这个问题,但是不是所有的浏览器都很好的支持了HTML5的。

      5.HttpWebRequest 和 WebClient:前面数据或转换成流或添加到集合到POST,一切都很完美。但是两者一样最后都给你返回一个404 错误,找不到页面。为什么?默认postback总是页面本身。

       以上均未成功。当然还有 document.domain+iframe、动态创建script、利用iframe和location.hash、window.name以及使用FLASH等等等。懒得试了。回到一开始就想到而立马被排除的方法:<form id='formid' method='post' action='页面提交URL' >...</form>,我把ACTION 提交的URL动态变更不就解决了嘛。哎,有时候碰到问题不要急于否认某个解决方案,还是应该冷静的下来把脑袋转个弯也许就是一个很好的解决方案。

    为了调用方便,我用CS写在后台:

 /// <summary>
    /// Summary description for HttpHelper
    /// </summary>
    /// <Author>jack che</Author>
    public static class HttpHelper
    {
        /// <summary>
        /// This method prepares an Html form which holds all data in hidden field in the addetion to form submitting script.
        /// </summary>
        /// <param name="url">The destination Url to which the post and redirection will occur, the Url can be in the same App or ouside the App.</param>
        /// <param name="data">A collection of data that will be posted to the destination Url.</param>
        /// <returns>Returns a string representation of the Posting form.</returns>
        /// <Author>jack che</Author>
        private static String PreparePostForm(string url, NameValueCollection data)
        {
            //Set a name for the form
            const string formId = "PostForm";

            //Build the form using the specified data to be posted.
            var strForm = new StringBuilder();
            strForm.Append("<form id=\"" + formId + "\" name=\"" + formId + "\" action=\"" + url + "\" method=\"POST\">");
            foreach (string key in data)
            {
                strForm.Append("<input type=\"hidden\" name=\"" + key + "\" value=\"" + data[key] + "\">");
            }
            strForm.Append("</form>");

            //Build the JavaScript which will do the Posting operation.
            var strScript = new StringBuilder();
            strScript.Append("<script type='text/javascript'>");
            strScript.Append("var v" + formId + " = document." + formId + ";");
            strScript.Append("v" + formId + ".submit();");
            strScript.Append("</script>");

            //Return the form and the script concatenated. (The order is important, Form then JavaScript)
            return strForm + strScript.ToString();
        }
        /// <summary>
        /// POST data and Redirect to the specified url using the specified page.
        /// </summary>
        /// <param name="page">The page which will be the referrer page.</param>
        /// <param name="destinationUrl">The destination Url to which the post and redirection is occuring.</param>
        /// <param name="data">The data should be posted.</param>
        /// <Author>jack che</Author>
        public static void RedirectAndPost(Page page, string destinationUrl, NameValueCollection data)
        {
            //Prepare the Posting form
            string strForm = PreparePostForm(destinationUrl, data);

            //Add a literal control the specified page holding the Post Form, this is to submit the Posting form with the request.
            page.Controls.Add(new LiteralControl(strForm));
        }
    }
View Code

     调用方法:

using System.Collections.Specialized;

var data = new NameValueCollection {{"uid","test uid"}, {"upwd", "test pwd"}};
  HttpHelper.RedirectAndPost(Page, "http://testing.aspx", data);
View Code

 

无独有偶,看到一个台湾 博客 F6 Team 也写了一个类似的,只不过他把JS写在前台,各有利弊。附地址:http://www.dotblogs.com.tw/puma/archive/2008/09/03/5288.aspx

 

 

 

转载于:https://www.cnblogs.com/chehaoj/archive/2013/06/14/3135332.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Java中,我们可以使用CORS(Cross-Origin Resource Sharing)机制来解决跨域问题。CORS是一种机制,允许Web应用程序从不同的域访问其资源。下面是一种使用Spring框架实现CORS的方式: 1. 创建一个类来配置CORS: ``` @Configuration public class CorsConfig { @Bean public WebMvcConfigurer corsConfigurer() { return new WebMvcConfigurerAdapter() { @Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping("/**") .allowedOrigins("*") .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS") .allowedHeaders("*"); } }; } } ``` 在上述代码中,我们使用了Spring的@Configuration注解来声明一个Java配置类,然后创建了一个名为CorsConfig的类。该类中我们使用了@Bean注解来声明一个名为corsConfigurer的方法,该方法返回一个WebMvcConfigurer对象。在该方法中,我们使用了WebMvcConfigurerAdapter类来扩展Spring MVC的配置,并重写了addCorsMappings()方法来配置CORS。我们使用了registry.addMapping()方法来配置允许跨域访问的URL和HTTP方法,这里我们设置了允许所有URL("**")和所有HTTP方法(GET、POST、PUT、DELETE、OPTIONS),同时也允许所有请求头。 2. 在Controller中使用@CrossOrigin注解: 我们也可以在Controller中直接使用@CrossOrigin注解来配置CORS,例如: ``` @RestController public class MyController { @CrossOrigin(origins = "*", methods = {RequestMethod.GET, RequestMethod.POST, RequestMethod.PUT, RequestMethod.DELETE, RequestMethod.OPTIONS}) @GetMapping("/hello") public String hello() { return "Hello World!"; } } ``` 在上述代码中,我们使用了Spring的@RestController注解来声明一个RESTful Controller,并在其中使用了@GetMapping注解来声明一个GET请求的处理方法。在该方法中,我们使用了@CrossOrigin注解来配置CORS,允许所有域名("*")进行跨域访问。 需要注意的是,为了安全起见,应该尽量限制允许跨域访问的域名和方法,避免出现安全问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值