diff --git a/docs/src/docs/asciidoc/guides/boot-jdbc.adoc b/docs/src/docs/asciidoc/guides/boot-jdbc.adoc index 2755f142..36d8138b 100644 --- a/docs/src/docs/asciidoc/guides/boot-jdbc.adoc +++ b/docs/src/docs/asciidoc/guides/boot-jdbc.adoc @@ -21,65 +21,40 @@ If you are using Maven, ensure to add the following dependencies: org.springframework.session spring-session-jdbc - {spring-session-version} - - - org.springframework.boot - spring-boot-starter-jdbc ---- -ifeval::["{version-snapshot}" == "true"] -Since we are using a SNAPSHOT version, we need to ensure to add the Spring Snapshot Maven Repository. -Ensure you have the following in your pom.xml: - -.pom.xml -[source,xml] ----- - - - - - - spring-snapshot - https://repo.spring.io/libs-snapshot - - ----- -endif::[] - -ifeval::["{version-milestone}" == "true"] -Since We are using a Milestone version, we need to ensure to add the Spring Milestone Maven Repository. -Ensure you have the following in your pom.xml: - -.pom.xml -[source,xml] ----- - - spring-milestone - https://repo.spring.io/libs-milestone - ----- -endif::[] +Spring Boot provides dependency management for Spring Session modules, so there's no need to explicitly declare dependency version. // tag::config[] [[httpsession-jdbc-boot-spring-configuration]] -== Spring Configuration +== Spring Boot Configuration -After adding the required dependencies, we can create our Spring configuration. -The Spring configuration is responsible for creating a Servlet Filter that replaces the `HttpSession` implementation with an implementation backed by Spring Session. -Add the following Spring Configuration: +After adding the required dependencies, we can create our Spring Boot configuration. +Thanks to first-class auto configuration support, setting up Spring Session backed by a relational database is as simple as adding a single configuration property to your `application.properties`: -[source,java] +.src/main/resources/application.properties ---- -include::{samples-dir}boot/jdbc/src/main/java/sample/config/HttpSessionConfig.java[tags=class] +spring.session.store-type=jdbc ---- -<1> The `@EnableJdbcHttpSession` annotation creates a Spring Bean with the name of `springSessionRepositoryFilter` that implements Filter. +Under the hood, Spring Boot will apply configuration that is equivalent to manually adding `@EnableJdbcHttpSession` annotation. +This creates a Spring Bean with the name of `springSessionRepositoryFilter` that implements Filter. The filter is what is in charge of replacing the `HttpSession` implementation to be backed by Spring Session. -In this instance Spring Session is backed by a relational database. + +Further customization is possible using `application.properties`: + +.src/main/resources/application.properties +---- +server.session.timeout= # Session timeout in seconds. +spring.session.jdbc.initializer.enabled= # Create the required session tables on startup if necessary. Enabled automatically if the default table name is set or a custom schema is configured. +spring.session.jdbc.schema=classpath:org/springframework/session/jdbc/schema-@@platform@@.sql # Path to the SQL file to use to initialize the database schema. +spring.session.jdbc.table-name=SPRING_SESSION # Name of database table used to store sessions. +---- + +For more information, refer to http://docs.spring.io/spring-boot/docs/{spring-boot-version}/reference/htmlsingle/#boot-features-session[Spring Session] portion of the Spring Boot documentation. [[httpsession-jdbc-boot-configuration]] == Configuring the DataSource @@ -95,12 +70,12 @@ spring.datasource.username=myapp spring.datasource.password=secret ---- -For more information, refer to http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-configure-datasource[Configure a DataSource] portion of the Spring Boot documentation. +For more information, refer to http://docs.spring.io/spring-boot/docs/{spring-boot-version}/reference/htmlsingle/#boot-features-configure-datasource[Configure a DataSource] portion of the Spring Boot documentation. [[httpsession-jdbc-boot-servlet-configuration]] == Servlet Container Initialization -Our <> created a Spring Bean named `springSessionRepositoryFilter` that implements `Filter`. +Our <> created a Spring Bean named `springSessionRepositoryFilter` that implements `Filter`. The `springSessionRepositoryFilter` bean is responsible for replacing the `HttpSession` with a custom implementation that is backed by Spring Session. In order for our `Filter` to do its magic, Spring needs to load our `Config` class. diff --git a/docs/src/docs/asciidoc/guides/boot-redis.adoc b/docs/src/docs/asciidoc/guides/boot-redis.adoc index a80c0045..c270d3e4 100644 --- a/docs/src/docs/asciidoc/guides/boot-redis.adoc +++ b/docs/src/docs/asciidoc/guides/boot-redis.adoc @@ -1,5 +1,5 @@ = Spring Session - Spring Boot -Rob Winch +Rob Winch, Vedran Pavić :toc: This guide describes how to use Spring Session to transparently leverage Redis to back a web application's `HttpSession` when using Spring Boot. @@ -20,64 +20,38 @@ If you are using Maven, ensure to add the following dependencies: org.springframework.session - spring-session - {spring-session-version} - - - org.springframework.boot - spring-boot-starter-data-redis + spring-session-data-redis ---- -ifeval::["{version-snapshot}" == "true"] -Since We are using a SNAPSHOT version, we need to ensure to add the Spring Snapshot Maven Repository. -Ensure you have the following in your pom.xml: - -.pom.xml -[source,xml] ----- - - - - - - spring-snapshot - https://repo.spring.io/libs-snapshot - - ----- -endif::[] - -ifeval::["{version-milestone}" == "true"] -Since We are using a Milestone version, we need to ensure to add the Spring Milestone Maven Repository. -Ensure you have the following in your pom.xml: - -.pom.xml -[source,xml] ----- - - spring-milestone - https://repo.spring.io/libs-milestone - ----- -endif::[] +Spring Boot provides dependency management for Spring Session modules, so there's no need to explicitly declare dependency version. [[boot-spring-configuration]] -== Spring Configuration +== Spring Boot Configuration -After adding the required dependencies, we can create our Spring configuration. -The Spring configuration is responsible for creating a Servlet Filter that replaces the `HttpSession` implementation with an implementation backed by Spring Session. -Add the following Spring Configuration: +After adding the required dependencies, we can create our Spring Boot configuration. +Thanks to first-class auto configuration support, setting up Spring Session backed by Redis is as simple as adding a single configuration property to your `application.properties`: -[source,java] +.src/main/resources/application.properties ---- -include::{samples-dir}boot/redis/src/main/java/sample/config/HttpSessionConfig.java[tags=class] +spring.session.store-type=redis ---- -<1> The `@EnableRedisHttpSession` annotation creates a Spring Bean with the name of `springSessionRepositoryFilter` that implements Filter. +Under the hood, Spring Boot will apply configuration that is equivalent to manually adding `@EnableRedisHttpSession` annotation. +This creates a Spring Bean with the name of `springSessionRepositoryFilter` that implements Filter. The filter is what is in charge of replacing the `HttpSession` implementation to be backed by Spring Session. -In this instance Spring Session is backed by Redis. + +Further customization is possible using `application.properties`: + +.src/main/resources/application.properties +---- +server.session.timeout= # Session timeout in seconds. +spring.session.redis.flush-mode= # Sessions flush mode. +spring.session.redis.namespace= # Namespace for keys used to store sessions. +---- + +For more information, refer to http://docs.spring.io/spring-boot/docs/{spring-boot-version}/reference/htmlsingle/#boot-features-session[Spring Session] portion of the Spring Boot documentation. [[boot-redis-configuration]] == Configuring the Redis Connection @@ -93,12 +67,12 @@ spring.redis.password=secret spring.redis.port=6379 ---- -For more information, refer to http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-connecting-to-redis[Connecting to Redis] portion of the Spring Boot documentation. +For more information, refer to http://docs.spring.io/spring-boot/docs/{spring-boot-version}/reference/htmlsingle/#boot-features-connecting-to-redis[Connecting to Redis] portion of the Spring Boot documentation. [[boot-servlet-configuration]] == Servlet Container Initialization -Our <> created a Spring Bean named `springSessionRepositoryFilter` that implements `Filter`. +Our <> created a Spring Bean named `springSessionRepositoryFilter` that implements `Filter`. The `springSessionRepositoryFilter` bean is responsible for replacing the `HttpSession` with a custom implementation that is backed by Spring Session. In order for our `Filter` to do its magic, Spring needs to load our `Config` class. @@ -106,12 +80,12 @@ Last we need to ensure that our Servlet Container (i.e. Tomcat) uses our `spring Fortunately, Spring Boot takes care of both of these steps for us. [[boot-sample]] -== boot Sample Application +== Boot Sample Application -The boot Sample Application demonstrates how to use Spring Session to transparently leverage Redis to back a web application's `HttpSession` when using Spring Boot. +The Boot Sample Application demonstrates how to use Spring Session to transparently leverage Redis to back a web application's `HttpSession` when using Spring Boot. [[boot-running]] -=== Running the boot Sample Application +=== Running the Boot Sample Application You can run the sample by obtaining the {download-url}[source code] and invoking the following command: diff --git a/docs/src/docs/asciidoc/guides/boot-websocket.adoc b/docs/src/docs/asciidoc/guides/boot-websocket.adoc index cef009c7..52ef555d 100644 --- a/docs/src/docs/asciidoc/guides/boot-websocket.adoc +++ b/docs/src/docs/asciidoc/guides/boot-websocket.adoc @@ -73,12 +73,11 @@ You can run the sample by obtaining the {download-url}[source code] and invoking [TIP] ==== -For the purposes of testing session expiration, you may want to change the session expiration to be 1 minute (default is 30 minutes) by removing the comment from the following file before starting the application: +For the purposes of testing session expiration, you may want to change the session expiration to be 1 minute (default is 30 minutes) by adding the following configuration property starting before the application: -.src/main/java/samples/config/WebSecurityConfig.java -[source,java] +.src/main/resources/application.properties ---- -include::{samples-dir}boot/websocket/src/main/java/sample/config/WebSecurityConfig.java[tags=enable-redis-httpsession] +server.session.timeout=60 ---- ==== diff --git a/samples/boot/findbyusername/spring-session-sample-boot-findbyusername.gradle b/samples/boot/findbyusername/spring-session-sample-boot-findbyusername.gradle index f765848f..417b4b8d 100644 --- a/samples/boot/findbyusername/spring-session-sample-boot-findbyusername.gradle +++ b/samples/boot/findbyusername/spring-session-sample-boot-findbyusername.gradle @@ -2,9 +2,9 @@ apply plugin: 'io.spring.convention.spring-sample-boot' dependencies { compile project(':spring-session-data-redis') - compile "org.springframework.boot:spring-boot-starter-security" compile "org.springframework.boot:spring-boot-starter-thymeleaf" - compile "org.springframework.boot:spring-boot-starter-web" + compile "org.springframework.boot:spring-boot-starter-security" + compile "org.springframework.boot:spring-boot-devtools" compile "nz.net.ultraq.thymeleaf:thymeleaf-layout-dialect" compile "org.webjars:bootstrap" compile "org.webjars:html5shiv" @@ -16,7 +16,6 @@ dependencies { testCompile "org.assertj:assertj-core" integrationTestCompile seleniumDependencies - } integrationTest { @@ -24,8 +23,9 @@ integrationTest { systemProperties['spring.session.redis.namespace'] = project.name } } + integrationTest { doFirst { systemProperties['spring.session.redis.namespace'] = project.name } -} \ No newline at end of file +} diff --git a/samples/boot/findbyusername/src/main/java/sample/config/SecurityConfig.java b/samples/boot/findbyusername/src/main/java/sample/config/SecurityConfig.java index 575cfc73..7b77ed16 100644 --- a/samples/boot/findbyusername/src/main/java/sample/config/SecurityConfig.java +++ b/samples/boot/findbyusername/src/main/java/sample/config/SecurityConfig.java @@ -1,5 +1,5 @@ /* - * Copyright 2014-2016 the original author or authors. + * Copyright 2014-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,29 +16,27 @@ package sample.config; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; +import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.web.builders.HttpSecurity; -import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; /** * @author Rob Winch */ -@EnableWebSecurity +@Configuration public class SecurityConfig extends WebSecurityConfigurerAdapter { // tag::config[] @Override protected void configure(HttpSecurity http) throws Exception { - http.formLogin().loginPage("/login").permitAll().and().authorizeRequests() - .antMatchers("/resources/**", "/webjars/**").permitAll().anyRequest().authenticated() - .and().logout().permitAll(); + http + .formLogin() + .loginPage("/login") + .permitAll() + .and() + .authorizeRequests() + .anyRequest().authenticated(); } // end::config[] - @Autowired - public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { - auth.inMemoryAuthentication().withUser("user").password("password").roles("USER"); - } } diff --git a/samples/boot/findbyusername/src/main/java/sample/mvc/LoginController.java b/samples/boot/findbyusername/src/main/java/sample/config/WebMvcConfig.java similarity index 52% rename from samples/boot/findbyusername/src/main/java/sample/mvc/LoginController.java rename to samples/boot/findbyusername/src/main/java/sample/config/WebMvcConfig.java index 7403b021..adfeb66f 100644 --- a/samples/boot/findbyusername/src/main/java/sample/mvc/LoginController.java +++ b/samples/boot/findbyusername/src/main/java/sample/config/WebMvcConfig.java @@ -1,5 +1,5 @@ /* - * Copyright 2014-2016 the original author or authors. + * Copyright 2014-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -14,21 +14,18 @@ * limitations under the License. */ -package sample.mvc; +package sample.config; -import org.springframework.stereotype.Controller; -import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; -/** - * Returns view for log in page - * - * @author Rob Winch - */ -@Controller -public class LoginController { +@Configuration +public class WebMvcConfig extends WebMvcConfigurerAdapter { - @RequestMapping("/login") - public String login() { - return "login"; + @Override + public void addViewControllers(ViewControllerRegistry registry) { + registry.addViewController("/login").setViewName("login"); } + } diff --git a/samples/boot/findbyusername/src/main/resources/application.properties b/samples/boot/findbyusername/src/main/resources/application.properties index f0baad36..b1c5b03e 100644 --- a/samples/boot/findbyusername/src/main/resources/application.properties +++ b/samples/boot/findbyusername/src/main/resources/application.properties @@ -1,2 +1,2 @@ -spring.thymeleaf.cache=false -spring.template.cache=false +spring.session.store-type=redis +security.user.password=password diff --git a/samples/boot/findbyusername/src/main/resources/static/resources/img/favicon.ico b/samples/boot/findbyusername/src/main/resources/static/favicon.ico similarity index 100% rename from samples/boot/findbyusername/src/main/resources/static/resources/img/favicon.ico rename to samples/boot/findbyusername/src/main/resources/static/favicon.ico diff --git a/samples/boot/findbyusername/src/main/resources/static/resources/img/logo.png b/samples/boot/findbyusername/src/main/resources/static/images/logo.png similarity index 100% rename from samples/boot/findbyusername/src/main/resources/static/resources/img/logo.png rename to samples/boot/findbyusername/src/main/resources/static/images/logo.png diff --git a/samples/boot/findbyusername/src/main/resources/templates/layout.html b/samples/boot/findbyusername/src/main/resources/templates/layout.html index 40863f64..5c5a3a3f 100644 --- a/samples/boot/findbyusername/src/main/resources/templates/layout.html +++ b/samples/boot/findbyusername/src/main/resources/templates/layout.html @@ -4,7 +4,7 @@ xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"> Spring Session Sample - + - + @@ -79,10 +79,10 @@