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,7 +31,15 @@ public abstract class AuthenticationException extends RuntimeException {
29
31
@ Serial
30
32
private static final long serialVersionUID = 2018827803361503060L ;
31
33
32
- private final Authentication authRequest ;
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 ;
33
43
34
44
/**
35
45
* Constructs an {@code AuthenticationException} with the specified message and root
@@ -52,8 +62,29 @@ public AuthenticationException(String msg) {
52
62
this .authRequest = null ;
53
63
}
54
64
65
+ /**
66
+ * Constructs an {@code AuthenticationException} with the specified message and root
67
+ * cause.
68
+ * @param msg the detail message
69
+ * @param authRequest details about the failed authentication request
70
+ */
55
71
public AuthenticationException (String msg , Authentication authRequest ) {
56
72
super (msg );
73
+ Assert .notNull (authRequest , "AuthRequest cannot be null" );
74
+ }
75
+
76
+ /**
77
+ * Sets the {@link Authentication} object representing the failed authentication
78
+ * attempt.
79
+ * <p>
80
+ * This method allows the injection of the authentication request that resulted in a
81
+ * failure. The provided {@code authRequest} should not be null if set.
82
+ * </p>
83
+ * @param authRequest the authentication request associated with the failed
84
+ * authentication attempt.
85
+ */
86
+ public void setAuthRequest (Authentication authRequest ) {
87
+ Assert .notNull (authRequest , "AuthRequest cannot be null" );
57
88
this .authRequest = authRequest ;
58
89
}
59
90
0 commit comments