package com.sztzjy.trade.config.security; import cn.hutool.system.UserInfo; import com.sztzjy.trade.entity.StuUser; import com.sztzjy.trade.entity.StuUserExample; import com.sztzjy.trade.mapper.StuUserMapper; import io.jsonwebtoken.lang.Assert; 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.bcrypt.BCryptPasswordEncoder; import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.util.List; @Service public class AuthenticationService implements UserDetailsService { @Resource private StuUserMapper stuUserMapper; // @Override // public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { // StuUserExample stuUserExample = new StuUserExample(); // stuUserExample.createCriteria().andStudentIdEqualTo(username); // List list = stuUserMapper.selectByExample(stuUserExample); // Assert.notNull(list, "用户不存在"); // StuUser user = list.get(0); // JwtUser jwtUser = new JwtUser(); // jwtUser.setUsername(user.getStudentId()); // jwtUser.setPassword(new BCryptPasswordEncoder().encode(user.getPassword())); //不加密会报错Encoded password does not look like BCrypt // jwtUser.setUserId(user.getUserId()); // jwtUser.setName(user.getName()); // jwtUser.setRoleId(user.getRoleId()); // jwtUser.setStudentId(user.getStudentId()); // return jwtUser; // } @Override public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { StuUserExample stuUserExample = new StuUserExample(); stuUserExample.createCriteria().andStudentIdEqualTo(username); List list = stuUserMapper.selectByExample(stuUserExample); Assert.notNull(list, "用户不存在"); StuUser user = list.get(0); JwtUser jwtUser = new JwtUser(); jwtUser.setUsername(user.getStudentId()); jwtUser.setPassword(new BCryptPasswordEncoder().encode(user.getPassword())); //不加密会报错Encoded password does not look like BCrypt jwtUser.setUserId(user.getUserId()); jwtUser.setName(user.getName()); jwtUser.setRoleId(user.getRoleId()); jwtUser.setStudentId(user.getStudentId()); return jwtUser; } }