UserServiceImpl.java 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package edu.travel.service;
  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.mapper.EduTenantMapper;
  6. import org.apache.commons.lang3.StringUtils;
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.data.redis.core.RedisTemplate;
  9. import org.springframework.security.core.userdetails.User;
  10. import org.springframework.security.core.userdetails.UserDetails;
  11. import org.springframework.security.core.userdetails.UserDetailsService;
  12. import org.springframework.security.core.userdetails.UsernameNotFoundException;
  13. import org.springframework.stereotype.Service;
  14. import org.springframework.web.context.request.RequestContextHolder;
  15. import org.springframework.web.context.request.ServletRequestAttributes;
  16. import javax.servlet.http.HttpServletRequest;
  17. import java.util.ArrayList;
  18. @Service
  19. public class UserServiceImpl implements UserDetailsService {
  20. @Autowired
  21. private EduTenantMapper eduTenantMapper;
  22. @Autowired
  23. private RedisTemplate redisTemplate;
  24. @Override
  25. public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
  26. if (StringUtils.isBlank(username)) {
  27. throw new UsernameNotFoundException("username is empty");
  28. }
  29. EduTenant tenant = eduTenantMapper.selectOne(new LambdaQueryWrapper<EduTenant>().eq(EduTenant::getTenantPhone, username));
  30. if (tenant == null) {
  31. throw new UsernameNotFoundException("username not found");
  32. }
  33. HttpServletRequest request = ((ServletRequestAttributes) (RequestContextHolder.currentRequestAttributes())).getRequest();
  34. String code = request.getParameter("password");
  35. if (StringUtils.isBlank(code)) {
  36. throw new UsernameNotFoundException("验证码为空");
  37. }
  38. Object object = "1234";//redisTemplate.opsForValue().get("code_" + username);
  39. if (object == null) {
  40. throw new UsernameNotFoundException("验证码为空");
  41. }
  42. if (code.equals(object.toString())){
  43. redisTemplate.opsForValue().set("user_"+username+"_info", JSON.toJSONString(tenant));
  44. return new User(tenant.getTenantPhone(),code,new ArrayList<>());
  45. }
  46. throw new UsernameNotFoundException("验证码错误");
  47. }
  48. }