新增:
1.用户登录时判断租户是否过期,在登录时赋予 token有效期为当前租户到期时间和当前日期的差值; 2.实现租户名查 租户 Id 接口
This commit is contained in:
@@ -15,8 +15,10 @@ public enum ErrorCode {
|
||||
USER_DISABLE(40300, "用户被禁用"),
|
||||
TOKEN_INVALID(40400, "Token无效,请重新登录"),
|
||||
NO_AUTH_ERROR(40101, "无权限"),
|
||||
PACKAGE_EXPIRED(40500, "套餐已到期"),
|
||||
NOT_FOUND_ERROR(40400, "请求数据不存在"),
|
||||
FORBIDDEN_ERROR(40300, "禁止访问"),
|
||||
TENANT_NAME_NOT_EXISTS(40600, "租户不存在"),
|
||||
SYSTEM_ERROR(50000, "系统内部异常"),
|
||||
OPERATION_ERROR(50001, "操作失败"),
|
||||
QUEUE_ERROR(60001, "队列消息添加失败"),
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
package com.yupi.springbootinit.controller;
|
||||
|
||||
import com.yupi.springbootinit.common.BaseResponse;
|
||||
import com.yupi.springbootinit.common.ErrorCode;
|
||||
import com.yupi.springbootinit.common.ResultUtils;
|
||||
import com.yupi.springbootinit.exception.BusinessException;
|
||||
import com.yupi.springbootinit.model.vo.user.SystemUsersVO;
|
||||
import com.yupi.springbootinit.service.SystemTenantService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
/*
|
||||
* @author: ziin
|
||||
* @date: 2025/6/20 15:18
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/tenant")
|
||||
@Slf4j
|
||||
@CrossOrigin
|
||||
public class TenantController {
|
||||
|
||||
@Resource
|
||||
private SystemTenantService systemTenantService;
|
||||
|
||||
@GetMapping("/get-id-by-name")
|
||||
public BaseResponse<Long> getTenantIdByName(@RequestParam("name") String name) {
|
||||
if (name == null) {
|
||||
throw new BusinessException(ErrorCode.PARAMS_ERROR);
|
||||
}
|
||||
return ResultUtils.success( systemTenantService.getTenantIdByName(name));
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -34,7 +34,7 @@ public class UserController {
|
||||
// 用户登陆接口
|
||||
@PostMapping("doLogin")
|
||||
public BaseResponse<SystemUsersVO> doLogin(@RequestBody SystemUsersDTO usersDTO) {
|
||||
SystemUsers user = usersService.getUserByUserName(usersDTO.getUsername());
|
||||
SystemUsers user = usersService.getUserByUserName(usersDTO.getUsername(),usersDTO.getTenantId());
|
||||
if (user == null) {
|
||||
throw new BusinessException(ErrorCode.USERNAME_OR_PASSWORD_ERROR);
|
||||
}
|
||||
@@ -46,9 +46,16 @@ public class UserController {
|
||||
if (CommonStatusEnum.isDisable(Integer.valueOf(user.getStatus()))) {
|
||||
throw new BusinessException(ErrorCode.USER_DISABLE);
|
||||
}
|
||||
if (usersService.isExpired(usersDTO.getTenantId())){
|
||||
throw new BusinessException(ErrorCode.PACKAGE_EXPIRED);
|
||||
}
|
||||
Long second = usersService.getTenantExpiredTime(usersDTO.getTenantId());
|
||||
SystemUsersVO systemUsersVO = new SystemUsersVO();
|
||||
BeanUtil.copyProperties(user, systemUsersVO);
|
||||
// 赋予用户 Id
|
||||
StpUtil.login(user.getId());
|
||||
// 设置 token 有效期为当前日期和套餐有效期的差值
|
||||
StpUtil.renewTimeout(second);
|
||||
systemUsersVO.setTokenName(StpUtil.getTokenName());
|
||||
systemUsersVO.setTokenValue(StpUtil.getTokenValue());
|
||||
return ResultUtils.success(systemUsersVO);
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
package com.yupi.springbootinit.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.yupi.springbootinit.model.entity.SystemTenant;
|
||||
|
||||
/*
|
||||
* @author: ziin
|
||||
* @date: 2025/6/20 14:50
|
||||
*/
|
||||
|
||||
public interface SystemTenantMapper extends BaseMapper<SystemTenant> {
|
||||
}
|
||||
@@ -0,0 +1,128 @@
|
||||
package com.yupi.springbootinit.model.entity;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.IdType;
|
||||
import com.baomidou.mybatisplus.annotation.TableField;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import io.swagger.annotations.ApiModel;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import java.util.Date;
|
||||
import lombok.Data;
|
||||
|
||||
/*
|
||||
* @author: ziin
|
||||
* @date: 2025/6/20 14:50
|
||||
*/
|
||||
|
||||
/**
|
||||
* 租户表
|
||||
*/
|
||||
@ApiModel(description="租户表")
|
||||
@Data
|
||||
@TableName(value = "system_tenant")
|
||||
public class SystemTenant {
|
||||
/**
|
||||
* 租户编号
|
||||
*/
|
||||
@TableId(value = "id", type = IdType.AUTO)
|
||||
@ApiModelProperty(value="租户编号")
|
||||
private Long id;
|
||||
|
||||
/**
|
||||
* 租户名
|
||||
*/
|
||||
@TableField(value = "`name`")
|
||||
@ApiModelProperty(value="租户名")
|
||||
private String name;
|
||||
|
||||
/**
|
||||
* 联系人的用户编号
|
||||
*/
|
||||
@TableField(value = "contact_user_id")
|
||||
@ApiModelProperty(value="联系人的用户编号")
|
||||
private Long contactUserId;
|
||||
|
||||
/**
|
||||
* 联系人
|
||||
*/
|
||||
@TableField(value = "contact_name")
|
||||
@ApiModelProperty(value="联系人")
|
||||
private String contactName;
|
||||
|
||||
/**
|
||||
* 联系手机
|
||||
*/
|
||||
@TableField(value = "contact_mobile")
|
||||
@ApiModelProperty(value="联系手机")
|
||||
private String contactMobile;
|
||||
|
||||
/**
|
||||
* 租户状态(0正常 1停用)
|
||||
*/
|
||||
@TableField(value = "`status`")
|
||||
@ApiModelProperty(value="租户状态(0正常 1停用)")
|
||||
private Byte status;
|
||||
|
||||
/**
|
||||
* 绑定域名
|
||||
*/
|
||||
@TableField(value = "website")
|
||||
@ApiModelProperty(value="绑定域名")
|
||||
private String website;
|
||||
|
||||
/**
|
||||
* 租户套餐编号
|
||||
*/
|
||||
@TableField(value = "package_id")
|
||||
@ApiModelProperty(value="租户套餐编号")
|
||||
private Long packageId;
|
||||
|
||||
/**
|
||||
* 过期时间
|
||||
*/
|
||||
@TableField(value = "expire_time")
|
||||
@ApiModelProperty(value="过期时间")
|
||||
private Date expireTime;
|
||||
|
||||
/**
|
||||
* 账号数量
|
||||
*/
|
||||
@TableField(value = "account_count")
|
||||
@ApiModelProperty(value="账号数量")
|
||||
private Integer accountCount;
|
||||
|
||||
/**
|
||||
* 创建者
|
||||
*/
|
||||
@TableField(value = "creator")
|
||||
@ApiModelProperty(value="创建者")
|
||||
private String creator;
|
||||
|
||||
/**
|
||||
* 创建时间
|
||||
*/
|
||||
@TableField(value = "create_time")
|
||||
@ApiModelProperty(value="创建时间")
|
||||
private Date createTime;
|
||||
|
||||
/**
|
||||
* 更新者
|
||||
*/
|
||||
@TableField(value = "updater")
|
||||
@ApiModelProperty(value="更新者")
|
||||
private String updater;
|
||||
|
||||
/**
|
||||
* 更新时间
|
||||
*/
|
||||
@TableField(value = "update_time")
|
||||
@ApiModelProperty(value="更新时间")
|
||||
private Date updateTime;
|
||||
|
||||
/**
|
||||
* 是否删除
|
||||
*/
|
||||
@TableField(value = "deleted")
|
||||
@ApiModelProperty(value="是否删除")
|
||||
private Boolean deleted;
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.yupi.springbootinit.service;
|
||||
|
||||
import com.yupi.springbootinit.common.BaseResponse;
|
||||
import com.yupi.springbootinit.model.entity.SystemTenant;
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
/*
|
||||
* @author: ziin
|
||||
* @date: 2025/6/20 14:50
|
||||
*/
|
||||
|
||||
public interface SystemTenantService extends IService<SystemTenant>{
|
||||
|
||||
|
||||
Long getTenantIdByName(String name);
|
||||
}
|
||||
@@ -10,7 +10,11 @@ import com.yupi.springbootinit.model.entity.SystemUsers;
|
||||
|
||||
public interface SystemUsersService extends IService<SystemUsers> {
|
||||
|
||||
SystemUsers getUserByUserName(String username);
|
||||
SystemUsers getUserByUserName(String username,Long tenantId);
|
||||
|
||||
boolean isPasswordMatch(String rawPassWord, String encodedPassword);
|
||||
|
||||
boolean isExpired(Long tendId);
|
||||
|
||||
Long getTenantExpiredTime(Long tenantId);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
package com.yupi.springbootinit.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.yupi.springbootinit.common.BaseResponse;
|
||||
import com.yupi.springbootinit.common.ErrorCode;
|
||||
import com.yupi.springbootinit.exception.BusinessException;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import java.util.List;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.yupi.springbootinit.model.entity.SystemTenant;
|
||||
import com.yupi.springbootinit.mapper.SystemTenantMapper;
|
||||
import com.yupi.springbootinit.service.SystemTenantService;
|
||||
/*
|
||||
* @author: ziin
|
||||
* @date: 2025/6/20 14:50
|
||||
*/
|
||||
|
||||
@Service
|
||||
public class SystemTenantServiceImpl extends ServiceImpl<SystemTenantMapper, SystemTenant> implements SystemTenantService{
|
||||
|
||||
@Override
|
||||
public Long getTenantIdByName(String name) {
|
||||
QueryWrapper<SystemTenant> queryWrapper = new QueryWrapper<>();
|
||||
queryWrapper.eq("name",name);
|
||||
SystemTenant systemTenant = baseMapper.selectOne(queryWrapper);
|
||||
if (systemTenant == null){
|
||||
throw new BusinessException(ErrorCode.TENANT_NAME_NOT_EXISTS);
|
||||
}
|
||||
return systemTenant.getId();
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,14 @@
|
||||
package com.yupi.springbootinit.service.impl;
|
||||
|
||||
import cn.hutool.core.date.DateUnit;
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import cn.hutool.crypto.SecureUtil;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.yupi.springbootinit.mapper.SystemTenantMapper;
|
||||
import com.yupi.springbootinit.model.entity.SystemTenant;
|
||||
import com.yupi.springbootinit.model.entity.SystemUsers;
|
||||
import com.yupi.springbootinit.service.SystemTenantService;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@@ -24,10 +29,14 @@ public class SystemUsersServiceImpl extends ServiceImpl<SystemUsersMapper,System
|
||||
@Value("${md5.salt}")
|
||||
private String MD5_SALT;
|
||||
|
||||
@Resource
|
||||
private SystemTenantMapper systemTenantMapper;
|
||||
|
||||
@Override
|
||||
public SystemUsers getUserByUserName(String username) {
|
||||
public SystemUsers getUserByUserName(String username,Long tenantId) {
|
||||
QueryWrapper <SystemUsers> queryWrapper = new QueryWrapper<>();
|
||||
queryWrapper.eq("username",username);
|
||||
queryWrapper.eq("tenant_id",tenantId);
|
||||
return baseMapper.selectOne(queryWrapper);
|
||||
}
|
||||
|
||||
@@ -36,4 +45,22 @@ public class SystemUsersServiceImpl extends ServiceImpl<SystemUsersMapper,System
|
||||
String s = SecureUtil.md5(MD5_SALT + rawPassword);
|
||||
return s.equals(encodedPassword);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isExpired(Long tendId) {
|
||||
SystemTenant systemTenant = systemTenantMapper.selectById(tendId);
|
||||
long between = DateUtil.between(systemTenant.getExpireTime(), DateUtil.date(), DateUnit.DAY);
|
||||
return between < 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long getTenantExpiredTime(Long tenantId) {
|
||||
SystemTenant systemTenant = systemTenantMapper.selectById(tenantId);
|
||||
long between = DateUtil.between(systemTenant.getExpireTime(), DateUtil.date(), DateUnit.SECOND);
|
||||
if (between > 0 ) {
|
||||
return between;
|
||||
}
|
||||
return 0L;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
28
src/main/resources/mapper/SystemTenantMapper.xml
Normal file
28
src/main/resources/mapper/SystemTenantMapper.xml
Normal file
@@ -0,0 +1,28 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="com.yupi.springbootinit.mapper.SystemTenantMapper">
|
||||
<resultMap id="BaseResultMap" type="com.yupi.springbootinit.model.entity.SystemTenant">
|
||||
<!--@mbg.generated-->
|
||||
<!--@Table system_tenant-->
|
||||
<id column="id" jdbcType="BIGINT" property="id" />
|
||||
<result column="name" jdbcType="VARCHAR" property="name" />
|
||||
<result column="contact_user_id" jdbcType="BIGINT" property="contactUserId" />
|
||||
<result column="contact_name" jdbcType="VARCHAR" property="contactName" />
|
||||
<result column="contact_mobile" jdbcType="VARCHAR" property="contactMobile" />
|
||||
<result column="status" jdbcType="TINYINT" property="status" />
|
||||
<result column="website" jdbcType="VARCHAR" property="website" />
|
||||
<result column="package_id" jdbcType="BIGINT" property="packageId" />
|
||||
<result column="expire_time" jdbcType="TIMESTAMP" property="expireTime" />
|
||||
<result column="account_count" jdbcType="INTEGER" property="accountCount" />
|
||||
<result column="creator" jdbcType="VARCHAR" property="creator" />
|
||||
<result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
|
||||
<result column="updater" jdbcType="VARCHAR" property="updater" />
|
||||
<result column="update_time" jdbcType="TIMESTAMP" property="updateTime" />
|
||||
<result column="deleted" jdbcType="BIT" property="deleted" />
|
||||
</resultMap>
|
||||
<sql id="Base_Column_List">
|
||||
<!--@mbg.generated-->
|
||||
id, `name`, contact_user_id, contact_name, contact_mobile, `status`, website, package_id,
|
||||
expire_time, account_count, creator, create_time, updater, update_time, deleted
|
||||
</sql>
|
||||
</mapper>
|
||||
Reference in New Issue
Block a user