【问题标题】:Keep Session Attributes after logout in JSP在JSP中注销后保留会话属性
【发布时间】:2021-04-02 17:05:32
【问题描述】:

我正在为一个学校项目使用 JSP 和 Spring 创建一个网上商店。 该网站必须在没有连接的情况下可用(付款除外),因此即使我没有连接,我也可以将商品添加到我的购物车。 购物车中的商品保存为会话属性。

我的问题是,当用户连接并且购物车中有物品时,如果他注销,所有会话属性将被删除,因此购物车被清除。对于拥有多个帐户(即专业帐户和私人帐户)并希望切换另一个帐户进行付款的用户来说,这是不切实际的。

有没有办法在注销后保留某个会话属性?

这是我在 JSP 页面中调用注销的方式:

<a href="<spring:url value='/logout'/>">
   <spring:message code='logout'/>
</a>

这是来自 Spring Security 的默认调用。

这是我的安全配置 (WebSecurityConfiguration.java):

@Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable(); 

        http
                .authorizeRequests() 
                .antMatchers(AUTHORIZED_REQUESTS_ADMIN).hasRole("ADMIN") 
                .antMatchers(AUTHORIZED_REQUESTS_ANYBODY).permitAll() 
                .antMatchers(STATIC_RESOURCES).permitAll()
                .anyRequest().authenticated() 
                .and()
                .formLogin() // We define the login part here.
                .successHandler(new SavedRequestAwareAuthenticationSuccessHandler()) 
                .loginPage(LOGIN_REQUEST) 
                .defaultSuccessUrl("/home")
                .failureUrl("/error.jsp")
                .permitAll()
                .and()
                .logout()
                //.logoutUrl("")
                .logoutSuccessUrl("/home") 
                .permitAll();
    }

感谢您的帮助

【问题讨论】:

  • 您是否正在清除或重置任何文件中的session storage?默认情况下,只要打开特定选项卡,该选项卡的session 数据就不会被清除。我对Spring Security 不太熟悉,但logoutauthenticatedhttp 上的重定向方法可能会清除会话存储。
  • 您应该在这些情况下使用客户端 cookie。
  • @Sal 是的,logout 方法正在重置会话存储,我正在寻找一种方法来防止它。我找到了(简单,让我感到羞耻)解决方案并在下面发布了答案。感谢您的帮助!

标签: java jsp session spring-security


【解决方案1】:

我找到了解决方案(就在我的眼前): 在configure 函数中添加.invalidateHttpSession(false) 似乎是我想要的。

我的WebSecurityConfiguration.java 现在看起来像这样:

@Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable(); 

        http
                .authorizeRequests() 
                .antMatchers(AUTHORIZED_REQUESTS_ADMIN).hasRole("ADMIN") 
                .antMatchers(AUTHORIZED_REQUESTS_ANYBODY).permitAll() 
                .antMatchers(STATIC_RESOURCES).permitAll()
                .anyRequest().authenticated() 
                .and()
                .formLogin() // We define the login part here.
                .successHandler(new SavedRequestAwareAuthenticationSuccessHandler()) 
                .loginPage(LOGIN_REQUEST) 
                .defaultSuccessUrl("/home")
                .failureUrl("/error.jsp")
                .permitAll()
                .and()
                .logout()
                //.logoutUrl("")
                .logoutSuccessUrl("/home") 
                .permitAll()
                .invalidateHttpSession(false);
    }

感谢你们的帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-10
    • 1970-01-01
    • 1970-01-01
    • 2012-06-15
    相关资源
    最近更新 更多