Skip to content

Commit a8ff29d

Browse files
committed
Deprecate Authentication#setAuthenticated
This commit deprecates the Authentication#setAuthenticated method in favor of implementing isAuthenticated(). Closes gh-16668 Signed-off-by: yoobin_mion <[email protected].>
1 parent 88a3801 commit a8ff29d

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

core/src/main/java/org/springframework/security/core/Authentication.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -131,7 +131,9 @@ public interface Authentication extends Principal, Serializable {
131131
* trusted (by passing <code>true</code> as the argument) is rejected due to the
132132
* implementation being immutable or implementing its own alternative approach to
133133
* {@link #isAuthenticated()}
134+
* @deprecated in favor of implementing {@link #isAuthenticated()}
134135
*/
136+
@Deprecated
135137
void setAuthenticated(boolean isAuthenticated) throws IllegalArgumentException;
136138

137139
}

docs/modules/ROOT/pages/migration/authentication.adoc

+51
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,54 @@ fun introspector(): OpaqueTokenIntrospector {
6666
}
6767
----
6868
======
69+
70+
== Deprecated Authentication#setAuthenticated
71+
72+
The `Authentication#setAuthenticated` method has been deprecated in favor of implementing the `isAuthenticated()` method.
73+
74+
Previously, implementations might use `setAuthenticated` to mark an authentication token as valid:
75+
76+
[source,java]
77+
----
78+
Authentication auth = // create authentication
79+
auth.setAuthenticated(true); // DEPRECATED
80+
----
81+
82+
Instead, implementations should override the `isAuthenticated()` method to determine authentication validity:
83+
84+
[tabs]
85+
======
86+
Java::
87+
+
88+
[source,java,role="primary"]
89+
----
90+
public class MyAuthentication implements Authentication {
91+
92+
// Other methods...
93+
94+
@Override
95+
public boolean isAuthenticated() {
96+
// Custom logic to determine if this authentication is valid
97+
return this.validated;
98+
}
99+
}
100+
----
101+
102+
Kotlin::
103+
+
104+
[source,kotlin,role="secondary"]
105+
----
106+
class MyAuthentication : Authentication {
107+
108+
// Other methods...
109+
110+
override fun isAuthenticated(): Boolean {
111+
// Custom logic to determine if this authentication is valid
112+
return this.validated
113+
}
114+
}
115+
----
116+
======
117+
118+
Note that existing implementations will still need to support `setAuthenticated` for backward compatibility,
119+
but new code should avoid calling this method.

0 commit comments

Comments
 (0)