使用过滤器/拦截器捕获相关的用户请求

时间:2018-10-23 07:12:47

标签: java spring spring-boot web-applications

我正在捕获为我的网站请求的URL。我需要查看从我的网站请求的所有页面。

为此,我创建了一个基本过滤器,并从那里开始记录页面请求。

现在,此过滤器将捕获特定于页面的所有请求。

  

例如 abc.com/page1 abc.com/resources/myjs.js

我的问题是,对于每个页面请求,也请求后续资源(js,css)。我只想捕获相关请求。

现在,我检查诸如/resources之类的模式以忽略此类请求,但我正在寻找一种更干净的方法。

此外,拦截器在这里会更有用吗? 我也看到了过滤器模式。但这没有用,因为我必须为过滤器创建模式。

1 个答案:

答案 0 :(得分:0)

如果您想捕获从您的网站访问的URL,则可以将spring boot配置为以以下方式生成访问日志,直到您不需要更多高级信息为止:

server.tomcat.accesslog.directory=/logs/  #absolute directory path for log files\
server.tomcat.accesslog.enabled=false #Enable access log.  
server.tomcat.accesslog.pattern=any_pattern # log string format pattern for access logs.

要基于任何请求模式执行任何操作,可以继续使用过滤器。 我通过以下方式针对此类要求使用过滤器:

    public class CustomFilter extends OncePerRequestFilter{

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, 
                                    FilterChain filterChain) throws ServletException, IOException {
        String url = request.getRequestURI();
        if (url.startsWith("/resources/")) {
            //your business logic
        }
        filterChain.doFilter(request,response);
    }
}
相关问题