12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- package edu.travel.service.impl;
- import com.alibaba.fastjson.JSON;
- import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
- import edu.travel.entity.EduTenant;
- import edu.travel.entity.SysRole;
- import edu.travel.mapper.EduTenantMapper;
- import edu.travel.service.SysRoleService;
- import org.apache.commons.lang3.StringUtils;
- import org.springframework.beans.BeanUtils;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.data.redis.core.RedisTemplate;
- import org.springframework.security.core.GrantedAuthority;
- import org.springframework.security.core.userdetails.User;
- import org.springframework.security.core.userdetails.UserDetails;
- import org.springframework.security.core.userdetails.UserDetailsService;
- import org.springframework.security.core.userdetails.UsernameNotFoundException;
- import org.springframework.stereotype.Service;
- import org.springframework.web.context.request.RequestContextHolder;
- import org.springframework.web.context.request.ServletRequestAttributes;
- import javax.servlet.http.HttpServletRequest;
- import java.util.ArrayList;
- import java.util.List;
- @Service
- public class UserServiceImpl implements UserDetailsService {
- @Autowired
- private EduTenantMapper eduTenantMapper;
- @Autowired
- private RedisTemplate redisTemplate;
- @Override
- public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
- if (StringUtils.isBlank(username)) {
- throw new UsernameNotFoundException("username is empty");
- }
- HttpServletRequest request = ((ServletRequestAttributes) (RequestContextHolder.currentRequestAttributes())).getRequest();
- EduTenant tenant = eduTenantMapper.selectOne(new LambdaQueryWrapper<EduTenant>().eq(EduTenant::getTenantPhone, username));
- if (tenant == null) {
- throw new UsernameNotFoundException("username not found");
- }
- if ("admin".equals(request.getParameter("loginFrom"))) {
- return new User(tenant.getTenantPhone(),null,new ArrayList<>());
- }else {
- String code = request.getParameter("password");
- if (StringUtils.isBlank(code)) {
- throw new UsernameNotFoundException("验证码为空");
- }
- Object object = "1234";//redisTemplate.opsForValue().get(username+"_user_sms");
- if (object == null) {
- throw new UsernameNotFoundException("验证码为空");
- }
- if (code.equals(object.toString())){
- redisTemplate.opsForValue().set(username+"_info", JSON.toJSONString(tenant));
- return new User(tenant.getTenantPhone(),code, new ArrayList<>());
- }
- throw new UsernameNotFoundException("验证码错误");
- }
- }
- }
|