在分布式系统中,经常使用redis来做会话保持,在压测接口的过程中,发现每次请求接口,都会把sessionId写入redis,,导致redis内存溢出,因为
SessionRepositoryFilter默认是拦截所有的请求。
查阅相关资料,可以通过下面的方法过滤掉不做会话保持的资源
下面的代码展示的是排除请求路径包含api的资源
@Component
@Order(SessionRepositoryFilter.DEFAULT_ORDER - 1)
public class ExcludeSessionRepositoryFilter extends OncePerRequestFilter {
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
throws ServletException, IOException {
if(StrUtil.contains(request.getRequestURI(), "/api/")) {
request.setAttribute("org.springframework.session.web.http.SessionRepositoryFilter.FILTERED", Boolean.TRUE);
}
filterChain.doFilter(request, response);
}
}