jsp的防注入代碼

//這裏寫在一個名爲sql_inj.java的文件中
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[i])>=0)
        {
            return true;
        }
    }
    return false;
 }

 jsp中調用該函數檢查是否包函非法字符
 <%
 sql_inj antihack=new sql_inj()
  if(request.getParameter("userID") != null)
     userID = request.getParameter("userID").trim();//這裏的也一樣

  if (antihack.sql_inj(userID) || antihack.sql_inj(pwd)){//這裏的userid和pwd你改成自己的變量
   %>
   <Script Language=javascript>alert('參數中包含非法字符!');history.back(-1);</Script>" ;
   <%
  }else{
  //這裏把正常執行的代碼包含進來
 }
%>

發佈了35 篇原創文章 · 獲贊 11 · 訪問量 11萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章