简体   繁体   English

如果spring security session超时,如何在尝试执行ajax请求后将用户重定向到登录页面?

[英]How redirect user to login page after try to execute ajax request if spring security session is timeout?

I have spring boot app with spring security. 我有弹簧安全的春季启动应用程序。 I need check redirect to login page if user make request after session time out. 如果用户在会话超时后发出请求,我需要检查重定向到登录页面。 I have 2 page: login and index . 我有2页: loginindex On index page I get requests by Ajax . index页面上,我收到了Ajax请求。

I open index page. 我打开索引页面。 wait 30 sec. 等30秒 Try update page after Session timeout but I not redirect to login page. 在会话超时后尝试更新页面,但我没有重定向到登录页面。 But if I try update page F5 spring redirect me to login page. 但如果我尝试更新页面F5 spring将我重定向到登录页面。 But if I try make ajax request - it work find and I not redirect. 但是,如果我尝试发出ajax请求 - 它可以找到并且我不会重定向。

It is my configs: 这是我的配置:

http.authorizeRequests().
                antMatchers("/css/**").permitAll()
                .antMatchers("/", "/index/**").hasRole("USER")
                .anyRequest().hasRole("USER")
                .and()
                .formLogin()
                .loginPage("/login").permitAll().defaultSuccessUrl("/index")
                .and()
                .logout().logoutUrl("/logout").permitAll().logoutSuccessUrl("/login");
        http.csrf()
                .disable()
                .authorizeRequests()
                .antMatchers("/resources/**", "/**").permitAll()
                .anyRequest().permitAll()
                .and();

and button on index page: 和索引页面上的按钮:

<button type="button" class="btn btn-primary" onclick="reload()">show</button>

and js method: 和js方法:

function reload() {
        table.ajax.reload(null, false);
    }

When I press "show" button - my table send request to spring controller(Session already invalid) and controller return data and table update success/ But I need redirect user to login page. 当我按下“显示”按钮 - 我的表发送请求到spring控制器(Session已经无效)和控制器返回数据和表更新成功/但我需要将用户重定向到登录页面。

http
    .authorizeRequests()
        .antMatchers("/css/**", "/resources/**").permitAll()
        // ??? .antMatchers("/", "/index/**").hasRole("USER")
        .anyRequest().hasRole("USER")
        .and()
    .formLogin()
        .loginPage("/login").permitAll()
        .defaultSuccessUrl("/index")
        .and()
    .logout()           
         .logoutUrl("/logout").permitAll().logoutSuccessUrl("/login")
        .and()
    .csrf()
        .disable();

Can you try this ? 你能试试吗? and .antMatchers("/", "/index/**").hasRole("USER") is very confused. .antMatchers("/", "/index/**").hasRole("USER").antMatchers("/", "/index/**").hasRole("USER")非常困惑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring Security中的会话超时重定向处理PrimeFaces Ajax请求 - Redirect handling PrimeFaces Ajax requests on session timeout in Spring Security 在会话超时时将ajax请求重定向到sfDoctrineGuard登录symfony 1.4 - redirect ajax request to sfDoctrineGuard login on session timeout symfony 1.4 Laravel:在会话在ajax请求上过期后重定向到登录 - Laravel: Redirect to login after session has expired on ajax request 如果会话超时,如何在MVC中调用jquery ajax后重定向到新页面? - how to redirect to new page after jquery ajax call in MVC if session timeout? Spring安全会话超时处理Ajax调用 - Spring security Session Timeout handling for Ajax calls Spring安全性+ Ajax会话超时问题 - Spring security + Ajax session timeout issue 基于Ajax的应用程序,并希望在会话过期后重定向到登录页面 - Ajax based application and want to redirect to login page after session expired 重定向到 ajax 请求的登录页面 - Redirect to login page on ajax request 会话过期后的 Ajax 调用未重定向到登录页面 - Spring Boot - Ajax call after session expired not redirecting to login page - spring boot 在Spring MVC中检测Ajax请求中的会话超时 - Detect Session Timeout in Ajax Request in Spring MVC
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM