91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

spring如何防止sql注入

小新
1156
2021-01-01 13:58:49
欄目: 云計算

spring如何防止sql注入

spring防止sql注入的方法:

可以通過編寫一個webFilter過濾器來防止sql注入,例如:

/**

* sql注入過濾器

*/

@Component

@WebFilter(urlPatterns = "/*", filterName = "SQLInjection", initParams = { @WebInitParam(name = "regx", value = "(?:')|(?:--)|(/\\*(?:.|[\\n\\r])*?\\*/)|" +

"(\\b(select|update|and|or|delete|insert|trancate|char|into|substr|ascii|declare|exec|count|master|into|drop|execute)\\b)") })

public class SqlInjectFilter implements Filter{

  private String regx;

  @Override

  public void init(FilterConfig filterConfig) throws ServletException {

    this.regx = filterConfig.getInitParameter("regx");

  }

  @Override

  public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {

    HttpServletRequest req = (HttpServletRequest) servletRequest;

    Map parametersMap = servletRequest.getParameterMap();

    Iterator it = parametersMap.entrySet().iterator();

    while (it.hasNext()) {

      Map.Entry entry = (Map.Entry) it.next();

      String[] value = (String[]) entry.getValue();

      for (int i = 0; i < value.length; i++) {

        if (null != value[i] && value[i].matches(this.regx)) {

          log.error("您輸入的參數有非法字符,請輸入正確的參數!");

          servletRequest.setAttribute("err", "您輸入的參數有非法字符,請輸入正確的參數!");

          servletRequest.setAttribute("pageUrl",req.getRequestURI());

          servletRequest.getRequestDispatcher(servletRequest.getServletContext().getContextPath() + "/error").forward(servletRequest, servletResponse);

          return;

        }

      }

    }

  }

  filterChain.doFilter(servletRequest, servletResponse);

 }

  @Override

  public void destroy() {

  }

}

在啟動類上添加注解:

@ServletComponentScan(basePackages ="xxxx.xxx.xxx.filter") //filter所在的包,掃描

0
连江县| 襄樊市| 鹿泉市| 沙河市| 新巴尔虎左旗| 边坝县| 信宜市| 肥城市| 七台河市| 马边| 洱源县| 沙洋县| 高邑县| 六安市| 周至县| 富锦市| 冷水江市| 伊宁县| 义乌市| 新昌县| 仪陇县| 股票| 和林格尔县| 什邡市| 东兰县| 蓬溪县| 色达县| 彭泽县| 定襄县| 民丰县| 绵竹市| 德化县| 孟连| 沈丘县| 高州市| 交口县| 民乐县| 惠州市| 北辰区| 定日县| 宣化县|