18
18
19
19
import java .io .Serial ;
20
20
21
+ import org .springframework .util .Assert ;
22
+
21
23
/**
22
24
* Abstract superclass for all exceptions related to an {@link Authentication} object
23
25
* being invalid for whatever reason.
@@ -29,6 +31,16 @@ public abstract class AuthenticationException extends RuntimeException {
29
31
@ Serial
30
32
private static final long serialVersionUID = 2018827803361503060L ;
31
33
34
+ /**
35
+ * The {@link Authentication} object representing the failed authentication attempt.
36
+ * <p>
37
+ * This field captures the authentication request that was attempted but ultimately
38
+ * failed, providing critical information for diagnosing the failure and facilitating
39
+ * debugging. If set, the value must not be null.
40
+ * </p>
41
+ */
42
+ private Authentication authRequest ;
43
+
32
44
/**
33
45
* Constructs an {@code AuthenticationException} with the specified message and root
34
46
* cause.
@@ -37,6 +49,7 @@ public abstract class AuthenticationException extends RuntimeException {
37
49
*/
38
50
public AuthenticationException (String msg , Throwable cause ) {
39
51
super (msg , cause );
52
+ this .authRequest = null ;
40
53
}
41
54
42
55
/**
@@ -46,6 +59,23 @@ public AuthenticationException(String msg, Throwable cause) {
46
59
*/
47
60
public AuthenticationException (String msg ) {
48
61
super (msg );
62
+ this .authRequest = null ;
63
+ }
64
+
65
+
66
+ /**
67
+ * Sets the {@link Authentication} object representing the failed authentication
68
+ * attempt.
69
+ * <p>
70
+ * This method allows the injection of the authentication request that resulted in a
71
+ * failure. The provided {@code authRequest} should not be null if set.
72
+ * </p>
73
+ * @param authRequest the authentication request associated with the failed
74
+ * authentication attempt.
75
+ */
76
+ public void setAuthRequest (Authentication authRequest ) {
77
+ Assert .notNull (authRequest , "AuthRequest cannot be null" );
78
+ this .authRequest = authRequest ;
49
79
}
50
80
51
81
}
0 commit comments