中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

springboot如何防sql注入

小新
3096
2021-02-19 12:44:19
欄目: 云計算

springboot如何防sql注入

springboot防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
临猗县| 佛冈县| 余姚市| 叶城县| 巴楚县| 禄丰县| 桂林市| 通许县| 宝山区| 沙河市| 宜宾市| 呼伦贝尔市| 普兰店市| 苏尼特左旗| 手机| 连城县| 南投县| 安义县| 泸定县| 特克斯县| 洛阳市| 赤水市| 碌曲县| 大厂| 定州市| 晋江市| 崇义县| 乐陵市| 湖州市| 穆棱市| 江华| 通山县| 周宁县| 塘沽区| 理塘县| 汶上县| 襄垣县| 康定县| 平定县| 建阳市| 晋宁县|