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