亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

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
海口市| 从化市| 江城| 绥棱县| 珲春市| 柯坪县| 休宁县| 高平市| 喜德县| 喀喇沁旗| 哈巴河县| 灵川县| 宜兰市| 岑巩县| 榆社县| 织金县| 伊宁县| 涡阳县| 常德市| 杂多县| 建平县| 河曲县| 海林市| 龙岩市| 河南省| 东平县| 防城港市| 宁夏| 甘孜县| 象州县| 修武县| 高要市| 辽源市| 阿克陶县| 南川市| 措勤县| 合山市| 缙云县| 日喀则市| 宁阳县| 遂溪县|