package com.ytpm.certify;

import com.ytpm.dao.YtPlatformUserMapper;
import com.ytpm.oauth.model.YtPlatformUser;
import org.springframework.security.core.authority.AuthorityUtils;
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 javax.annotation.Resource;
import java.util.Objects;

@Service
public class UserDetailService implements UserDetailsService {

    @Resource
    private YtPlatformUserMapper userMapper;

    @Override
    public UserDetails loadUserByUsername(String loginName) throws UsernameNotFoundException {
        //获取本地用户
        YtPlatformUser platformUser = userMapper.selectByLoginName(loginName);
        if(Objects.nonNull(platformUser)){
            //需要返回Oauth2中的认证对象，因此这里的username实际上是login-name
            //                    AuthorityUtils.createAuthorityList(systemUser.getRole())) ;
            return new User(platformUser.getUsername(),
                    platformUser.getPassword(),
//                    AuthorityUtils.createAuthorityList(systemUser.getRole())) ;
                    AuthorityUtils.createAuthorityList("ROLE_ADMIN"));
        }else{
            throw new UsernameNotFoundException("用户["+platformUser+"]不存在");
        }
    }
}
