Skip to content

Commit 60bed7f

Browse files
committed
Polish AuthenticationRequest Property
- Add getter for reading the request - Update BadCredentialsMixing to ignore authentication - Allow exception to be mutable Issue gh-16444
1 parent 3b6aca0 commit 60bed7f

File tree

3 files changed

+23
-21
lines changed

3 files changed

+23
-21
lines changed

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

+21-20
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,7 @@ public abstract class AuthenticationException extends RuntimeException {
3131
@Serial
3232
private static final long serialVersionUID = 2018827803361503060L;
3333

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;
34+
private Authentication authenticationRequest;
4335

4436
/**
4537
* Constructs an {@code AuthenticationException} with the specified message and root
@@ -49,7 +41,6 @@ public abstract class AuthenticationException extends RuntimeException {
4941
*/
5042
public AuthenticationException(String msg, Throwable cause) {
5143
super(msg, cause);
52-
this.authRequest = null;
5344
}
5445

5546
/**
@@ -59,23 +50,33 @@ public AuthenticationException(String msg, Throwable cause) {
5950
*/
6051
public AuthenticationException(String msg) {
6152
super(msg);
62-
this.authRequest = null;
6353
}
6454

55+
/**
56+
* Get the {@link Authentication} object representing the failed authentication
57+
* attempt.
58+
* <p>
59+
* This field captures the authentication request that was attempted but ultimately
60+
* failed, providing critical information for diagnosing the failure and facilitating
61+
* debugging
62+
* @since 6.5
63+
*/
64+
public Authentication getAuthenticationRequest() {
65+
return this.authenticationRequest;
66+
}
6567

6668
/**
67-
* Sets the {@link Authentication} object representing the failed authentication
69+
* Set the {@link Authentication} object representing the failed authentication
6870
* attempt.
6971
* <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.
72+
* The provided {@code authenticationRequest} should not be null
73+
* @param authenticationRequest the authentication request associated with the failed
74+
* authentication attempt
75+
* @since 6.5
7576
*/
76-
public void setAuthRequest(Authentication authRequest) {
77-
Assert.notNull(authRequest, "AuthRequest cannot be null");
78-
this.authRequest = authRequest;
77+
public void setAuthenticationRequest(Authentication authenticationRequest) {
78+
Assert.notNull(authenticationRequest, "authenticationRequest cannot be null");
79+
this.authenticationRequest = authenticationRequest;
7980
}
8081

8182
}

core/src/main/java/org/springframework/security/jackson2/BadCredentialsExceptionMixin.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
* @see CoreJackson2Module
4141
*/
4242
@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, include = JsonTypeInfo.As.PROPERTY)
43-
@JsonIgnoreProperties(ignoreUnknown = true, value = { "cause", "stackTrace" })
43+
@JsonIgnoreProperties(ignoreUnknown = true, value = { "cause", "stackTrace", "authenticationRequest" })
4444
class BadCredentialsExceptionMixin {
4545

4646
/**

etc/checkstyle/checkstyle-suppressions.xml

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
<suppress files="AbstractOAuth2AuthorizationGrantRequestEntityConverter\.java" checks="SpringMethodVisibility"/>
3939
<suppress files="JoseHeader\.java" checks="SpringMethodVisibility"/>
4040
<suppress files="DefaultLoginPageGeneratingFilterTests\.java" checks="SpringLeadingWhitespace"/>
41+
<suppress files="AuthenticationException\.java" checks="MutableException"/>
4142

4243
<!-- Lambdas that we can't replace with a method reference because a closure is required -->
4344
<suppress files="BearerTokenAuthenticationFilter\.java" checks="SpringLambda"/>

0 commit comments

Comments
 (0)