1818
1919import java .io .Serial ;
2020
21+ import org .springframework .util .Assert ;
22+
2123/**
2224 * Abstract superclass for all exceptions related to an {@link Authentication} object
2325 * being invalid for whatever reason.
@@ -29,6 +31,16 @@ public abstract class AuthenticationException extends RuntimeException {
2931 @ Serial
3032 private static final long serialVersionUID = 2018827803361503060L ;
3133
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+
3244 /**
3345 * Constructs an {@code AuthenticationException} with the specified message and root
3446 * cause.
@@ -37,6 +49,7 @@ public abstract class AuthenticationException extends RuntimeException {
3749 */
3850 public AuthenticationException (String msg , Throwable cause ) {
3951 super (msg , cause );
52+ this .authRequest = null ;
4053 }
4154
4255 /**
@@ -46,6 +59,23 @@ public AuthenticationException(String msg, Throwable cause) {
4659 */
4760 public AuthenticationException (String msg ) {
4861 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 ;
4979 }
5080
5181}
0 commit comments