package com.zoniot.ccrc.security.mobile; import com.zoniot.ccrc.commom.utils.RedisUtil; import com.zoniot.ccrc.dao.SiteMapper; import com.zoniot.ccrc.dao.SiteUserMapper; import com.zoniot.ccrc.dao.UserMapper; import com.zoniot.ccrc.dto.LoginUser; import com.zoniot.ccrc.entity.Permission; import com.zoniot.ccrc.entity.Site; import com.zoniot.ccrc.entity.SiteUser; import com.zoniot.ccrc.entity.User; import com.zoniot.ccrc.service.IotService; import com.zoniot.ccrc.service.PermissionService; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.authentication.DisabledException; 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.Component; import javax.annotation.Resource; import java.util.List; @Component public class MobileLoginUserDetailService implements UserDetailsService { @Resource private UserMapper userMapper; @Resource private PermissionService permissionService; @Resource private SiteUserMapper siteUserMapper; @Resource private SiteMapper siteMapper; @Autowired private RedisUtil redisUtil; @Autowired private IotService iotService; @Override public UserDetails loadUserByUsername(String mobile) throws UsernameNotFoundException { User user = userMapper.findUserByMobile(mobile); if (user == null) { throw new UsernameNotFoundException("该用户不存在"); } //判断是否禁用 if (user.getEnable() == 0) { throw new DisabledException("账号已被禁用"); } LoginUser loginUser = new LoginUser(); BeanUtils.copyProperties(user, loginUser); //Map> permissionMap = new HashMap<>(); //Map> datePermissionMap = new HashMap<>(); if (user.getIsSuperAdmin() == 1) { //超级管理员的权限 List permissions = permissionService.getPermissionBySuperAdmin(); loginUser.setType(1); loginUser.setPermissionList(permissions); //查询物联网token String iotToken = iotService.getLoginIotToken("18800000001"); if (iotToken != null) { loginUser.setIotToken(iotToken); }else { throw new UsernameNotFoundException("物联网平台无该账号"); } } else { //站点管理员(多站点) Site site = siteUserMapper.getSiteByUserId(user.getId()); SiteUser siteUser = siteUserMapper.findBySiteIdAndUserId(site.getId(),user.getId()); if(siteUser.getType() == 2){ List permissions = permissionService.getPermissionBySiteAdminV2(site.getId(),site.getType());; loginUser.setPermissionList(permissions); }else { loginUser.setPermissionList(permissionService.getPermissionByUserId(user.getId(), site.getId())); } loginUser.setType(siteUser.getType()); loginUser.setRoleType(siteUser.getRoleType()); loginUser.setOrgId(siteUser.getOrganId()); loginUser.setSiteId(site.getId()); loginUser.setIsSiteAdmin(siteUser.getIsAdmin());//站点管理员 loginUser.setSite(site); loginUser.setCustomerId(site.getCustomerId()); loginUser.setCustomerName(site.getCustomerName()); loginUser.setOrgId(siteUser.getOrganId()); } return loginUser; } //判断siteList中是否包含siteId private boolean checkSite(List siteList, Integer siteId) { return siteList.parallelStream().anyMatch(site -> site.getId().equals(siteId)); } private Integer checkSiteType(List siteList, Integer siteId) { Integer siteType = null; for (Site site : siteList) { if(site.getId().equals(siteId)){ siteType = site.getType(); break; } } return siteType; } }