Skip to content

Commit 1cfe849

Browse files
Ayush Kohlieleftherias
authored andcommitted
Add Java examples to session management docs
Closes gh-8979
1 parent 4302a86 commit 1cfe849

File tree

1 file changed

+82
-7
lines changed

1 file changed

+82
-7
lines changed

docs/manual/src/docs/asciidoc/_includes/servlet/authentication/session-management.adoc

Lines changed: 82 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,55 @@ Typical usage includes session-fixation protection attack prevention, detection
77
You can configure Spring Security to detect the submission of an invalid session ID and redirect the user to an appropriate URL.
88
This is achieved through the `session-management` element:
99

10-
[source,xml]
10+
====
11+
.Java
12+
[source,java,role="primary"]
13+
----
14+
@Override
15+
protected void configure(HttpSecurity http) throws Exception{
16+
http
17+
.sessionManagement(session -> session
18+
.invalidSessionUrl("/invalidSession.htm")
19+
);
20+
}
21+
----
22+
23+
.XML
24+
[source,xml,role="secondary"]
1125
----
1226
<http>
1327
...
1428
<session-management invalid-session-url="/invalidSession.htm" />
1529
</http>
1630
----
31+
====
1732

1833
Note that if you use this mechanism to detect session timeouts, it may falsely report an error if the user logs out and then logs back in without closing the browser.
1934
This is because the session cookie is not cleared when you invalidate the session and will be resubmitted even if the user has logged out.
2035
You may be able to explicitly delete the JSESSIONID cookie on logging out, for example by using the following syntax in the logout handler:
2136

22-
[source,xml]
37+
====
38+
.Java
39+
[source,java,role="primary"]
40+
----
41+
@Override
42+
protected void configure(HttpSecurity http) throws Exception{
43+
http
44+
.logout(logout -> logout
45+
.deleteCookies("JSESSIONID")
46+
);
47+
}
48+
----
49+
50+
.XML
51+
[source,xml,role="secondary"]
2352
----
2453
<http>
2554
<logout delete-cookies="JSESSIONID" />
2655
</http>
2756
----
57+
====
58+
2859

2960
Unfortunately this can't be guaranteed to work with every servlet container, so you will need to test it in your environment
3061

@@ -45,20 +76,46 @@ Header always set Set-Cookie "JSESSIONID=;Path=/tutorial;Expires=Thu, 01 Jan 197
4576
[[ns-concurrent-sessions]]
4677
=== Concurrent Session Control
4778
If you wish to place constraints on a single user's ability to log in to your application, Spring Security supports this out of the box with the following simple additions.
48-
First you need to add the following listener to your `web.xml` file to keep Spring Security updated about session lifecycle events:
79+
First, you need to add the following listener to your configuration to keep Spring Security updated about session lifecycle events:
4980

50-
[source,xml]
81+
====
82+
.Java
83+
[source,java,role="primary"]
84+
----
85+
@Bean
86+
public HttpSessionEventPublisher httpSessionEventPublisher() {
87+
return new HttpSessionEventPublisher();
88+
}
89+
----
90+
91+
.XML
92+
[source,xml,role="secondary"]
5193
----
5294
<listener>
5395
<listener-class>
5496
org.springframework.security.web.session.HttpSessionEventPublisher
5597
</listener-class>
5698
</listener>
5799
----
100+
====
58101

59102
Then add the following lines to your application context:
60103

61-
[source,xml]
104+
====
105+
.Java
106+
[source,java,role="primary"]
107+
----
108+
@Override
109+
protected void configure(HttpSecurity http) throws Exception {
110+
http
111+
.sessionManagement(session -> session
112+
.maximumSessions(1)
113+
);
114+
}
115+
----
116+
117+
.XML
118+
[source,xml,role="secondary"]
62119
----
63120
<http>
64121
...
@@ -67,19 +124,37 @@ Then add the following lines to your application context:
67124
</session-management>
68125
</http>
69126
----
127+
====
128+
70129

71130
This will prevent a user from logging in multiple times - a second login will cause the first to be invalidated.
72131
Often you would prefer to prevent a second login, in which case you can use
73132

74-
[source,xml]
133+
====
134+
.Java
135+
[source,java,role="primary"]
136+
----
137+
@Override
138+
protected void configure(HttpSecurity http) throws Exception {
139+
http
140+
.sessionManagement(session -> session
141+
.maximumSessions(1)
142+
.maxSessionsPreventsLogin(true)
143+
);
144+
}
145+
----
146+
147+
.XML
148+
[source,xml,role="secondary"]
75149
----
76150
<http>
77-
...
78151
<session-management>
79152
<concurrency-control max-sessions="1" error-if-maximum-exceeded="true" />
80153
</session-management>
81154
</http>
82155
----
156+
====
157+
83158

84159
The second login will then be rejected.
85160
By "rejected", we mean that the user will be sent to the `authentication-failure-url` if form-based login is being used.

0 commit comments

Comments
 (0)