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 authSet = getAuthSet(user.getId()); Collection 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); } }