---
title: "jsp参数过滤防注入的解决方法"
author: "ydsakyclguozi"
date: "2010-02-03T19:56:11Z"
tags: []
categories: ["Web前端"]
slug: "jsp"
draft: false
lastmod: "2010-02-03T19:56:11Z"
id: 587013
url: "https://52347.iteye.com/blog/587013"
---

转自http://voff12.iteye.com/blog
关键字: 反黑 
首先注入的类型分为参数数字型注入，参数字浮型注入，搜索框注入三种。 
防注入的方法 
1）参数过滤。 
2）sql语句添加参数用占位符。
public static boolean sql_inj(String str)    
{   
String inj_str = "'|and|exec|insert|select|delete|update|count|*|%|chr|mid|master|truncate|char|declare|;|or|-|+|,";   
String inj_stra = split(inj_str,"|");   
for (int i=0 ; i < inj_stra.length ; i++ )   
{   
if (str.indexOf(inj_stra)>=0)   
{   
return true;   
}   
}   
return false;   
}   
  
jsp中调用该函数检查是否包函非法字符   
<%   
if(request.getParameter("userID") != null)   
userID = request.getParameter("userID").trim();   
  
if (StringUtil.sql_inj(userID) || StringUtil.sql_inj(pwd)){   
%>   
<Script Language=javascript>alert('参数中包含非法字符！');history.back(-1);</Script>" ;   
<%   
}else{   
……   
}%>