spring-projects / spring-session

Spring Session
https://spring.io/projects/spring-session
Apache License 2.0
1.86k stars 1.12k forks source link

comprehensive tutorial with tests #576

Open xenoterracide opened 8 years ago

xenoterracide commented 8 years ago

I have never successfully gotten spring session to work. http://stackoverflow.com/q/28384907/206466 https://github.com/spring-projects/spring-session/issues/217

it would be nice to see an example project that enables one of the production modes, show map repository used for development and an MVC test that verifies the header.

vpavic commented 8 years ago

The project itself contains many sample projects that demonstrate most common use cases and are also covered in the documentation.

Can you provide more information about the problems you have encountered trying to use Spring Session?

xenoterracide commented 8 years ago

see all I've ever wanted to do was get it working with an in memory store for the header so I don't have JSESSIONID just for development, that way I can worry about setting up something like Redis later. On top of it, having looked through a number of samples none of the tests seem to verify that header (and admittedly I find the groovy api a little harder to read, I don't think it's doing a lot of mockmvc stuff though).

once tests are written and and the cookie can be worked with, then I could concern myself with backing it with a real database.

Might be worth saying since I'm using Spring Platform I'm still stuck on 1.0 and it seems there have been quite a few releases.

of course I've tried creating an

@Bean
new MapSessionRepository();

and a

HeaderHttpSessionStrategy

it might be valid to say that I should upgrade, but that still leaves out the how to address building something that ultimately uses both redis and and then the map impl for development along with tests for the headers.

vpavic commented 8 years ago

If I understood correctly what you're trying to achieve is to have an in-memory session store for development environment, and use external store such as Redis in production. Also you'd like to use HTTP header based session strategy in development, and cookie based one in production.

If so, your requirement is more about general configuration of your project rather than something specific to Spring Session itself. One way of addressing it would be using Spring's @Profile support.

Having said that, you'd definitely benefit upgrading the Spring Session. Overriding the version provided by Spring Platform is really easy.

You could define dev and prod application profiles and, if you upgrade to at least Spring Session 1.1, do something like this:

@Configuration
@Profile("dev")
@EnableSpringHttpSession
class SessionDevConfig {

    @Bean
    public MapSessionRepository sessionRepository() {
        return new MapSessionRepository();
    }

    @Bean
    public HttpSessionStrategy httpSessionStrategy() {
        return new HeaderHttpSessionStrategy(); 
    }

}

@Configuration
@Profile("prod")
@EnableRedisHttpSession
class SessionProdConfig {

    // omitted Redis connection config

}

Note that @EnableSpringHttpSession was introduced in Spring Session 1.1.

Regarding the tests, I hope some of the Spring Session's own HTTP tests might be useful to you.

xenoterracide commented 8 years ago

right so couldn't http://docs.spring.io/spring-session/docs/current/reference/html5/#api-mapsessionrepository be written clearer?

also all of the samples I see have this class https://github.com/spring-projects/spring-session/blob/master/samples/httpsession/src/main/java/sample/Initializer.java which isn't necessary? Or maybe it is? but if I had 2 configs.... the fact that it's a constructor calling super doing the switch... and it seems like something that could maybe be generated by the annotations on the config

side note: that does actually work (well I haven't tried redis yet, but I'll get to that later)

vpavic commented 8 years ago

right so couldn't http://docs.spring.io/spring-session/docs/current/reference/html5/#api-mapsessionrepository be written clearer?

I assume you're referring to the @EnableSpringHttpSession part that wasn't clear enough from there? It's covered in its own section but maybe we could consider mentioning it in the MapSessionRepository part of the docs. WDYT @rwinch?

also all of the samples I see have this class https://github.com/spring-projects/spring-session/blob/master/samples/httpsession/src/main/java/sample/Initializer.java which isn't necessary? Or maybe it is? but if I had 2 configs.... the fact that it's a constructor calling super doing the switch... and it seems like something that could maybe be generated by the annotations on the config

Well, you do need to somehow bootstrap the Spring context and register Spring Session's filter, and that's what the Initializer class (which is a WebApplicationInitializer) does in samples. You don't need it if you use Spring Boot since in that case it will be auto-configured for you, as show in the Boot sample.