package com.mindskip.xzs.configuration.spring.security; import com.mindskip.xzs.domain.enums.RoleEnum; import com.mindskip.xzs.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.core.GrantedAuthority; 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.Component; import java.util.ArrayList; /** * @version 2.2.0 * @description: 用户二次加载 * Copyright (C), 2020-2021, 武汉思维跳跃科技有限公司 * @date 2021 /9/7 9:45 */ @Component public class RestDetailsServiceImpl implements UserDetailsService { private final UserService userService; @Autowired public RestDetailsServiceImpl(UserService userService) { this.userService = userService; } @Override public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { com.mindskip.xzs.domain.User user = userService.getUserByUserName(username); if (user == null) { throw new UsernameNotFoundException("Username not found."); } ArrayList grantedAuthorities = new ArrayList<>(); grantedAuthorities.add(new SimpleGrantedAuthority(RoleEnum.fromCode(user.getRole()).getRoleName())); return new User(user.getUserName(), user.getPassword(), grantedAuthorities); } }