修改各个层的token报错异常处理

This commit is contained in:
hxf13 2024-07-23 09:58:16 +08:00
parent 73ce32d5e7
commit 39591d3940
9 changed files with 74 additions and 10 deletions

View File

@ -36,11 +36,11 @@ public class SysUserDetailsServiceImpl implements SysUserDetailsService {
if (userDetail.getStatus() == UserStatusEnum.DISABLE.getValue()) {
userDetail.setEnabled(false);
}
// 根据用户id获得身份
// 数据权限范围
List<Long> dataScopeList = getDataScope(userDetail);
userDetail.setDataScopeList(dataScopeList);
//
// 用户权限列表
Set<String> authoritySet = sysMenuService.getUserAuthority(userDetail);

View File

@ -14,6 +14,8 @@ auth:
- /maku/t_book/{id}
- /maku/t_book/{id}
- /user/login
- /maku/t_shopping_trolley/aaa
- /maku/t_book_collection/**
- /maku/t_shopping_trolley/**

View File

@ -1,12 +1,19 @@
package net.maku.maku.controller;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import lombok.AllArgsConstructor;
import net.maku.framework.common.cache.RedisCache;
import net.maku.framework.common.exception.ErrorCode;
import net.maku.framework.common.exception.ServerException;
import net.maku.framework.common.utils.PageResult;
import net.maku.framework.common.utils.Result;
import net.maku.maku.convert.TBookCollectionConvert;
import net.maku.maku.dao.TBookCollectionDao;
import net.maku.maku.entity.TBookCollectionEntity;
import net.maku.maku.entity.TUserEntity;
import net.maku.maku.service.TBookCollectionService;
import net.maku.maku.query.TBookCollectionQuery;
import net.maku.maku.vo.TBookCollectionVO;
@ -15,6 +22,11 @@ import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import jakarta.validation.Valid;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Date;
import java.util.List;
/**
@ -29,7 +41,30 @@ import java.util.List;
@AllArgsConstructor
public class TBookCollectionController {
private final TBookCollectionService tBookCollectionService;
private final TBookCollectionDao bookCollectionDao;
private final RedisCache redisCache;
@GetMapping("/mylist")
public List<TBookCollectionEntity> getBookCollectionByUserId(HttpServletRequest request) {
String token = request.getHeader("token");
TUserEntity entity = (TUserEntity) redisCache.get(token);
if(ObjectUtils.isNull(entity)){
throw new ServerException(ErrorCode.REFRESH_TOKEN_INVALID);
}
TUserEntity tUserEntity = (TUserEntity) redisCache.get(token);
Integer userId=tUserEntity.getId();
LocalDateTime currentTime = LocalDateTime.now();
ZoneId zoneId = ZoneId.systemDefault();
//DATE没有时区二localDateTime有时区,为了将 LocalDateTime 转换为 Date你需要指定一个时区因为 Date 是基于UTC的
// 将LocalDateTime转换为ZonedDateTime然后转换为Instant最后转换为Date
ZonedDateTime zdt = currentTime.atZone(zoneId);
Date date = Date.from(zdt.toInstant());
return bookCollectionDao.selectBookCollectionByUserId(Long.valueOf(userId));
}
@GetMapping("page")
@Operation(summary = "分页")

View File

@ -1,10 +1,13 @@
package net.maku.maku.controller;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import lombok.AllArgsConstructor;
import net.maku.framework.common.cache.RedisCache;
import net.maku.framework.common.exception.ErrorCode;
import net.maku.framework.common.exception.ServerException;
import net.maku.framework.common.utils.PageResult;
import net.maku.framework.common.utils.Result;
import net.maku.maku.convert.TBookCommentConvert;
@ -44,10 +47,12 @@ public class TBookCommentController {
public Result setbookcomment(HttpServletRequest request, @RequestParam Integer bookId,@RequestParam String comment) {
// 首先验证token的有效性然后从Redis中获取用户信息
String token = request.getHeader("token");
if(token==null || token.isEmpty()){
return Result.error("token is error");
TUserEntity entity = (TUserEntity) redisCache.get(token);
if(ObjectUtils.isNull(entity)){
throw new ServerException(ErrorCode.REFRESH_TOKEN_INVALID);
}
TUserEntity tUserEntity = (TUserEntity) redisCache.get(token);
Integer userId=tUserEntity.getId();
LocalDateTime currentTime = LocalDateTime.now();

View File

@ -1,15 +1,19 @@
package net.maku.maku.controller;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import lombok.AllArgsConstructor;
import net.maku.framework.common.cache.RedisCache;
import net.maku.framework.common.exception.ErrorCode;
import net.maku.framework.common.exception.ServerException;
import net.maku.framework.common.utils.PageResult;
import net.maku.framework.common.utils.Result;
import net.maku.maku.convert.TBookCollectionConvert;
import net.maku.maku.convert.TBookConvert;
import net.maku.maku.entity.TBookCollectionEntity;
import net.maku.maku.entity.TBookEntity;
import net.maku.maku.entity.TUserEntity;
import net.maku.maku.service.TBookCollectionService;
@ -43,6 +47,8 @@ public class TBookController {
private final RedisCache redisCache;
private final TBookCollectionService tBookCollectionService;
@GetMapping("/list")
@Operation(summary = "列表")
public Result<List<TBookEntity>> list() {
@ -70,9 +76,10 @@ public class TBookController {
public Result<String> setbookcollection(HttpServletRequest request, @RequestParam Integer bookId) {
// 首先验证token的有效性然后从Redis中获取用户信息
String token = request.getHeader("token");
if(token==null || token.isEmpty()){
return Result.error("token is error");
}
TUserEntity entity = (TUserEntity) redisCache.get(token);
if(ObjectUtils.isNull(entity)){
throw new ServerException(ErrorCode.REFRESH_TOKEN_INVALID);
}
TUserEntity tUserEntity = (TUserEntity) redisCache.get(token);
Integer userId=tUserEntity.getId();
LocalDateTime currentTime = LocalDateTime.now();

View File

@ -1,10 +1,13 @@
package net.maku.maku.controller;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import lombok.AllArgsConstructor;
import net.maku.framework.common.cache.RedisCache;
import net.maku.framework.common.exception.ErrorCode;
import net.maku.framework.common.exception.ServerException;
import net.maku.framework.common.utils.PageResult;
import net.maku.framework.common.utils.Result;
import net.maku.maku.convert.TShoppingTrolleyConvert;
@ -42,10 +45,12 @@ public class TShoppingTrolleyController {
public Result setshoppingtrolly(HttpServletRequest request, @RequestParam Integer bookId) {
// 首先验证token的有效性然后从Redis中获取用户信息
String token = request.getHeader("token");
if(token==null || token.isEmpty()){
return Result.error("token is error");
TUserEntity entity = (TUserEntity) redisCache.get(token);
if(ObjectUtils.isNull(entity)){
throw new ServerException(ErrorCode.REFRESH_TOKEN_INVALID);
}
TUserEntity tUserEntity = (TUserEntity) redisCache.get(token);
Integer userId=tUserEntity.getId();
LocalDateTime currentTime = LocalDateTime.now();

View File

@ -4,6 +4,8 @@ import net.maku.framework.mybatis.dao.BaseDao;
import net.maku.maku.entity.TBookCollectionEntity;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
/**
*
*
@ -12,5 +14,5 @@ import org.apache.ibatis.annotations.Mapper;
*/
@Mapper
public interface TBookCollectionDao extends BaseDao<TBookCollectionEntity> {
List<TBookCollectionEntity> selectBookCollectionByUserId(Long id);
}

View File

@ -11,4 +11,11 @@
<result property="updateTime" column="update_time"/>
</resultMap>
<select id="selectBookCollectionByUserId" resultMap="tBookCollectionMap" parameterType="java.lang.Long">
SELECT *
FROM
t_book_collection bc
LEFT JOIN `t_book` tb ON bc.book_id=tb.id
WHERE user_id = #{id}
</select>
</mapper>

View File

@ -14,4 +14,5 @@
<result property="updateTime" column="update_time"/>
</resultMap>
</mapper>