本篇内容介绍了“java过滤特殊字符的操作方法”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
XSS ,全名:cross-site scripting(跨站点脚本),是当前 web 应用中最危险和最普遍的漏洞之一。攻击者尝试注入恶意脚本代码(常js脚本)到受信任的网站上执行恶意操作,用户使用浏览器浏览含有恶意脚本页面时,会执行该段恶意脚本,进而影响用户(比如关不完的网站、盗取用户的 cookie 信息从而伪装成用户去操作)等等。
它与 SQL 注入很类似,同样是通过注入恶意指令来进行攻击。但 SQL 注入是在服务器端上执行的,而 XSS 攻击是在客户端上执行的,这点是他们本质区别。
其实,个人感觉对于xss攻击不必区分究竟是反射型XSS、存储型XSS还是DOM Based XSS,只需要知道如何去防护。而防护的最有效的措施就是过滤,对前端页面提交到后台的内容进行过滤。具体如下:
1.解决方法一
拦截所有的请求参数,对请求参数中包含特殊字符'<‘或'>'进行过滤。
package com.haier.openplatform.srm.base.filter;import java.io.IOException;import java.util.Iterator;import java.util.Map;import java.util.Set;import javax.servlet.FilterChain;import javax.servlet.ServletException;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletRequestWrapper;import javax.servlet.http.HttpServletResponse;import org.springframework.web.filter.OncePerRequestFilter;public class StringFilter extends OncePerRequestFilter{@Overrideprotected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain)throws ServletException, IOException {chain.doFilter(new StringFilterRequest((HttpServletRequest)request), response);}}class StringFilterRequest extends HttpServletRequestWrapper {public StringFilterRequest(HttpServletRequest request) {super(request);}@Overridepublic String getParameter(String name) {// 返回值之前 先进行过滤return filterDangerString(super.getParameter(name));}@Overridepublic String[] getParameterValues(String name) {// 返回值之前 先进行过滤String[] values = super.getParameterValues(name);if(values==null){return null;}for (int i = 0; i < values.length; i++) {values[i] = filterDangerString(values[i]);}return values;}@Overridepublic Map getParameterMap() {Map keys = super.getParameterMap();Set set = keys.entrySet();Iterator iters = set.iterator();while (iters.hasNext()) {Object key = iters.next();Object value = keys.get(key);keys.put(key, filterDangerString((String[]) value));}return keys;}public String filterDangerString(String value) {if (value == null) {return null;}// value = value.replaceAll("\\{", "{");value = value.replaceAll("<", "<");value = value.replaceAll(">", ">");// value = value.replaceAll("\t", " ");// value = value.replaceAll("\r\n", "\n");// value = value.replaceAll("\n", "<br/>");// value = value.replaceAll("'", "'");// value = value.replaceAll("\\\\", "\");// value = value.replaceAll("\"", """);// value = value.replaceAll("\\}", "﹜").trim();return value;}public String[] filterDangerString(String[] value) {if (value == null) {return null;}for (int i = 0; i < value.length; i++) {String val = filterDangerString(value[i]);value[i] = val;}return value;}}
web.xm中的过滤器配置:
<filter> <filter-name>StringFilter</filter-name> <filter-class>com.xxx.base.filter.StringFilter</filter-class> </filter> <filter-mapping> <filter-name>StringFilter</filter-name> <url-pattern>public class HTMLUtils {public static String filterHTMLTag(String htmlStr) { //定义HTML标签的正则表达式 String reg_html="<[^>]+>"; Pattern pattern=Pattern.compile(reg_html,Pattern.CASE_INSENSITIVE); Matcher matcher=pattern.matcher(htmlStr); htmlStr=matcher.replaceAll(""); //过滤html标签 return htmlStr;}public static String filterTagByName(String htmlStr,String tagName) { String reg_html="<"+tagName+"[^>]*?>[\\s\\S]*?<\\/"+tagName+">"; Pattern pattern=Pattern.compile(reg_html,Pattern.CASE_INSENSITIVE); Matcher matcher=pattern.matcher(htmlStr); htmlStr=matcher.replaceAll(""); //过滤html标签 return htmlStr;}public static String filterHTMLTagInStyle(String htmlStr) { String reg_html="style=('|\")(.*?)('|\")"; Pattern pattern=Pattern.compile(reg_html,Pattern.CASE_INSENSITIVE); Matcher matcher=pattern.matcher(htmlStr); htmlStr=matcher.replaceAll(""); //过滤html标签 return htmlStr;}public static String replayFace(String htmlStr) { String reg_html="\\[em_\\d{1,}\\]"; Pattern pattern =Pattern.compile(reg_html,Pattern.CASE_INSENSITIVE); Matcher matcher=pattern.matcher(htmlStr); if(matcher.find()) { matcher.reset(); while(matcher.find()) { String num = matcher.group(0); String number=num.substring(num.lastIndexOf('_')+1, num.length()-1); htmlStr = htmlStr.replace(num, "<img src='/face/arclist/"+number+".gif' border='0' />"); } } return htmlStr;} public static void main(String[] args) { String html = "<script>alert('test');</script><img src='/face/arclist/5.gif' border='0' /><div style='position:fixs;s'></div><style>body{color:#fff;}</style><Style>body{color:#fff;}</Style><STYLE>body{color:#fff;}</STYLE>"; System.out.println("html="+html); html = HTMLUtils.filterTagByName(html, "style"); System.out.println("html="+html); html = HTMLUtils.filterTagByName(html, "script"); System.out.println("html="+html); html = HTMLUtils.filterHTMLTagInStyle(html); System.out.println("html="+html); }}
java 过滤特殊字符串升级版
ASCII码中除了32之外还有160这个特殊的空格 db中的空格 不间断空格->页面上的 所产生的空格;
public static String stringFilter (String str){ String regEx="[\\u00A0\\s\"`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~!@#¥%……&*()——+|{}【】‘;:”“'。,、?]"; Pattern p = Pattern.compile(regEx); Matcher m = p.matcher(str); return m.replaceAll("").trim(); }
“java过滤特殊字符的操作方法”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注编程网网站,小编将为大家输出更多高质量的实用文章!