You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
username: As identifiable to the UserDetailsService
27
28
password: That matches the one in the retrieved UserDetails
28
29
expirationTime: The date and time when the remember-me token expires, expressed in milliseconds
29
30
key: A private key to prevent modification of the remember-me token
31
+
algorithmName: The algorithm used to generate and to verify the remember-me token signature
30
32
----
33
+
====
31
34
32
35
As such the remember-me token is valid only for the period specified, and provided that the username, password and key does not change.
33
36
Notably, this has a potential security issue in that a captured remember-me token will be usable from any user agent until such time as the token expires.
@@ -38,13 +41,15 @@ Alternatively, remember-me services should simply not be used at all.
38
41
39
42
If you are familiar with the topics discussed in the chapter on xref:servlet/configuration/xml-namespace.adoc#ns-config[namespace configuration], you can enable remember-me authentication just by adding the `<remember-me>` element:
40
43
44
+
====
41
45
[source,xml]
42
46
----
43
47
<http>
44
48
...
45
49
<remember-me key="myAppKey"/>
46
50
</http>
47
51
----
52
+
====
48
53
49
54
The `UserDetailsService` will normally be selected automatically.
50
55
If you have more than one in your application context, you need to specify which one should be used with the `user-service-ref` attribute, where the value is the name of your `UserDetailsService` bean.
@@ -55,23 +60,27 @@ This approach is based on the article https://web.archive.org/web/20180819014446
55
60
There is a discussion on this in the comments section of this article.].
56
61
To use the this approach with namespace configuration, you would supply a datasource reference:
57
62
63
+
====
58
64
[source,xml]
59
65
----
60
66
<http>
61
67
...
62
68
<remember-me data-source-ref="someDataSource"/>
63
69
</http>
64
70
----
71
+
====
65
72
66
73
The database should contain a `persistent_logins` table, created using the following SQL (or equivalent):
67
74
75
+
====
68
76
[source,ddl]
69
77
----
70
78
create table persistent_logins (username varchar(64) not null,
71
79
series varchar(64) primary key,
72
80
token varchar(64) not null,
73
81
last_used timestamp not null)
74
82
----
83
+
====
75
84
76
85
[[remember-me-impls]]
77
86
== Remember-Me Interfaces and Implementations
@@ -80,6 +89,7 @@ It is also used within `BasicAuthenticationFilter`.
80
89
The hooks will invoke a concrete `RememberMeServices` at the appropriate times.
Please refer to the Javadoc for a fuller discussion on what the methods do, although note at this stage that `AbstractAuthenticationProcessingFilter` only calls the `loginFail()` and `loginSuccess()` methods.
94
105
The `autoLogin()` method is called by `RememberMeAuthenticationFilter` whenever the `SecurityContextHolder` does not contain an `Authentication`.
@@ -105,8 +116,56 @@ In addition, `TokenBasedRememberMeServices` requires A UserDetailsService from w
105
116
Some sort of logout command should be provided by the application that invalidates the cookie if the user requests this.
106
117
`TokenBasedRememberMeServices` also implements Spring Security's `LogoutHandler` interface so can be used with `LogoutFilter` to have the cookie cleared automatically.
107
118
108
-
The beans required in an application context to enable remember-me services are as follows:
119
+
By default, this implementation uses the MD5 algorithm to encode the token signature.
120
+
To verify the token signature, the algorithm retrieved from `algorithmName` is parsed and used.
121
+
If no `algorithmName` is present, the default matching algorithm will be used, which is MD5.
122
+
You can specify different algorithms for signature encoding and for signature matching, this allows users to safely upgrade to a different encoding algorithm while still able to verify old ones if there is no `algorithmName` present.
123
+
To do that you can specify your customized `TokenBasedRememberMeServices` as a Bean and use it in the configuration.
The following beans are required in an application context to enable remember-me services:
167
+
168
+
====
110
169
[source,xml]
111
170
----
112
171
<bean id="rememberMeFilter" class=
@@ -126,13 +185,13 @@ The beans required in an application context to enable remember-me services are
126
185
<property name="key" value="springRocks"/>
127
186
</bean>
128
187
----
188
+
====
129
189
130
190
Don't forget to add your `RememberMeServices` implementation to your `UsernamePasswordAuthenticationFilter.setRememberMeServices()` property, include the `RememberMeAuthenticationProvider` in your `AuthenticationManager.setProviders()` list, and add `RememberMeAuthenticationFilter` into your `FilterChainProxy` (typically immediately after your `UsernamePasswordAuthenticationFilter`).
131
191
132
192
133
193
=== PersistentTokenBasedRememberMeServices
134
-
This class can be used in the same way as `TokenBasedRememberMeServices`, but it additionally needs to be configured with a `PersistentTokenRepository` to store the tokens.
135
-
There are two standard implementations.
194
+
You can use this class in the same way as `TokenBasedRememberMeServices`, but it additionally needs to be configured with a `PersistentTokenRepository` to store the tokens.
136
195
137
196
* `InMemoryTokenRepositoryImpl` which is intended for testing only.
138
197
* `JdbcTokenRepositoryImpl` which stores the tokens in a database.
Copy file name to clipboardExpand all lines: web/src/main/java/org/springframework/security/web/authentication/rememberme/TokenBasedRememberMeServices.java
0 commit comments