content: package com.cx.bank.mhg.bdemo.config;
  
  
  import com.cx.bank.mhg.bdemo.domain.TUser;
  import com.cx.bank.mhg.bdemo.service.UserService;
  import org.slf4j.Logger;
  import org.slf4j.LoggerFactory;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.security.core.GrantedAuthority;
  import org.springframework.security.core.authority.SimpleGrantedAuthority;
  import org.springframework.security.core.userdetails.User;
  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.Collection;
  import java.util.List;
  
  /**
   * 自定义userDetailsService
   */
  @Service("userDetailsService")
  public class CustomUserDetailsService implements UserDetailsService {
      @Autowired
      private UserService userService;
  
  
      private Logger logger = LoggerFactory.getLogger(CustomUserDetailsService.class);
  
      @Override
      public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
          Collection<GrantedAuthority> authorities = new ArrayList<>();
  
          // 从数据库中取出用户信息
          TUser user = userService.selectUserByName(username);
          logger.info(user.toString());
          // 判断用户是否存在
          if (user == null) {
              throw new UsernameNotFoundException("用户名不存在");
          }
  
          // 添加权限
          authorities.add(new SimpleGrantedAuthority("ROLE_USER"));
  
          logger.info(authorities.toString());
  
          return new User(user.getUserName(), user.getUserPassword(), authorities);
      }
  }