admin验证

This commit is contained in:
2025-07-18 16:38:40 +08:00
parent f54595466a
commit b3df268964
6 changed files with 38 additions and 2 deletions

View File

@@ -9,8 +9,10 @@ import com.ycwl.basic.exception.CheckTokenException;
import com.ycwl.basic.exception.MissTokenException;
import com.ycwl.basic.exception.PermissionException;
import com.ycwl.basic.exception.TokenExpireException;
import com.ycwl.basic.mapper.AdminUserMapper;
import com.ycwl.basic.mapper.ScenicAccountMapper;
import com.ycwl.basic.model.jwt.JwtInfo;
import com.ycwl.basic.model.pc.adminUser.entity.LoginEntity;
import com.ycwl.basic.model.pc.scenic.entity.ScenicAccountEntity;
import com.ycwl.basic.utils.JwtTokenUtil;
import lombok.extern.slf4j.Slf4j;
@@ -31,6 +33,7 @@ import java.time.ZoneId;
import java.time.ZoneOffset;
import java.util.List;
import static com.ycwl.basic.constant.JwtRoleConstant.ADMIN;
import static com.ycwl.basic.constant.JwtRoleConstant.MERCHANT;
@Slf4j
@@ -41,6 +44,8 @@ public class AuthInterceptor implements HandlerInterceptor {
RedisTemplate redisTemplate;
@Autowired
private ScenicAccountMapper scenicAccountMapper;
@Autowired
private AdminUserMapper adminUserMapper;
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
@@ -86,6 +91,14 @@ public class AuthInterceptor implements HandlerInterceptor {
throw new TokenExpireException("token过期");
}
}
if (StringUtils.equals(jwtInfo.getRoleId(), ADMIN.type)) {
Long adminId = jwtInfo.getUserId();
LoginEntity account = adminUserMapper.getById(adminId);
LocalDateTime expireTime = jwtInfo.getExpireTime();
if (account.getUpdateAt().toInstant().getEpochSecond() != expireTime.atZone(ZoneId.systemDefault()).toEpochSecond()) {
throw new TokenExpireException("token过期");
}
}
BaseContextHandler.setToken(token);
BaseContextHandler.setName(jwtInfo.getName());
BaseContextHandler.setUserId(String.valueOf(jwtInfo.getUserId()));

View File

@@ -25,4 +25,6 @@ public interface AdminUserMapper {
int updatePassword(UpdatePasswordReqVO updatePasswordReqVO);
String getPasswordByAccount(@Param("id")String id);
LoginEntity getById(Long id);
}

View File

@@ -2,6 +2,8 @@ package com.ycwl.basic.model.pc.adminUser.entity;
import lombok.Data;
import java.util.Date;
@Data
public class LoginEntity {
private Long staffId;
@@ -10,4 +12,5 @@ public class LoginEntity {
private String password;
private String roleId;
private String typeName;
private Date updateAt;
}

View File

@@ -23,9 +23,11 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.List;
import static com.ycwl.basic.constant.JwtRoleConstant.ADMIN;
import static com.ycwl.basic.constant.PermissionConstant.ROLE_STATUS;
@@ -130,7 +132,7 @@ public class AdminUserServiceImpl implements AdminUserService {
}
}
LoginRespVO loginRespVO = new LoginRespVO();
String token = jwtTokenUtil.generateToken(new JwtInfo(login.getStaffName(), login.getStaffId(), roleId, login.getAccount(), login.getAccount(), null));
String token = jwtTokenUtil.generateToken(new JwtInfo(login.getStaffName(), login.getStaffId(), ADMIN.type, login.getAccount(), login.getAccount(), null), login.getUpdateAt());
loginRespVO.setToken(token);
loginRespVO.setName(login.getStaffName());
loginRespVO.setTypeName(login.getTypeName());

View File

@@ -51,6 +51,9 @@ public class JwtTokenUtil {
public static String generateToken(JwtInfo jwtInfo, int expire) throws Exception {
LocalDateTime expireTime = LocalDateTime.now().plusDays(expire);
if (jwtInfo.getExpireTime() != null) {
expireTime = jwtInfo.getExpireTime();
}
byte[] bytes = RsaKeyUtil.toBytes(PRI_KEY);
String token = JwtAnalysisUtil.generateToken(jwtInfo, bytes, expireTime);
return token;

View File

@@ -77,7 +77,8 @@
au.name as staffName,
au.id as staffId,
au.password,
au.role_id
au.role_id,
au.update_at
from admin_user au
where account = #{account}
and au.status = 1
@@ -89,4 +90,16 @@
where id = #{id}
and status = 1
</select>
<select id="getById" resultType="com.ycwl.basic.model.pc.adminUser.entity.LoginEntity">
select
au.account,
au.name as staffName,
au.id as staffId,
au.password,
au.role_id,
au.update_at
from admin_user au
where id = #{id}
and au.status = 1
</select>
</mapper>