陶杰
2024-08-22 ee9032d9baf5f33e376d2d2699136e0a7b26bec7
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
77
78
79
80
81
82
83
84
85
86
87
package com.mzl.flower.service.impl;
 
import com.mzl.flower.dto.security.UserDTO;
import com.mzl.flower.entity.system.User;
import com.mzl.flower.service.system.UserService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
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.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
 
@Component
public class BaseUserDetailsService implements UserDetailsService {
 
    @Autowired
    @Lazy
    protected UserService userService;
 
    @Autowired
    protected MessageSource messageSource;
 
    private final static String USER_NOT_FOUND = "error.login.userNotFound";
 
    /**
     * 获取登录用户的权限信息(包含角色和菜单权限)
     *
     * @param userId
     * @return
     */
    protected Set<String> getAuthSet(String userId) {
        Set<String> authSet = new HashSet<>();
 
        //如果当前用户没有任何权限则添加默认角色
        if (CollectionUtils.isEmpty(authSet)) {
            authSet.add("ROLE_DEFAULT");
        }
 
        return authSet;
    }
 
    @Override
    public UserDetails loadUserByUsername(String id) throws UsernameNotFoundException {
        User user = userService.getUserById(id);
        Set<String> authSet = getAuthSet(user.getId());
 
        Collection<? extends GrantedAuthority> authorities
                = AuthorityUtils.createAuthorityList(authSet.toArray(new String[0]));
        String password = user.getPassword();
        if(StringUtils.isEmpty(password)){
            password = "87654321";
        }
        return new UserDTO(user.getId(), user.getNickName(),user.getType(), user.getLoginName(), password,
                true, true, true, true, authorities);
    }
 
    public UserDetails loadUserByUsername(String username, List<String> userTypes) throws UsernameNotFoundException {
        String loginName = username;
 
        User user = findUser(loginName, userTypes);
        if (user == null) {
            throw new UsernameNotFoundException(messageSource.getMessage(USER_NOT_FOUND, null, null, LocaleContextHolder.getLocale()));
        }
 
        Set<String> authSet = getAuthSet(user.getId());
 
        Collection<? extends GrantedAuthority> authorities
                = AuthorityUtils.createAuthorityList(authSet.toArray(new String[0]));
        return new UserDTO(user.getId(), user.getNickName(),user.getType(), user.getLoginName(), user.getPassword(),
                true, true, true, true, authorities);
    }
 
    protected User findUser(String key, List<String> userTypes){
        return userService.findByLoginName(key, userTypes);
    }
}