UserDetailServiceImpl.java
1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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());
}
}