后端如何防范xss攻击
后端防范xss攻击的示例:
java后端避免xss攻击可添加Filter过滤器,例如:
publicclassRequestXssFilterimplementsFilter{
FilterConfigfilterConfig=null;@Override
publicvoidinit(FilterConfigfilterConfig)throwsServletException{
this.filterConfig=filterConfig;
}@Override
publicvoiddoFilter(ServletRequestservletRequest,ServletResponseservletResponse,FilterChainfilterChain)throwsIOException,ServletException{
filterChain.doFilter(newXssHttpServletRequestWrapper(
(HttpServletRequest)servletRequest),servletResponse);
}@Override
publicvoiddestroy(){
this.filterConfig=null;
}
}
再写一个实际过滤类:
publicclassXssHttpServletRequestWrapperextendsHttpServletRequestWrapper{//白名单数组
privatestaticfinalString[]WHITE_LIST={"content"};
//定义script的正则表达式
privatestaticfinalStringREGEX_SCRIPT="<script[^>]*?>[\\s\\S]*?<\\/script>";
//定义style的正则表达式
privatestaticfinalStringREGEX_STYLE="<style[^>]*?>[\\s\\S]*?<\\/style>";
//定义HTML标签的正则表达式
privatestaticfinalStringREGEX_HTML="<[^>]+>";
//定义空格回车换行符
privatestaticfinalStringREGEX_SPACE="\\s*|\t|\r|\n";
//定义所有w标签
privatestaticfinalStringREGEX_W="<w[^>]*?>[\\s\\S]*?<\\/w[^>]*?>";
//定义sql注入
privatestaticStringreg="(\\b(select|update|union|and|or|delete|insert|trancate|char|into|substr|ascii|declare|exec|count|master|into|drop|execute)\\b)";publicXssHttpServletRequestWrapper(HttpServletRequestrequest){
super(request);
}@Override
publicString[]getParameterValues(Stringparameter){
String[]values=super.getParameterValues(parameter);
if(values==null){
returnnull;
}intcount=values.length;String[]encodedValues=newString[count];for(inti=0;i<count;i++){
//白名单放行的只有HTML标签,sql标签或要验证
if(isWhitelist(parameter)){
if(sqlValidate(values[i])){
encodedValues[i]=values[i];
}
encodedValues[i]=null;
}
encodedValues[i]=removeHtml(values[i]);
}returnencodedValues;}@Override
publicStringgetParameter(Stringparameter){
Stringvalue=super.getParameter(parameter);
if(value==null){
returnnull;
}
//白名单放行的只有HTML标签,sql标签或要验证
if(isWhitelist(parameter)){
if(sqlValidate(value)){
returnvalue;
}
returnnull;
}
returnremoveHtml(value);
}@Override
publicStringgetHeader(Stringname){
Stringvalue=super.getHeader(name);
if(value==null){
returnnull;
}if(isWhitelist(name)){
if(sqlValidate(value)){
returnvalue;
}
returnnull;
}
returnremoveHtml(value);
}
//\\b表示限订单词边界比如select不通过1select则是可以的
privatestaticPatternsqlPattern=Pattern.compile(reg,Pattern.CASE_INSENSITIVE);/**
*sql注入过滤器
*@paramstr
*@return
*/
privatestaticbooleansqlValidate(Stringstr){
if(sqlPattern.matcher(str).find()){
System.out.println("未能通过过滤器:str="+str);
returnfalse;
}
returntrue;
}/**
*是否是白名单,白名单的放行
*
*@paramparamName
*@return
*/
privatestaticbooleanisWhitelist(StringparamName){
StringlowerParam=paramName.toLowerCase();
Stringname=Arrays.stream(WHITE_LIST).filter(y->y.toLowerCase().equals(lowerParam)).findAny().orElse(null);
returnname!=null;
}/**
*移除HTML标签
*@paramhtmlStr
*@return
*/
privatestaticStringremoveHtml(StringhtmlStr){
Patternp_w=Pattern.compile(REGEX_W,Pattern.CASE_INSENSITIVE);
Matcherm_w=p_w.matcher(htmlStr);
htmlStr=m_w.replaceAll("");//过滤script标签
Patternp_script=Pattern.compile(REGEX_SCRIPT,Pattern.CASE_INSENSITIVE);
Matcherm_script=p_script.matcher(htmlStr);
htmlStr=m_script.replaceAll("");//过滤script标签
Patternp_style=Pattern.compile(REGEX_STYLE,Pattern.CASE_INSENSITIVE);
Matcherm_style=p_style.matcher(htmlStr);
htmlStr=m_style.replaceAll("");//过滤style标签
Patternp_html=Pattern.compile(REGEX_HTML,Pattern.CASE_INSENSITIVE);
Matcherm_html=p_html.matcher(htmlStr);
htmlStr=m_html.replaceAll("");//过滤html标签
Patternp_space=Pattern.compile(REGEX_SPACE,Pattern.CASE_INSENSITIVE);
Matcherm_space=p_space.matcher(htmlStr);
htmlStr=m_space.replaceAll("");//过滤空格回车标签
htmlStr=htmlStr.replaceAll("","");//过滤
returnhtmlStr.trim();//返回文本字符串
}
}
在Web.xml中添加过滤器,例如:
<filter>
<filter-name>XssEscape</filter-name>
<filter-class>cn.pinming.common.xss.RequestXssFilter</filter-class>
</filter>
本文来源:https://www.yuntue.com/post/62703.html | 云服务器网,转载请注明出处!