【问题标题】:Restrict creation of Sessions which do not have principal限制创建没有主体的会话
【发布时间】:2019-08-29 07:31:13
【问题描述】:

我正在使用带有 JDBC 的 Spring 会话,它工作正常。但是有许多没有主体的会话被创建。 即使是简单的 curl 请求也会在数据库中创建一个新的会话条目。

我们可以限制没有主体的会话的会话创建吗?

这里的Principal是指登录的用户。

示例数据库数据:

SESSION_ID| CREATION_TIME| LAST_ACCESS_TIME | MAX_INACTIVE_INTERVAL | PRINCIPAL_NAME

| 0112a88c | 1567062697443 | 1567062697443 | 43200 | abc@gmail.com

| 0123f31e | 1567063051563 | 1567063051563 | 43200 | NULL

更新:这些会话是为状态 api 调用创建的。

【问题讨论】:

    标签: spring-boot jdbc spring-session


    【解决方案1】:

    设置 SessionCreationPolicy 不起作用。我通过引入过滤器解决了这个问题。

    @Component
    public class ExcludeSessionRepositoryFilter extends OncePerRequestFilter {
    
      @Override
      protected void doFilterInternal(HttpServletRequest httpRequest, HttpServletResponse httpResponse,
                                      FilterChain filterChain) throws ServletException, IOException {
        httpRequest.setAttribute("org.springframework.session.web.http.SessionRepositoryFilter.FILTERED", Boolean.TRUE);
        filterChain.doFilter(httpRequest, httpResponse);
      }
    }

    我将使用以下代码排除自定义 API 的会话创建。这不会在 SPRING_SESSION 表中创建虚拟会话。

    @Bean
      public FilterRegistrationBean sessionExclusionFilterRegBean(ExcludeSessionRepositoryFilter excludeSessionRepositoryFilter) {
        FilterRegistrationBean registrationBean = new FilterRegistrationBean(excludeSessionRepositoryFilter);
    
        registrationBean.setOrder(Integer.MIN_VALUE);
        registrationBean.addUrlPatterns("/hello", "/status");
    
        return registrationBean;
      }

    【讨论】:

      猜你喜欢
      • 2012-11-01
      • 2018-02-21
      • 1970-01-01
      • 2015-10-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多