Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring引导安全性:请求的url创建不需要的redis会话_Spring_Spring Boot_Session_Spring Security - Fatal编程技术网

Spring引导安全性:请求的url创建不需要的redis会话

Spring引导安全性:请求的url创建不需要的redis会话,spring,spring-boot,session,spring-security,Spring,Spring Boot,Session,Spring Security,假设登录url为“/login”。 有两种受保护的资源: “/受保护” “/”是指向“/protected”的302重定向 当未经身份验证的用户尝试访问“/protected”时,他将被重定向到“/login”。在后台创建了一个会话,其中存储了SPRING\u SECURITY\u SAVED\u请求,以便在成功登录后将用户重定向到“/protected”url 这是spring security的默认行为 我的问题: 即使用户调用“/”也会创建会话。因此,所有在没有有效登录信息的情况下调用

假设登录url为“/login”。 有两种受保护的资源:

  • “/受保护”
  • “/”是指向“/protected”的302重定向
当未经身份验证的用户尝试访问“/protected”时,他将被重定向到“/login”。在后台创建了一个会话,其中存储了SPRING\u SECURITY\u SAVED\u请求,以便在成功登录后将用户重定向到“/protected”url

这是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();
}

您可以通过设置NullRequestCache来避免SPRING\u SECURITY\u保存\u请求,但我想这对您的用例不起作用

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

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

      final HttpSessionRequestCache requestCache = new HttpSessionRequestCache();
        requestCache.setRequestMatcher(new AntPathRequestMatcher("/**"));

    http
        .authorizeRequests()
            .antMatchers("/", "/home").permitAll()
            .anyRequest().authenticated()
            .and()
            .requestCache().requestCache(requestCache)
            .and()...