lizijie
2020-12-03 a66d53c3cbfb0024804045f4d795be06089d4f9d
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
package com.moral.service.impl;
 
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
 
import javax.annotation.Resource;
 
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
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 com.moral.common.util.RedisUtil;
import com.moral.entity.Account;
import com.moral.entity.OperateUser;
import com.moral.mapper.AccountMapper;
import com.moral.service.AccountService;
import com.moral.service.OperateUserService;
 
@Service
public class AuthUserServiceImpl implements UserDetailsService {
 
    @Resource
    private AccountMapper accountMapper;
 
    @Resource
    private AccountService accountService;
 
    @Resource
    private OperateUserService operateUserService;
 
    @Resource
    private RedisTemplate<String, String> redisTemplate;
 
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        String type = RedisUtil.get(redisTemplate, "token_" + username), password;
        Collection<SimpleGrantedAuthority> collection = new HashSet<SimpleGrantedAuthority>();
        if ("screen".equals(type)) {
            Account account = accountService.getAccountByAccountName(username);
            password = account.getPassword();
            List<Map<String, Object>> roleNames = accountMapper.getRoleNameByAccountId(account.getId());
            for (Map<String, Object> roleName : roleNames) {
                collection.add(new SimpleGrantedAuthority((String) roleName.get("role_name")));
            }
        } else {
            OperateUser operateUser = operateUserService.getOperateUserByMobile(username);
            password = operateUser.getPassword();
            collection.add((new SimpleGrantedAuthority("ROLE_MOBILE")));
        }
        return new User(username, password, collection);
    }
}