Browse Source

Add Username Property to Exception

Closes gh-17179
pull/16953/head
Josh Cummings 8 months ago
parent
commit
da2d9aa868
No known key found for this signature in database
GPG Key ID: 869B37A20E876129
  1. 39
      core/src/main/java/org/springframework/security/core/userdetails/UsernameNotFoundException.java

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

@ -31,12 +31,15 @@ public class UsernameNotFoundException extends AuthenticationException { @@ -31,12 +31,15 @@ public class UsernameNotFoundException extends AuthenticationException {
@Serial
private static final long serialVersionUID = 1410688585992297006L;
private final String name;
/**
* Constructs a <code>UsernameNotFoundException</code> with the specified message.
* @param msg the detail message.
*/
public UsernameNotFoundException(String msg) {
super(msg);
this.name = null;
}
/**
@ -47,6 +50,42 @@ public class UsernameNotFoundException extends AuthenticationException { @@ -47,6 +50,42 @@ public class UsernameNotFoundException extends AuthenticationException {
*/
public UsernameNotFoundException(String msg, Throwable cause) {
super(msg, cause);
this.name = null;
}
private UsernameNotFoundException(String msg, String name, Throwable cause) {
super(msg, cause);
this.name = name;
}
/**
* Construct an exception based on a specific username
* @param username the invalid username
* @return the {@link UsernameNotFoundException}
* @since 7.0
*/
public static UsernameNotFoundException fromUsername(String username) {
return fromUsername(username, null);
}
/**
* Construct an exception based on a specific username
* @param username the invalid username
* @param cause any underlying cause
* @return the {@link UsernameNotFoundException}
* @since 7.0
*/
public static UsernameNotFoundException fromUsername(String username, Throwable cause) {
return new UsernameNotFoundException("user not found", username, cause);
}
/**
* Get the username that couldn't be found
* @return the username
* @since 7.0
*/
public String getName() {
return this.name;
}
}

Loading…
Cancel
Save