Skip to content

Commit 0c9fbed

Browse files
Jay Bryantrwinch
authored andcommitted
Editing pass
I edited for spelling, grammar, punctuation, usage, and corporate voice, with the goal of making this content be consistent with our other content.
1 parent 08495b1 commit 0c9fbed

14 files changed

+1248
-910
lines changed

docs/src/docs/asciidoc/guides/boot-findbyusername.adoc

Lines changed: 59 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -4,137 +4,141 @@ Rob Winch
44

55
This guide describes how to use Spring Session to find sessions by username.
66

7-
NOTE: The completed guide can be found in the <<findbyusername-sample, findbyusername application>>.
7+
NOTE: You can find the completed guide in the <<findbyusername-sample, findbyusername application>>.
88

99

1010
[[findbyusername-assumptions]]
1111
== Assumptions
1212

13-
The guide assumes you have already added Spring Session using the built in Redis configuration support to your application.
13+
The guide assumes you have already added Spring Session to your application by using the built-in Redis configuration support.
1414
The guide also assumes you have already applied Spring Security to your application.
15-
However, we the guide will be somewhat general purpose and can be applied to any technology with minimal changes we will discuss.
15+
However, we the guide is somewhat general purpose and can be applied to any technology with minimal changes, which we discuss later in the guide.
1616

17-
[NOTE]
18-
====
19-
If you need to learn how to add Spring Session to your project, please refer to the listing of link:../#samples[samples and guides]
20-
====
17+
NOTE: If you need to learn how to add Spring Session to your project, see the listing of link:../#samples[samples and guides]
2118

2219
== About the Sample
2320

24-
Our sample is using this feature to invalidate the users session that might have been compromised.
21+
Our sample uses this feature to invalidate the users session that might have been compromised.
2522
Consider the following scenario:
2623

27-
* User goes to library and authenticates to the application
28-
* User goes home and realizes they forgot to log out
29-
* User can log in and terminate the session from the library using clues like the location, created time, last accessed time, etc.
24+
* User goes to library and authenticates to the application.
25+
* User goes home and realizes they forgot to log out.
26+
* User can log in and terminate the session from the library using clues like the location, created time, last accessed time, and so on.
3027

31-
Wouldn't it be nice if we could allow the user to invalidate the session at the library from any device they authenticate with?
28+
Would it not be nice if we could let the user invalidate the session at the library from any device with which they authenticate?
3229
This sample demonstrates how this is possible.
3330

3431
[[findbyindexnamesessionrepository]]
35-
== FindByIndexNameSessionRepository
32+
== Using `FindByIndexNameSessionRepository`
3633

37-
In order to look up a user by their username, you must first choose a `SessionRepository` that implements link:../#api-findbyindexnamesessionrepository[FindByIndexNameSessionRepository].
38-
Our sample application assumes that the Redis support is already setup, so we are ready to go.
34+
To look up a user by their username, you must first choose a `SessionRepository` that implements link:../#api-findbyindexnamesessionrepository[`FindByIndexNameSessionRepository`].
35+
Our sample application assumes that the Redis support is already set up, so we are ready to go.
3936

40-
== Mapping the username
37+
== Mapping the User Name
4138

42-
`FindByIndexNameSessionRepository` can only find a session by the username, if the developer instructs Spring Session what user is associated with the `Session`.
43-
This is done by ensuring that the session attribute with the name `FindByUsernameSessionRepository.PRINCIPAL_NAME_INDEX_NAME` is populated with the username.
39+
`FindByIndexNameSessionRepository` can find a session only by the user name if the developer instructs Spring Session what user is associated with the `Session`.
40+
You can do so by ensuring that the session attribute with the name `FindByUsernameSessionRepository.PRINCIPAL_NAME_INDEX_NAME` is populated with the username.
4441

45-
Generally, speaking this can be done with the following code immediately after the user authenticates:
42+
Generally speaking, you can do so with the following code immediately after the user authenticates:
4643

44+
====
4745
[source,java,indent=0]
4846
----
4947
include::{docs-test-dir}docs/FindByIndexNameSessionRepositoryTests.java[tags=set-username]
5048
----
49+
====
5150

52-
== Mapping the username with Spring Security
51+
== Mapping the User Name with Spring Security
5352

54-
Since we are using Spring Security, the user name is automatically indexed for us.
55-
This means we will not have to perform any steps to ensure the user name is indexed.
53+
Since we use Spring Security, the user name is automatically indexed for us.
54+
This means we need not perform any steps to ensure the user name is indexed.
5655

57-
== Adding Additional Data to Session
56+
== Adding Additional Data to the Session
5857

59-
It may be nice to associate additional information (i.e. IP Address, the browser, location, etc) to the session.
60-
This makes it easier for the user to know which session they are looking at.
58+
It may be nice to associate additional information (such as the IP Address, the browser, location, and other details) to the session.
59+
Doing so makes it easier for the user to know which session they are looking at.
6160

62-
To do this simply determine which session attribute you want to use and what information you wish to provide.
61+
To do so, determine which session attribute you want to use and what information you wish to provide.
6362
Then create a Java bean that is added as a session attribute.
64-
For example, our sample application includes the location and access type of the session
63+
For example, our sample application includes the location and access type of the session, as the following listing shows:
6564

65+
====
6666
[source,java,indent=0]
6767
----
6868
include::{samples-dir}boot/findbyusername/src/main/java/sample/session/SessionDetails.java[tags=class]
6969
----
70+
====
7071

71-
We then inject that information into the session on each HTTP request using a `SessionDetailsFilter`.
72-
For example:
72+
We then inject that information into the session on each HTTP request using a `SessionDetailsFilter`, as the following example shows:
7373

74+
====
7475
[source,java,indent=0]
7576
----
7677
include::{samples-dir}boot/findbyusername/src/main/java/sample/session/SessionDetailsFilter.java[tags=dofilterinternal]
7778
----
79+
====
7880

7981
We obtain the information we want and then set the `SessionDetails` as an attribute in the `Session`.
80-
When we retrieve the `Session` by username, we can then use the session to access our `SessionDetails` just like any other session attribute.
82+
When we retrieve the `Session` by user name, we can then use the session to access our `SessionDetails` as we would any other session attribute.
8183

82-
[NOTE]
83-
====
84-
You might be wondering at this point why Spring Session does not provide `SessionDetails` functionality out of the box.
85-
The reason, is twofold.
86-
The first is that it is very trivial for applications to implement this themselves.
87-
The second reason is that the information that is populated in the session (and how frequently that information is updated) is highly application dependent.
88-
====
84+
NOTE: You might wonder why Spring Session does not provide `SessionDetails` functionality out of the box.
85+
We have two reasons.
86+
The first reason is that it is very trivial for applications to implement this themselves.
87+
The second reason is that the information that is populated in the session (and how frequently that information is updated) is highly application-dependent.
8988

9089
== Finding sessions for a specific user
9190

9291
We can now find all the sessions for a specific user.
92+
The following example shows how to do so:
9393

94+
====
9495
[source,java,indent=0]
9596
----
9697
include::{samples-dir}boot/findbyusername/src/main/java/sample/mvc/IndexController.java[tags=findbyusername]
9798
----
99+
====
98100

99101
In our instance, we find all sessions for the currently logged in user.
100-
However, this could easily be modified for an administrator to use a form to specify which user to look up.
102+
However, you can modify this for an administrator to use a form to specify which user to look up.
101103

102104
[[findbyusername-sample]]
103-
== findbyusername Sample Application
105+
== `findbyusername` Sample Application
106+
107+
This section describes how to use the `findbyusername` sample application.
104108

105-
=== Running the findbyusername Sample Application
109+
=== Running the `findbyusername` Sample Application
106110

107111
You can run the sample by obtaining the {download-url}[source code] and invoking the following command:
108112

109-
[NOTE]
110-
====
111-
For the sample to work, you must https://redis.io/download[install Redis 2.8+] on localhost and run it with the default port (6379).
112-
Alternatively, you can update the `RedisConnectionFactory` to point to a Redis server.
113-
Another option is to use https://www.docker.com/[Docker] to run Redis on localhost. See https://hub.docker.com/_/redis/[Docker Redis repository] for detailed instructions.
114113
====
115-
116114
----
117115
$ ./gradlew :spring-session-sample-boot-findbyusername:bootRun
118116
----
117+
====
118+
119+
NOTE: For the sample to work, you must https://redis.io/download[install Redis 2.8+] on localhost and run it with the default port (6379).
120+
Alternatively, you can update the `RedisConnectionFactory` to point to a Redis server.
121+
Another option is to use https://www.docker.com/[Docker] to run Redis on localhost.
122+
See https://hub.docker.com/_/redis/[Docker Redis repository] for detailed instructions.
119123

120124
You should now be able to access the application at http://localhost:8080/
121125

122126
=== Exploring the security Sample Application
123127

124-
Try using the application. Enter the following to log in:
128+
You can now try using the application. Enter the following to log in:
125129

126-
* **Username** _user_
127-
* **Password** _password_
130+
* *Username* _user_
131+
* *Password* _password_
128132

129-
Now click the **Login** button.
133+
Now click the *Login* button.
130134
You should now see a message indicating your are logged in with the user entered previously.
131135
You should also see a listing of active sessions for the currently logged in user.
132136

133-
Let's emulate the flow we discussed in the <<About the Sample>> section
137+
You can emulate the flow we discussed in the <<About the Sample>> section by doing the following:
134138

135139
* Open a new incognito window and navigate to http://localhost:8080/
136140
* Enter the following to log in:
137-
** **Username** _user_
138-
** **Password** _password_
139-
* Terminate your original session
140-
* Refresh the original window and see you are logged out
141+
** *Username* _user_
142+
** *Password* _password_
143+
* Terminate your original session.
144+
* Refresh the original window and see that you are logged out.

docs/src/docs/asciidoc/guides/boot-jdbc.adoc

Lines changed: 50 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,17 @@
22
Rob Winch, Vedran Pavić
33
:toc:
44

5-
This guide describes how to use Spring Session to transparently leverage a relational database to back a web application's `HttpSession` when using Spring Boot.
5+
This guide describes how to use Spring Session to transparently leverage a relational database to back a web application's `HttpSession` when you use Spring Boot.
66

7-
NOTE: The completed guide can be found in the <<httpsession-jdbc-boot-sample, httpsession-jdbc-boot sample application>>.
7+
NOTE: You can find the completed guide in the <<httpsession-jdbc-boot-sample, httpsession-jdbc-boot sample application>>.
88

99
== Updating Dependencies
10-
Before you use Spring Session, you must ensure to update your dependencies.
10+
11+
Before you use Spring Session, you must update your dependencies.
1112
We assume you are working with a working Spring Boot web application.
12-
If you are using Maven, ensure to add the following dependencies:
13+
If you use Maven, you must add the following dependencies:
1314

15+
====
1416
.pom.xml
1517
[source,xml]
1618
[subs="verbatim,attributes"]
@@ -24,104 +26,116 @@ If you are using Maven, ensure to add the following dependencies:
2426
</dependency>
2527
</dependencies>
2628
----
29+
====
2730

28-
Spring Boot provides dependency management for Spring Session modules, so there's no need to explicitly declare dependency version.
31+
Spring Boot provides dependency management for Spring Session modules, so you need not explicitly declare the dependency version.
2932

3033
// tag::config[]
3134

3235
[[httpsession-jdbc-boot-spring-configuration]]
3336
== Spring Boot Configuration
3437

3538
After adding the required dependencies, we can create our Spring Boot configuration.
36-
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`:
39+
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`.
40+
The following listing shows how to do so:
3741

42+
====
3843
.src/main/resources/application.properties
3944
----
4045
spring.session.store-type=jdbc # Session store type.
4146
----
47+
====
4248

43-
Under the hood, Spring Boot will apply configuration that is equivalent to manually adding `@EnableJdbcHttpSession` annotation.
44-
This creates a Spring Bean with the name of `springSessionRepositoryFilter` that implements Filter.
45-
The filter is what is in charge of replacing the `HttpSession` implementation to be backed by Spring Session.
49+
Under the hood, Spring Boot applies configuration that is equivalent to manually adding the `@EnableJdbcHttpSession` annotation.
50+
This creates a Spring bean with the name of `springSessionRepositoryFilter`. That bean implements `Filter`.
51+
The filter is in charge of replacing the `HttpSession` implementation to be backed by Spring Session.
4652

47-
Further customization is possible using `application.properties`:
53+
You can further customize by using `application.properties`.
54+
The following listing shows how to do so:
4855

56+
====
4957
.src/main/resources/application.properties
5058
----
51-
server.servlet.session.timeout= # Session timeout. If a duration suffix is not specified, seconds will be used.
59+
server.servlet.session.timeout= # Session timeout. If a duration suffix is not specified, seconds are used.
5260
spring.session.jdbc.initialize-schema=embedded # Database schema initialization mode.
5361
spring.session.jdbc.schema=classpath:org/springframework/session/jdbc/schema-@@platform@@.sql # Path to the SQL file to use to initialize the database schema.
5462
spring.session.jdbc.table-name=SPRING_SESSION # Name of the database table used to store sessions.
5563
----
64+
====
5665

57-
For more information, refer to https://docs.spring.io/spring-boot/docs/{spring-boot-version}/reference/htmlsingle/#boot-features-session[Spring Session] portion of the Spring Boot documentation.
66+
For more information, see the https://docs.spring.io/spring-boot/docs/{spring-boot-version}/reference/htmlsingle/#boot-features-session[Spring Session] portion of the Spring Boot documentation.
5867

5968
[[httpsession-jdbc-boot-configuration]]
60-
== Configuring the DataSource
69+
== Configuring the `DataSource`
6170

62-
Spring Boot automatically creates a `DataSource` that connects Spring Session to an embedded instance of H2 database.
63-
In a production environment you need to ensure to update your configuration to point to your relational database.
64-
For example, you can include the following in your *application.properties*
71+
Spring Boot automatically creates a `DataSource` that connects Spring Session to an embedded instance of an H2 database.
72+
In a production environment, you need to update your configuration to point to your relational database.
73+
For example, you can include the following in your application.properties:
6574

75+
====
6676
.src/main/resources/application.properties
6777
----
6878
spring.datasource.url= # JDBC URL of the database.
6979
spring.datasource.username= # Login username of the database.
7080
spring.datasource.password= # Login password of the database.
7181
----
82+
====
7283

73-
For more information, refer to https://docs.spring.io/spring-boot/docs/{spring-boot-version}/reference/htmlsingle/#boot-features-configure-datasource[Configure a DataSource] portion of the Spring Boot documentation.
84+
For more information, see the https://docs.spring.io/spring-boot/docs/{spring-boot-version}/reference/htmlsingle/#boot-features-configure-datasource[Configure a DataSource] portion of the Spring Boot documentation.
7485

7586
[[httpsession-jdbc-boot-servlet-configuration]]
7687
== Servlet Container Initialization
7788

78-
Our <<httpsession-jdbc-boot-spring-configuration,Spring Boot Configuration>> created a Spring Bean named `springSessionRepositoryFilter` that implements `Filter`.
89+
Our <<httpsession-jdbc-boot-spring-configuration,Spring Boot Configuration>> created a Spring bean named `springSessionRepositoryFilter` that implements `Filter`.
7990
The `springSessionRepositoryFilter` bean is responsible for replacing the `HttpSession` with a custom implementation that is backed by Spring Session.
8091

8192
In order for our `Filter` to do its magic, Spring needs to load our `Config` class.
82-
Last we need to ensure that our Servlet Container (i.e. Tomcat) uses our `springSessionRepositoryFilter` for every request.
93+
Last, we need to ensure that our Servlet Container (that is, Tomcat) uses our `springSessionRepositoryFilter` for every request.
8394
Fortunately, Spring Boot takes care of both of these steps for us.
8495

8596
// end::config[]
8697

8798
[[httpsession-jdbc-boot-sample]]
88-
== httpsession-jdbc-boot Sample Application
99+
== `httpsession-jdbc-boot` Sample Application
89100

90-
The httpsession-jdbc-boot Sample Application demonstrates how to use Spring Session to transparently leverage H2 database to back a web application's `HttpSession` when using Spring Boot.
101+
The httpsession-jdbc-boot Sample Application demonstrates how to use Spring Session to transparently leverage an H2 database to back a web application's `HttpSession` when you use Spring Boot.
91102

92103
[[httpsession-jdbc-boot-running]]
93-
=== Running the httpsession-jdbc-boot Sample Application
104+
=== Running the `httpsession-jdbc-boot` Sample Application
94105

95106
You can run the sample by obtaining the {download-url}[source code] and invoking the following command:
96107

108+
====
97109
----
98110
$ ./gradlew :spring-session-sample-boot-jdbc:bootRun
99111
----
112+
====
100113

101114
You should now be able to access the application at http://localhost:8080/
102115

103116
[[httpsession-jdbc-boot-explore]]
104-
=== Exploring the security Sample Application
117+
=== Exploring the Security Sample Application
105118

106-
Try using the application. Enter the following to log in:
119+
You can now try using the application.
120+
To do so, enter the following to log in:
107121

108-
* **Username** _user_
109-
* **Password** _password_
122+
* *Username* _user_
123+
* *Password* _password_
110124

111-
Now click the **Login** button.
112-
You should now see a message indicating your are logged in with the user entered previously.
113-
The user's information is stored in H2 database rather than Tomcat's `HttpSession` implementation.
125+
Now click the *Login* button.
126+
You should now see a message indicating that your are logged in with the user entered previously.
127+
The user's information is stored in the H2 database rather than Tomcat's `HttpSession` implementation.
114128

115129
[[httpsession-jdbc-boot-how]]
116-
=== How does it work?
130+
=== How Does It Work?
117131

118-
Instead of using Tomcat's `HttpSession`, we are actually persisting the values in H2 database.
132+
Instead of using Tomcat's `HttpSession`, we persist the values in the H2 database.
119133
Spring Session replaces the `HttpSession` with an implementation that is backed by a relational database.
120-
When Spring Security's `SecurityContextPersistenceFilter` saves the `SecurityContext` to the `HttpSession` it is then persisted into H2 database.
134+
When Spring Security's `SecurityContextPersistenceFilter` saves the `SecurityContext` to the `HttpSession`, it is then persisted into the H2 database.
121135

122-
When a new `HttpSession` is created, Spring Session creates a cookie named SESSION in your browser that contains the id of your session.
123-
Go ahead and view the cookies (click for help with https://developers.google.com/web/tools/chrome-devtools/manage-data/cookies[Chrome] or https://developer.mozilla.org/en-US/docs/Tools/Storage_Inspector[Firefox]).
136+
When a new `HttpSession` is created, Spring Session creates a cookie named `SESSION` in your browser. That cookie contains the ID of your session.
137+
You can view the cookies (with https://developers.google.com/web/tools/chrome-devtools/manage-data/cookies[Chrome] or https://developer.mozilla.org/en-US/docs/Tools/Storage_Inspector[Firefox]).
124138

125-
If you like, you can easily remove the session using H2 web console available at: http://localhost:8080/h2-console/ (use `jdbc:h2:mem:testdb` for JDBC URL)
139+
You can remove the session by using the H2 web console available at: http://localhost:8080/h2-console/ (use `jdbc:h2:mem:testdb` for JDBC URL).
126140

127-
Now visit the application at http://localhost:8080/ and observe that we are no longer authenticated.
141+
Now you can visit the application at http://localhost:8080/ and see that we are no longer authenticated.

0 commit comments

Comments
 (0)