郑永安
2023-06-19 59e91a4e9ddaf23cebb12993c774aa899ab22d16
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
package com.gk.firework.Config.Oauth2;
 
import com.gk.firework.Domain.UserInfo;
import com.gk.firework.Domain.Utils.CommonUtil;
import com.gk.firework.Domain.Utils.Constants;
import com.gk.firework.Service.UserService;
import io.swagger.models.auth.In;
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.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;
 
    /**
     * 根据用户名查询用户
     */
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        String authType = System.getProperty(Constants.AUTH_TYPE);
        OauthUser oauthUser = new OauthUser();
        //查询登录用户
        UserInfo userInfo = userService.selectByLoginname(username);
        Optional.of(userInfo).ifPresent(us ->{
            //查询角色
            List<OauthRole> roleByUser = userService.selectRoleByUser(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.getUsername());
            oauthUser.setOauthRoles(oauthRoles);
        });
        //判断登录入口
        return oauthUser;
    }
}