【问题标题】:Spring boot security: Requested url creates unwanted redis sessionSpring Boot 安全性:请求的 url 创建了不需要的 redis 会话
【发布时间】:2019-07-18 15:10:27
【问题描述】:

假设登录网址是“/login”。 有两个受保护的资源:

  • “/保护”
  • “/”是一个 302 重定向到“/protected”

当未经身份验证的用户尝试访问“/protected”时,他会被重定向到“/login”。在后台创建了一个会话,其中存储了 SPRING_SECURITY_SAVED_REQUEST,以便在成功登录后将用户重定向到“/protected”网址。

这是 Spring Security 的默认行为。

我的问题: 即使用户调用“/”,也会创建会话。因此,所有在没有有效登录信息的情况下调用域的机器人和渗透测试都会在底层 redis 层中创建会话。

当没有存储重定向请求或至少将它们限制为有效后端端点的定义列表时,如何防止创建这些会话?

我的安全配置:

protected void configure(HttpSecurity http) throws Exception {
    http
            .authorizeRequests()
            .antMatchers("/password/forgot/**").permitAll()
            .antMatchers("/password/reset/**").permitAll()
            .antMatchers("/css/**").permitAll()
            .antMatchers("/js/**").permitAll()
            .antMatchers("/img/**").permitAll()
            .antMatchers( "/favicon.ico").permitAll()
            .antMatchers("/login").permitAll()
            .anyRequest().fullyAuthenticated();

    http
            .formLogin()
            .loginPage("/login")
            .permitAll()
            .successHandler(authSuccessHandler)
            .and()
            .logout().logoutRequestMatcher(new AntPathRequestMatcher("/logout")).logoutSuccessUrl("/login")
            .deleteCookies("SESSION")
            .clearAuthentication(true)
            .invalidateHttpSession(true)
            .permitAll();

    http.sessionManagement()
            .maximumSessions(1)
            .and()
            .sessionCreationPolicy(SessionCreationPolicy.NEVER);

    http.headers().frameOptions().disable();
    http.csrf().disable();
}

【问题讨论】:

    标签: spring spring-boot session spring-security


    【解决方案1】:

    您可以通过设置 NullRequestCache 来避免创建 SPRING_SECURITY_SAVED_REQUEST,但我想这不适用于您的用例。

    或者至少将它们限制为有效后端端点的定义列表?

    这可以通过提供 requestCache 并设置 RequestMatcher 来完成 -

          final HttpSessionRequestCache requestCache = new HttpSessionRequestCache();
            requestCache.setRequestMatcher(new AntPathRequestMatcher("/**"));
    
        http
            .authorizeRequests()
                .antMatchers("/", "/home").permitAll()
                .anyRequest().authenticated()
                .and()
                .requestCache().requestCache(requestCache)
                .and()...
    

    【讨论】:

      猜你喜欢
      • 2018-10-21
      • 1970-01-01
      • 1970-01-01
      • 2014-03-17
      • 2022-09-28
      • 2015-08-31
      • 2022-10-13
      • 2019-11-20
      • 2019-10-02
      相关资源
      最近更新 更多