jeetmp3 / spring-session

Apache License 2.0
18 stars 24 forks source link

flash scoped messages #5

Closed jprabawa closed 8 years ago

jprabawa commented 8 years ago

Im running Grails 2.4.0 and spring-session-1.1 and the plugin seems to work fine overall except that my flash scoped messages do not get retained after redirect. Is this a known issue or do you have an idea of how I can get around it?

Thanks

jeetmp3 commented 8 years ago

Hi @jprabawa this is expected behavior of actual spring session project. Check the conversation https://github.com/spring-projects/spring-session/issues/177 Flash scope is a map which persist in redis at the time of session creation. So if we store any object in flash scope (map) it doesn't sync with redis. If you really needed this behavior we can optionally allow updation of mutable objects in session. This feature will be available on next version.

dizonn commented 8 years ago

Hello @jeetmp3 , will this fix chain method of controllers?

jeetmp3 commented 8 years ago

Hello @dizonn yes it'll fix that too.

jprabawa commented 8 years ago

Thanks @jeetmp3 ! Would be great if that'll solve the issue with flash messages. I've been looking for a better way to show user notifications but flash messages are still the most convenient way of doing things especially with the Post/Redirect/Get pattern.

jeetmp3 commented 8 years ago

Hi @jprabawa and @dizonn we've publish new version of plugin which includes support to update mutable object. A new config property added to enable this feature

springsession.allow.persist.mutable = true

Read Documentation

rwinch commented 8 years ago

@jeetmp3 Out of curiosity what changes did you make to support this?

jeetmp3 commented 8 years ago

@rwinch we've added a filter just after the sessionRepositoryFilter. Just before sending response back, it again put updated objects in session by using setAttribute(). By default this filter doesn't do anything unless we explicitly enable it. Are you guys planning to add some kind of support in spring-session ?

rwinch commented 8 years ago

@jeetmp3 Sorry for the delayed response. I have been considering adding an approach that writes the entire session every time. This would make it so that a setting within Spring Data would allow this to work.

robertoschwald commented 3 years ago

Just for completeness: Spring-Session got a similar feature since 2.2.0 to set SaveMode.ON_GET_ATTRIBUTE on the SessionRepository, which re-sets all Objects in the Session which were obtained. This way, it is ensured mutable objects are distributed to the cache. This can be set using a SessionRepositoryCustomizer bean.

Sushil2044 commented 3 years ago

springsession.allow.persist.mutable = true Above line works on grails 2 but not working on grails 3...any idea

robertoschwald commented 3 years ago

As written before, Spring-Session >= 2.2.0 got a feature to set session entries on getAttribute(). Here is a SessionRepositoryCustomizer for Hazelcast to support Flash scope. You may use it for your used storage.

import groovy.transform.CompileStatic
import groovy.util.logging.Slf4j
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.session.FlushMode
import org.springframework.session.SaveMode
import org.springframework.session.config.SessionRepositoryCustomizer
import org.springframework.session.hazelcast.HazelcastIndexedSessionRepository

/**
 * Hazelcast Spring-Session SessionRepository configuration.
 * Used to re-configure the HazelcastIndexedSessionRepository.
 * {@see org.springframework.boot.autoconfigure.cache.HazelcastInstanceConfiguration}
 */
@Slf4j
@CompileStatic
@Configuration
class HazelcastSymInstanceConfiguration {

  /**
   * Reconfigure SessionRepository to re-save all session keys which have been obtained using getAttribute().
   * This way we ensure mutable objects are updated coherently in the Hazelcast cache.
   * Mutable objects in session are considered bad practice, but Grails uses them at least in:
   *  - Flash Messages
   *  - Form Tokens
   * {@see https://github.com/spring-projects/spring-session/issues/177}
   *
   * @return Customizer
   */
  @Bean
  SessionRepositoryCustomizer<HazelcastIndexedSessionRepository> customize() {
    return new SessionRepositoryCustomizer<HazelcastIndexedSessionRepository>() {
      @Override
      void customize(HazelcastIndexedSessionRepository sessionRepository) {
        log.info("Re-Configuring SessionRepositoryCustomizer to re-save session keys on getAttribute")
        sessionRepository.setFlushMode(FlushMode.ON_SAVE)
        sessionRepository.setSaveMode(SaveMode.ON_GET_ATTRIBUTE)
      }
    }
  }
}