• ADADADADAD

    springboot如何防sql注入[ 网络知识 ]

    网络知识 时间:2024-11-20 12:07:42 热度:5℃

    作者:文/会员上传 下载docx

    简介:

    springboot防sql注入的方法:通过webFilter防止sql注入,例如:/*** sql注入过滤器*/@Component@WebFilter(urlPatterns = "/*", filterName = "SQLInjection", initParams = { @W

    以下为本文的正文内容,请查阅,本站为公益性网站,复制本文以及下载DOC文档全部免费。

    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所在的包,扫描

    springboot如何防sql注入.docx

    将本文的Word文档下载到电脑

    推荐度:

    下载
    热门标签: sql注入springboot
    ADADAD
    热门栏目