UserServiceImpl.java 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. package edu.travel.service.impl;
  2. import cn.hutool.core.bean.BeanUtil;
  3. import com.alibaba.fastjson.JSON;
  4. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  5. import edu.travel.entity.EduTenant;
  6. import edu.travel.entity.SysRole;
  7. import edu.travel.mapper.EduTenantMapper;
  8. import edu.travel.service.SysRoleService;
  9. import org.apache.commons.lang3.StringUtils;
  10. import org.springframework.beans.BeanUtils;
  11. import org.springframework.beans.factory.annotation.Autowired;
  12. import org.springframework.data.redis.core.RedisTemplate;
  13. import org.springframework.security.core.GrantedAuthority;
  14. import org.springframework.security.core.userdetails.User;
  15. import org.springframework.security.core.userdetails.UserDetails;
  16. import org.springframework.security.core.userdetails.UserDetailsService;
  17. import org.springframework.security.core.userdetails.UsernameNotFoundException;
  18. import org.springframework.stereotype.Service;
  19. import org.springframework.web.context.request.RequestContextHolder;
  20. import org.springframework.web.context.request.ServletRequestAttributes;
  21. import javax.servlet.http.HttpServletRequest;
  22. import java.util.ArrayList;
  23. import java.util.List;
  24. import java.util.Set;
  25. import java.util.stream.Collectors;
  26. @Service
  27. public class UserServiceImpl implements UserDetailsService {
  28. @Autowired
  29. private EduTenantMapper eduTenantMapper;
  30. @Autowired
  31. private RedisTemplate redisTemplate;
  32. @Autowired
  33. private SysRoleService sysRoleService;
  34. @Override
  35. public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
  36. if (StringUtils.isBlank(username)) {
  37. throw new UsernameNotFoundException("username is empty");
  38. }
  39. HttpServletRequest request = ((ServletRequestAttributes) (RequestContextHolder.currentRequestAttributes())).getRequest();
  40. EduTenant tenant = eduTenantMapper.selectOne(new LambdaQueryWrapper<EduTenant>().eq(EduTenant::getTenantPhone, username));
  41. if (tenant == null) {
  42. throw new UsernameNotFoundException("username not found");
  43. }
  44. if ("admin".equals(request.getParameter("loginFrom"))) {
  45. redisTemplate.opsForValue().set(username+"_info", JSON.toJSONString(tenant));
  46. }else {
  47. String code = request.getParameter("password");
  48. if (StringUtils.isBlank(code)) {
  49. throw new UsernameNotFoundException("验证码为空");
  50. }
  51. Object object = "1234";//redisTemplate.opsForValue().get(username+"_user_sms");
  52. if (object == null) {
  53. throw new UsernameNotFoundException("验证码为空");
  54. }
  55. if (code.equals(object.toString())){
  56. redisTemplate.opsForValue().set(username+"_info", JSON.toJSONString(tenant));
  57. return new User(tenant.getTenantPhone(),code,new ArrayList<>() );
  58. }
  59. throw new UsernameNotFoundException("验证码错误");
  60. }
  61. List<SysRole> roleList = sysRoleService.getRoleListByUserId(tenant.getId());
  62. request.setAttribute("authorities", roleList);
  63. Set<String> collect = roleList.stream().map(SysRole::getName).collect(Collectors.toSet());
  64. String[] arr = new String[collect.size()];
  65. String[] array = collect.toArray(arr);
  66. return User.builder().username(tenant.getTenantPhone()).password(tenant.getPassword()).roles(array).build();
  67. }
  68. }