package cn.reghao.autodop.dmaster.rbac.security;

import cn.reghao.autodop.dmaster.rbac.model.po.User;
import cn.reghao.autodop.dmaster.rbac.service.UserService;
import org.springframework.security.authentication.DisabledException;
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;

/**
 * 用户名密码认证
 *
 * @author reghao
 * @date 2019-05-20 11:03:56
 */
@Service
public class UserDetailsServiceImpl implements UserDetailsService {
    private UserService userService;

    public UserDetailsServiceImpl(UserService userService) {
        this.userService = userService;
    }

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User user = userService.getUser(username);
        if (user != null) {
            return user;
        } else {
            // Spring Security 会将 UsernameNotFoundException 捕获并替换，使得前端无法看到信息
            //throw new UsernameNotFoundException(email + " 未注册");
            throw new DisabledException(username + " 未注册");
        }
    }
}
