当前位置: 首页 > 知识库问答 >
问题:

如何在java spring boot/spring security(HttpSecurity)中自动注销会话过期

赫连照
2023-03-14

通过自动注销,我的意思是当会话到期时,浏览器将自己重定向到注销url,而用户不必单击任何将他重定向到注销url的链接。

导入org.springframework.beans.factory.annotation.autowired;导入org.springframework.boot.autocigure.security.securityproperties;导入org.springframework.context.annotation.bean;导入org.springframework.context.annotation.configuration;导入org.springframework.core.annotation.order;导入org.springframework.security.access.vote.rolevoter;导入org.springframework.security.config.annotation.authentication.builders.authenticationmanagerbuilder;导入org.springframework.security.config.annotation.method.configuration.enableGlobalMethodSecurity;导入org.springframework.security.config.annotation.web.builders.HttpSecurity;导入org.springframework.security.config.annotation.web.builders.websecurity;导入org.springframework.security.config.annotation.web.configuration.enablewebsecurity;导入org.springframework.security.config.annotation.web.configuration.websecurityconfigeradapter;导入org.springframework.security.config.http.SessionCreationPolicy;

/**  * Created by plato on 5/5/2016.  */ 

@Configuration@EnableWebSecurity@EnableGlobalMethodSecurity(prePostEnabled=true)@Order(SecurityProperties.Access_Override_Order)公共类SecurityConfig扩展WebSecurityConfigurerAdapter{

    @Autowired
    DatabaseAuthenticationProvider authenticationProvider;

    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring().antMatchers("/js/**", "/css/**", "/img/**", "/templates/**", "/thymeleaf/**");
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.formLogin()
                .loginPage("/login")
                .failureUrl("/login?failed=true")
                .defaultSuccessUrl("/login-success")
                .and().logout()
                .logoutSuccessUrl("/")
                .and().authorizeRequests()
                .antMatchers("/admin**", "/api/admin/**").hasAuthority("ADMIN")
                .antMatchers("/**")
                .permitAll()
                .anyRequest().authenticated()
                .and().csrf().disable()
                .sessionManagement()
                .maximumSessions(1)
                .expiredUrl("/login?expired-session")
                .and()
                .invalidSessionUrl("/?invalid-session");
    }

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth.authenticationProvider(authenticationProvider).eraseCredentials(true);
    }
 }

共有1个答案

鲁泰宁
2023-03-14

客户必须轮询。那里的服务器无法“推”重定向。

客户机可以每隔X段时间轮询一次,其中X只比会话超时长一点。如果轮询的频率高于此频率,则会刷新会话,因此不会超时。客户端可以在每次用户交互时重置计时器。

 类似资料:
  • 问题内容: 我们的应用程序在30分钟后注销并重定向到登录页面,我在web.xml中指定会话超时,并使用requestProcessor进行重定向。我想向用户显示一条消息,指出会话过期后会话已过期,如何我可以这样做吗?自动注销?我想在“会话超时,请重新登录”页面上提示错误消息。那我怎么能检测到会话超时呢?有什么方法会自动触发吗? 问题答案: 创建一个活动检查器,该检查器每分钟检查是否发生了任何用户活

  • 问题内容: 我有一个会话验证过滤器,当会话过期时该过滤器注销用户。 这是一段代码,但是没有用。不起作用意味着即使会话过期,它也不会重定向到登录页面。 请帮助我解决此问题。 问题答案: 我有一个会话验证过滤器,当会话过期时该过滤器注销用户。 老实说,这完全没有道理。如果将已登录用户存储为会话的属性,并基于会话中已登录用户的存在来拦截“已登录”状态,则在任何时候都不需要手动注销该用户。会话已过期。当会

  • 只是一个关于Spring Security性和会话失效的问题。 当会话被ConcurrentSessionControlStrategy无效时,会话将通过调用removeSessionInformation方法从SessionRegistry中移除;然而,当会话被手动注销无效时,HttpSession也会无效,但不会调用SessionRegistry从那里移除条目。 我添加了HttpSession

  • 我正在学习springsecurity(基于java的配置),我无法使注销正常工作。当我点击注销时,我看到URL更改为http://localhost:8080/logout并获取“HTTP 404-/logout”。登录功能工作正常(即使使用自定义登录表单),但问题是注销,我怀疑重定向的url“localhost:8080/logout”应该类似于“localhost:8808/springte

  • 问题内容: 我在iOS应用中使用Firebase身份验证。当用户使用Firebase登录我的应用程序然后注销该用户的所有其他设备(会话)时,Firebase中是否有任何方法?我可以使用Firebase admin SDK来做到这一点吗? 问题答案: 当我遇到此问题时,我已使用云功能解决了该问题,请访问此链接以获取更多详细信息https://firebase.google.com/docs/auth

  • 我尝试了几乎所有我能在StackOverflow和其他地方找到的东西来让它工作,但它仍然不工作。我使用的是Spring框架4.1.6。发布,Spring安全4.0.0。释放。我配置了命名空间注销标记,我能够使会话无效的唯一方法是通过在我的控制器中使用HttpSession.invalidate()调用以编程方式执行它。 当请求注销时,我会被重定向到相应的页面,但是会话不会失效,JSSessionI