陶杰
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
package com.mzl.flower.service.impl;
 
import com.mzl.flower.dto.security.UserDTO;
import com.mzl.flower.entity.permission.AuthUser;
import com.mzl.flower.entity.system.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
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.UsernameNotFoundException;
import org.springframework.stereotype.Component;
 
import java.util.Collection;
import java.util.Set;
 
@Component
public class PhoneUserDetailsService extends BaseUserDetailsService {
    @Autowired
    private MessageSource messageSource;
 
    private final static String USER_NOT_FOUND = "error.login.telNotFound";
 
    public UserDetails loadUserByPhone(String tel, String userType) {
 
        User user = userService.findByTel(tel, userType);
        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);
    }
}