UserServiceImpl.java 2.7 KB

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