Java protection against XSS attacks

Source: Internet
Author: User

Method One:

1. Add Xssfilter

@Configuration Public classXssfilterImplementsFilter {@Override Public voidInit (Filterconfig filterconfig)throwsservletexception {} @Override Public voidDoFilter (ServletRequest servletrequest, Servletresponse servletresponse, Filterchain filterchain)throwsIOException, servletexception {httpservletrequest request=(httpservletrequest) ServletRequest; Filterchain.dofilter (NewXsshttpservletrequestwrapper (Request), servletresponse); } @Override Public voiddestroy () {}}
Xssfilter

2. Add Xsshttpservletrequestwrapper.java Class

 Public classXsshttpservletrequestwrapperextendsHttpservletrequestwrapper { PublicXsshttpservletrequestwrapper (HttpServletRequest request) {Super(Request); } @Override Publicstring GetHeader (string name) {returnSTRINGESCAPEUTILS.ESCAPEHTML4 (Super. GetHeader (name)); } @Override PublicString getquerystring () {returnSTRINGESCAPEUTILS.ESCAPEHTML4 (Super. getquerystring ()); } @Override Publicstring GetParameter (string name) {returnSTRINGESCAPEUTILS.ESCAPEHTML4 (Super. GetParameter (name)); } @Override Publicstring[] Getparametervalues (String name) {string[] values=Super. Getparametervalues (name); if(Values! =NULL) {              intLength =values.length; String[] Escapsevalues=NewString[length];  for(inti = 0; i < length; i++) {Escapsevalues[i]=stringescapeutils.escapehtml4 (Values[i]); }              returnescapsevalues; }          return Super. Getparametervalues (name); }        }  
Xsshttpservletrequestwrapper

From this, it can be achieved,

If you enter <script>alert ("OK") in the text box of the website; </script>

The data saved after submission to the database is: &amp;lt;script&amp;gt;alert (&amp;quot;OK&amp;quot;);&amp;lt;/script&amp; Gt

Method Two:

1. Add Xssfilter, (IBID.)

2: Add Xsshttpservletrequestwrapper.java class

 Public classXsshttpservletrequestwrapperextendsHttpservletrequestwrapper {/*** Constructs a Request object wrapping the given request. *     * @paramrequest the request to wrap *@throwsIllegalArgumentException If the request is null*/     PublicXsshttpservletrequestwrapper (HttpServletRequest request) {Super(Request); } @Override Publicstring GetHeader (string name) {String Value=Super. GetHeader (name); if(Stringutils.isempty (value)) {returnvalue; }        Else{            returnCLEANXSS (value); }} @Override Publicstring GetParameter (string name) {String Value=Super. GetParameter (name); if(Stringutils.isempty (value)) {returnvalue; }        Else{            returnCLEANXSS (value); }} @Override Publicstring[] Getparametervalues (String name) {string[] values=Super. Getparametervalues (name); if(Values! =NULL) {            intLength =values.length; String[] Escapsevalues=NewString[length];  for(inti = 0; i < length; i++) {Escapsevalues[i]=CLEANXSS (Values[i]); }            returnescapsevalues; }        return Super. Getparametervalues (name); } @Override PublicServletInputStream getInputStream ()throwsIOException {String str=getrequestbody (Super. getInputStream ()); STR=CLEANXSS (str); FinalBytearrayinputstream Bais =NewBytearrayinputstream (Str.getbytes ()); return NewServletInputStream () {@Override Public intRead ()throwsIOException {returnBais.read (); } @Override Public Booleanisfinished () {return false; } @Override Public BooleanIsReady () {return false; } @Override Public voidSetreadlistener (Readlistener listener) {}}; }    Privatestring getrequestbody (InputStream stream) {string line= ""; StringBuilder Body=NewStringBuilder (); intCounter = 0; //read the data content of the post submissionBufferedReader reader =NewBufferedReader (NewInputStreamReader (Stream, Charset.forname ("UTF-8"))); Try {             while(line = Reader.readline ())! =NULL) {body.append (line); Counter++; }        } Catch(IOException e) {e.printstacktrace (); }        returnbody.tostring (); }    Privatestring Cleanxss (String value) {if(Stringutils.isempty (value)) {returnvalue; }        Else{Value= Value.replaceall ("<", "" "); Value= Value.replaceall (">", "" "); String tmp= "^script$"; Value= Value.replaceall (tmp, ""); returnvalue; }    }}
Xsshttpservletrequestwrapper

Two methods, the same principle is not the same as the wording,

The second way to save to the database is: Scriptalert ("OK");/script

Java prevents XSS attacks

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.