Newer
Older
SampleSpringSecurity3 / src / main / java / com / sample / service / SampleUserDetailsService.java
yhornisse on 30 May 2023 1 KB init
package com.sample.service;

import java.util.List;
import lombok.Builder;
import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

@Service
public class SampleUserDetailsService implements UserDetailsService {

  @Override
  public UserDetails loadUserByUsername(String accountId) throws UsernameNotFoundException {
    return "user".equals(accountId)
        ? CustomUser.builder()
        .isEnabled(true)
        .isAccountNonExpired(true)
        .isAccountNonLocked(true)
        .isCredentialsNonExpired(true)
        .username("user")
        .password("{noop}password")
        .build()
        : null;
  }

  @Builder
  @Data
  public static class CustomUser implements UserDetails {

    private List<GrantedAuthority> authorities;
    private String password;
    private String username;
    private boolean isAccountNonExpired;
    private boolean isAccountNonLocked;
    private boolean isCredentialsNonExpired;
    private boolean isEnabled;
  }
}