java后台對前端輸入的特殊字符進行轉義


HTML:

常見的幫助類有2個:一個是spring的HtmlUtils,另外一個是apache.commons下的StringEscapeUtils

 1 public static void testHtml(){
 2     String str = "<a href='http://www.qq.com'>QQ</a><script>";
 3     /**
 4      *  Spring的HtmlUtils進行轉義  5      */
 6     //&lt;a href=&#39;http://www.qq.com&#39;&gt;QQ&lt;/a&gt;&lt;script&gt;
 7     System.out.println(org.springframework.web.util.HtmlUtils.htmlEscape(str));
 8     //&#60;a href=&#39;http://www.qq.com&#39;&#62;QQ&#60;/a&#62;&#60;script&#62;
 9     System.out.println(org.springframework.web.util.HtmlUtils.htmlEscapeDecimal(str));
10     //&#x3c;a href=&#x27;http://www.qq.com&#x27;&#x3e;QQ&#x3c;/a&#x3e;&#x3c;script&#x3e;
11     System.out.println(org.springframework.web.util.HtmlUtils.htmlEscapeHex(str));
12     
13     /**
14      *  Spring的HtmlUtils進行還原 15      */
16     //<a href='http://www.qq.com'>QQ</a><script>
17     System.out.println(org.springframework.web.util.HtmlUtils.htmlUnescape("&lt;a href=&#39;http://www.qq.com&#39;&gt;QQ&lt;/a&gt;&lt;script&gt;"));
18     //<a href='http://www.qq.com'>QQ</a><script>
19     System.out.println(org.springframework.web.util.HtmlUtils.htmlUnescape("&#60;a href=&#39;http://www.qq.com&#39;&#62;QQ&#60;/a&#62;&#60;script&#62;"));
20     //<a href='http://www.qq.com'>QQ</a><script>
21     System.out.println(org.springframework.web.util.HtmlUtils.htmlUnescape("&#x3c;a href=&#x27;http://www.qq.com&#x27;&#x3e;QQ&#x3c;/a&#x3e;&#x3c;script&#x3e;"));
22     
23     /**
24      *  apache的StringEscapeUtils進行轉義 25      */
26     //&lt;a href='http://www.qq.com'&gt;QQ&lt;/a&gt;&lt;script&gt;
27     System.out.println(org.apache.commons.lang.StringEscapeUtils.escapeHtml(str));
28     
29     /**
30      *  apache的StringEscapeUtils進行還原 31      */
32     //<a href='http://www.qq.com'>QQ</a><script>
33     System.out.println(org.apache.commons.lang.StringEscapeUtils.unescapeHtml("&lt;a href='http://www.qq.com'&gt;QQ&lt;/a&gt;&lt;script&gt;"));
34 }

JavaScript:

常見的幫助類有2個:一個是spring的JavaScriptUtils,另外一個是apache.commons下的StringEscapeUtils

 1 public static void testJavascript(){
 2     String js = "<script type='text/javascript'>var a=10;alert(a);</script>";
 3     /**
 4      *  Spring的JavaScriptUtils進行轉義, 未提供還原的方法  5      */
 6     //\u003Cscript type=\'text\/javascript\'\u003Evar a=10;alert(a);\u003C\/script\u003E
 7     System.out.println(org.springframework.web.util.JavaScriptUtils.javaScriptEscape(js));
 8     
 9     /**
10      *  apache的StringEscapeUtils進行轉義 11      */
12     //<script type=\'text\/javascript\'>var a=10;alert(a);<\/script>
13     System.out.println(org.apache.commons.lang.StringEscapeUtils.escapeJavaScript(js));
14     /**
15      *  apache的StringEscapeUtils進行還原 16      */
17     //<script type='text/javascript'>var a=10;alert(a);</script>
18     System.out.println(org.apache.commons.lang.StringEscapeUtils.unescapeJavaScript(org.apache.commons.lang.StringEscapeUtils.escapeJavaScript(js)));
19 }

SQL:

apache.commons下的StringEscapeUtils

/**
 *  apache的StringEscapeUtils進行轉義
 */
String sql = "select * from table where username='" + org.apache.commons.lang.StringEscapeUtils.escapeSql("admin' or '1=1") + "' and password='admin'";
//select * from table where username='admin'' or ''1=1' and password='admin'
System.out.println(sql);

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM