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

溫馨提示×

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
连南| 房产| 日照市| 关岭| 永年县| 自治县| 长武县| 汉川市| 阿尔山市| 盐津县| 阜新市| 绥阳县| 财经| 新沂市| 炉霍县| 油尖旺区| 金寨县| 宁津县| 临湘市| 从化市| 沙坪坝区| 资源县| 栾川县| 华容县| 广丰县| 许昌县| 富锦市| 太谷县| 咸阳市| 钟祥市| 稷山县| 大足县| 双城市| 贡嘎县| 靖宇县| 龙里县| 翼城县| 昭平县| 沙雅县| 慈溪市| 平顶山市|