solr SecurityUtil

本文介绍了一种处理特殊字符以防止XSS攻击的方法,并提供了处理HTTP响应头中特殊字符以避免cache-poisoning、cross-sitescripting等攻击的实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package com.paic.wcm.search.utils;


/**
 * 
 * @author yangzhenyan
 * @version
 */


public class SecurityUtil {


/**
* 处理特殊字符(Cross-site scripting (XSS))
*  // Filter the HTTP response using SecurityUtil.outputfilter 
*  PrintWriter out = response.getWriter(); 
*  // set output response
* out.write(SecurityUtil.outputfilter(response)); 
* out.close();
*/
public static String outputfilter(String value) {
if (value == null) {
return null;
}
StringBuffer result = new StringBuffer(value.length());
for (int i=0; i<value.length(); ++i) {
switch (value.charAt(i)) {
case '<':
result.append("<");
break;
case '>':
result.append(">");
break;
case '"':
result.append(""");
break;
case '\'':
result.append("'");
break;
case '%':
result.append("%");
break;
case ';':
result.append(";");
break;
case '(':
result.append("(");
break;
case ')':
result.append(")");
break;
case '&':
result.append("&");
break;
case '+':
result.append("+");
break;
default:
result.append(value.charAt(i));
break;
}
}
return result.toString();
}





   /**
* 处理特殊字符(HTTP response splitting)
* 在HTTP响应头文件中包含未经过校验的数据会导致cache-poisoning,cross-site scripting,cross-user
* defacement或者page hijacking攻击。
* 应用程序应该屏蔽任何肯定要出现在HTTP响应头中、含有特殊字符的输入,特别是CR(回车符,也可由%0d或\r提供)和LF(换行符,也可由%0a或\n提供)字符,将它们当作非法字符。
* 
* @param str
* @return
*/
 public static String fixHttpRS(String str){
if (str==null){
    return null;
  }
  String str_temp = str;
  
  while(true){
  if ((str_temp.indexOf("CR")==-1)&&(str_temp.indexOf("%0d")==-1)&&(str_temp.indexOf("\r")==-1)
  &&(str_temp.indexOf("LF")==-1)&&(str_temp.indexOf("%0a")==-1)&&(str_temp.indexOf("\n")==-1)){
  break;
  }
  if (str_temp.indexOf("CR")!=-1){
  str_temp = str_temp.replaceAll("CR", "");
  }
  if (str_temp.indexOf("%0d")!=-1){
  str_temp = str_temp.replaceAll("%0d", "");
}
  if (str_temp.indexOf("\r")!=-1){
  str_temp = str_temp.replaceAll("\r", "");
}
if (str_temp.indexOf("LF")!=-1){
str_temp = str_temp.replaceAll("LF", "");
}
if (str_temp.indexOf("%0a")!=-1){
str_temp = str_temp.replaceAll("%0a", "");
}
if (str_temp.indexOf("\n")!=-1){
str_temp = str_temp.replaceAll("\n", "");
}
  }


   return str_temp.toString();
 }
  
  public static void main( String[] args) {
    String str= "aa \r";
    System.out.println( SecurityUtil.fixHttpRS( str));
  }


}







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值