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

溫馨提示×

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
长海县| 高平市| 长丰县| 黄大仙区| 泽库县| 屯门区| 清流县| 集贤县| 巴林左旗| 平塘县| 深泽县| 广元市| 平泉县| 和田市| 鲁山县| 武隆县| 威海市| 博湖县| 安远县| 山西省| 海伦市| 舒城县| 阜新| 无棣县| 吉水县| 克东县| 江门市| 大连市| 商丘市| 博湖县| 遂川县| 靖宇县| 高邮市| 大连市| 原阳县| 绥中县| 朝阳县| 吉林市| 岳普湖县| 武清区| 洛隆县|