| 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); | 
|     } | 
| } |