content: package com.spring4all.config;
  
  import com.spring4all.entity.UserDO;
  import com.spring4all.service.UserService;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.security.core.authority.SimpleGrantedAuthority;
  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;
  
  import java.util.ArrayList;
  import java.util.List;
  
  @Service
  public class DbUserDetailsService implements UserDetailsService {
  
      private final UserService userService;
  
      @Autowired
      DbUserDetailsService(UserService userService){
          this.userService = userService;
      }
  
      @Override
      public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
          UserDO userDO = userService.getByUsername(username);
          if (userDO == null){
              throw new UsernameNotFoundException("用户不存在！");
          }
          List<SimpleGrantedAuthority> simpleGrantedAuthorities = new ArrayList<>();
          simpleGrantedAuthorities.add(new SimpleGrantedAuthority("USER"));
          return new org.springframework.security.core.userdetails.User(userDO.getUsername(), userDO.getPassword(), simpleGrantedAuthorities);
      }
  
  }
  