郑永安
2023-09-19 69185134fcfaf913ea45f1255677225a2cc311a4
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package com.gk.hotwork.Config.Oauth2;
 
import com.gk.hotwork.Domain.DepartmentInfo;
import com.gk.hotwork.Domain.UserInfo;
import com.gk.hotwork.Domain.Utils.CommonUtil;
import com.gk.hotwork.Domain.Utils.Constants;
import com.gk.hotwork.Service.DepartmentService;
import com.gk.hotwork.Service.RoleService;
import com.gk.hotwork.Service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
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.List;
import java.util.Optional;
 
/**
 * 加载用户特定数据的核心接口(Core interface which loads user-specific data.)
 *
 * @author zhangby
 * @date 2019-05-14 09:49
 */
@Service
public class UserServiceDetail implements UserDetailsService {
 
    @Autowired
    UserService userService;
    @Autowired
    RoleService roleService;
    @Autowired
    DepartmentService departmentService;
 
    @Value("${workname}")
    private String workname;
    /**
     * 根据用户名查询用户
     */
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        String authType = System.getProperty(Constants.AUTH_TYPE);
        OauthUser oauthUser = new OauthUser();
        //查询登录用户
        UserInfo userInfo = userService.selectUserVoByName(workname,username);
        Optional.of(userInfo).ifPresent(us ->{
            //查询角色
            List<OauthRole> roleByUser = roleService.selectOauthRoleByUser(us.getId().intValue());
            //数据类型转换
            List<OauthRole> oauthRoles = CommonUtil
                    .convers(roleByUser, role -> new OauthRole(
                            role.getId(),
                            role.getAuthority(),null,null)
                    );
            //数据结果集封装
            oauthUser.setId(us.getId().toString());
            oauthUser.setPassword(us.getPassword());
            oauthUser.setUsername(us.getRealname());
            oauthUser.setOauthRoles(oauthRoles);
            oauthUser.setDepartment(us.getDepartment());
            if (us.getDepartment() != null) {
                DepartmentInfo dep = departmentService.getById(us.getId());
                if (dep != null) {
                    oauthUser.setDepartmentName(dep.getDepartment());
                }
 
            }
            oauthUser.setCompany(us.getCompany());
            oauthUser.setUserType(us.getType());
            oauthUser.setIdcard(us.getIdcard());
        });
        //判断登录入口
        return oauthUser;
    }
}