UserDetails class abstract interface

What the security code needs to know about one of your users to log them in: the username, the stored password hash, the authorities and whether the account may log in at all.

Your user store returns it from UserDetailsService.loadUserByUsername, and after a successful login it becomes the principal of the Authentication. Most applications build a User instead of implementing this interface. Implement it on your own account class when you want the principal to carry more, such as the account id or the tenant.

// The account entity itself serves as the logged-in principal.
class Account implements UserDetails {
  Account(this.id, this.email, this.passwordHash, this.authorities);

  final int id;
  final String email;
  final String passwordHash;

  @override
  String get username => email;

  @override
  String? get password => passwordHash;

  @override
  final List<GrantedAuthority> authorities;

  @override
  bool get isAccountNonExpired => true;

  @override
  bool get isAccountNonLocked => true;

  @override
  bool get isCredentialsNonExpired => true;

  @override
  bool get isEnabled => true;
}

Spring (Java): UserDetails; Java's getUsername() and isEnabled() are getters here.

Properties

authorities → List<GrantedAuthority>
The user's permissions, such as ROLE_ADMIN; they become the authorities of the Authentication.
no setter
hashCode → int
The hash code for this object.
no setterinherited
isAccountNonExpired → bool
false refuses every login with an AccountExpiredException, before the password is checked.
no setter
isAccountNonLocked → bool
false refuses every login with a LockedException, before the password is checked.
no setter
isCredentialsNonExpired → bool
false refuses the login with a CredentialsExpiredException, after the password matched.
no setter
isEnabled → bool
false refuses every login with a DisabledException, before the password is checked.
no setter
password → String?
The stored password hash, such as {argon2}$argon2id$...; null after the login erased it, and a null password never matches.
no setter
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited
username → String
The name the user logs in with; Authentication.name returns it.
no setter

Methods

noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() → String
A string representation of this object.
inherited

Operators

operator ==(Object other) → bool
The equality operator.
inherited