1

To avoid re-developing the wheel. Are there any example Java EE servlet filters that take care of some basic security checks/ i.e.

  • Block web requests for a time period if a rootkit hits the server, ie with a url that ends in .exe or contains "../../.."
  • Throttle or block IP's that are making unexpectedly high number of requests.

I also wonder if something equivalent to a Thread.sleep(1000); in the servlet filter for those particular types of requests wouldn't be such a bad thing.

Arjan Tijms
  • 37,782
  • 12
  • 108
  • 140
Jay
  • 19,649
  • 38
  • 121
  • 184

1 Answers1

4

Maybe this will help.

public class SuspiciousURLFilter implements Filter {

        @Override
        public void destroy() {
        }

        @Override
        public void doFilter(ServletRequest request, ServletResponse response,
                FilterChain filterChain) throws IOException, ServletException {

            HttpServletRequest httpRequest = (HttpServletRequest) request;
            String requestURI = httpRequest.getRequestURI();

            if (requestURI.endsWith(".exe")) {

                HttpServletResponse httpResponse = (HttpServletResponse) response;
                            //send error or maybe redirect to some error page
                            httpResponse.sendError(HttpServletResponse.SC_BAD_REQUEST);
            }

            filterChain.doFilter(request, response);
        }

        @Override
        public void init(FilterConfig config) throws ServletException {
        }
    }

In your web.xml:

    <filter>
        <filter-name>suspiciousURLFilter </filter-name>
        <filter-class>your.package.SuspiciousURLFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>SuspiciousURLFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
sermolaev
  • 975
  • 11
  • 23
  • Yep, this is the sort of thing you would do if you were going to write something yourself. However this doesn't handle temporarily banning/blocking the offending IP address. – Jay Aug 23 '13 at 06:15