Java anti-Cross-domain attack solution __java

Source: Internet
Author: User
Train of thought: Determine if the address in the Referer is consistent with the current address, if inconsistent it is a cross-domain attack, otherwise it is not
/** * Verify the legality of the request and prevent Cross-domain attacks * @param request * @return/@SuppressWarnings ("Rawtypes") Publicstatic boolean validaterequ
    EST (httpservletrequest request) {String referer = ""; Booleanreferer_sign = true;//True Station COMMIT, verify outbound commit via//false, verify failure enumeration headervalues = Request.getheaders ("Referer"
    );
    while (Headervalues.hasmoreelements ()) {referer = (String) headervalues.nextelement ();
    //Determine if there is a request page if (Stringutils.isblank (referer)) Referer_sign = false;
        else{//Judge whether the request page and Getrequesturi are the same String servername_str = Request.getservername ();
            if (Stringutils.isnotblank (SERVERNAME_STR)) {intindex = 0;
            if (Stringutils.indexof (Referer, "https://") = = 0) {index = 8;
            } elseif (Stringutils.indexof (referer, "http://") = = 0) {index = 7; } if (Referer.length ()-index < Servername_str.length ()) {//length not enough referer_sign = FALse else{//comparison string (host name) is the same string referer_str = referer.substring (index, index + Servername_st
                R.length ());
            if (!servername_str.equalsignorecase (referer_str)) Referer_sign = false;
    } elsereferer_sign = false;
} returnreferer_sign; }


Original address: http://www.mspring.org/post/241.html




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.