package com.rzyc.config; import com.rzyc.mapper.ent.EntUserMapper; import com.rzyc.model.ent.EntUser; 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.UserDetails; import org.springframework.security.core.userdetails.UserDetailsService; import org.springframework.security.core.userdetails.UsernameNotFoundException; import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.stereotype.Service; import java.util.ArrayList; import java.util.List; import java.util.Objects; /** * spring security 用户认证过程 * @author Xuwanxin * @author 2022/09/27 * */ @Service("userService") public class UserDetailsServiceImpl implements UserDetailsService { /** * 内存过程密码加密 * */ private PasswordEncoder passwordEncoder; /** * 企业端用户 * */ private EntUserMapper entUserMapper; @Autowired public void UserDetailsServiceImplFinder(PasswordEncoder passwordEncoder,EntUserMapper entUserMapper) { this.passwordEncoder = passwordEncoder; this.entUserMapper = entUserMapper; } @Override public UserDetails loadUserByUsername(String name) throws UsernameNotFoundException { //判断数据库用户 EntUser entUser = entUserMapper.selectByName(name); if (Objects.isNull(entUser)){ throw new UsernameNotFoundException("用户名或密码错误"); } // 获取用户权限 List authority= new ArrayList(); //给通过登陆的进行role权限,也可以根据业务调整 authority.add(new SimpleGrantedAuthority("ROLE_ADMIN")); return new EntUserDetails(entUser.getName(), passwordEncoder.encode(entUser.getPasswd()), authority,entUser.getEntUserId()); } }