UserDetailServiceImpl.java 1.72 KB
package com.trash.garbage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.trash.garbage.global.GlobalStatus;
import com.trash.garbage.pojo.domain.GarUser;
import com.trash.garbage.security.UserCustom;
import com.trash.garbage.service.GarUserService;
import org.springframework.beans.factory.annotation.Autowired;
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.Component;

import java.util.Collections;
import java.util.Objects;

/**
 * 用户信息实现类
 * @author 20412
 */
@Component
public class UserDetailServiceImpl implements UserDetailsService {

    @Autowired
    private GarUserService userService;

    /** 查询数据库中的数据是否配对上 */
    @Override
    public UserDetails loadUserByUsername(String tel) throws UsernameNotFoundException {
        // 查表是否有对应的tel
        QueryWrapper<GarUser> qw = new QueryWrapper<>();
        qw.lambda().eq(GarUser::getGarUserTel, tel);
        GarUser user = userService.getOne(qw);
        if (Objects.isNull(user)) {
            // 不存在就创建用户
            GarUser nUser = new GarUser();
            nUser.setGarUserTel(tel);
            nUser.setGarUserDelFlag(GlobalStatus.DEL_FLAG_NO);
            nUser.setGarUserType("iphone");
            if (userService.save(nUser)) {
                return new UserCustom(nUser, Collections.emptyList());
            }
        }
        // 第二参数是控制权限  普通用户无需角色认证
        return new UserCustom(user, Collections.emptyList());
    }
}