Skip to content

Commit da2d9aa

Browse files
committed
Add Username Property to Exception
Closes gh-17179
1 parent 4279040 commit da2d9aa

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

core/src/main/java/org/springframework/security/core/userdetails/UsernameNotFoundException.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,15 @@ public class UsernameNotFoundException extends AuthenticationException {
3131
@Serial
3232
private static final long serialVersionUID = 1410688585992297006L;
3333

34+
private final String name;
35+
3436
/**
3537
* Constructs a <code>UsernameNotFoundException</code> with the specified message.
3638
* @param msg the detail message.
3739
*/
3840
public UsernameNotFoundException(String msg) {
3941
super(msg);
42+
this.name = null;
4043
}
4144

4245
/**
@@ -47,6 +50,42 @@ public UsernameNotFoundException(String msg) {
4750
*/
4851
public UsernameNotFoundException(String msg, Throwable cause) {
4952
super(msg, cause);
53+
this.name = null;
54+
}
55+
56+
private UsernameNotFoundException(String msg, String name, Throwable cause) {
57+
super(msg, cause);
58+
this.name = name;
59+
}
60+
61+
/**
62+
* Construct an exception based on a specific username
63+
* @param username the invalid username
64+
* @return the {@link UsernameNotFoundException}
65+
* @since 7.0
66+
*/
67+
public static UsernameNotFoundException fromUsername(String username) {
68+
return fromUsername(username, null);
69+
}
70+
71+
/**
72+
* Construct an exception based on a specific username
73+
* @param username the invalid username
74+
* @param cause any underlying cause
75+
* @return the {@link UsernameNotFoundException}
76+
* @since 7.0
77+
*/
78+
public static UsernameNotFoundException fromUsername(String username, Throwable cause) {
79+
return new UsernameNotFoundException("user not found", username, cause);
80+
}
81+
82+
/**
83+
* Get the username that couldn't be found
84+
* @return the username
85+
* @since 7.0
86+
*/
87+
public String getName() {
88+
return this.name;
5089
}
5190

5291
}

0 commit comments

Comments
 (0)