mirror of
https://github.com/201206030/novel.git
synced 2025-04-27 07:30:50 +00:00
feat: 增加 Token 解析拦截器
This commit is contained in:
parent
e537240c73
commit
80e7264afa
@ -4,6 +4,7 @@ import io.github.xxyopen.novel.core.constant.ApiRouterConsts;
|
|||||||
import io.github.xxyopen.novel.core.constant.SystemConfigConsts;
|
import io.github.xxyopen.novel.core.constant.SystemConfigConsts;
|
||||||
import io.github.xxyopen.novel.core.interceptor.AuthInterceptor;
|
import io.github.xxyopen.novel.core.interceptor.AuthInterceptor;
|
||||||
import io.github.xxyopen.novel.core.interceptor.FileInterceptor;
|
import io.github.xxyopen.novel.core.interceptor.FileInterceptor;
|
||||||
|
import io.github.xxyopen.novel.core.interceptor.TokenParseInterceptor;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
||||||
@ -25,6 +26,8 @@ public class WebConfig implements WebMvcConfigurer {
|
|||||||
|
|
||||||
private final FileInterceptor fileInterceptor;
|
private final FileInterceptor fileInterceptor;
|
||||||
|
|
||||||
|
private final TokenParseInterceptor tokenParseInterceptor;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void addInterceptors(InterceptorRegistry registry) {
|
public void addInterceptors(InterceptorRegistry registry) {
|
||||||
// 文件访问拦截
|
// 文件访问拦截
|
||||||
@ -42,7 +45,12 @@ public class WebConfig implements WebMvcConfigurer {
|
|||||||
// 放行登录注册相关请求接口
|
// 放行登录注册相关请求接口
|
||||||
.excludePathPatterns(ApiRouterConsts.API_FRONT_USER_URL_PREFIX + "/register"
|
.excludePathPatterns(ApiRouterConsts.API_FRONT_USER_URL_PREFIX + "/register"
|
||||||
, ApiRouterConsts.API_FRONT_USER_URL_PREFIX + "/login"
|
, ApiRouterConsts.API_FRONT_USER_URL_PREFIX + "/login"
|
||||||
,ApiRouterConsts.API_ADMIN_URL_PREFIX + "/login");
|
, ApiRouterConsts.API_ADMIN_URL_PREFIX + "/login");
|
||||||
|
|
||||||
|
// Token 解析拦截器
|
||||||
|
registry.addInterceptor(tokenParseInterceptor)
|
||||||
|
// 拦截小说内容查询接口,需要解析 token 以判断该用户是否有权阅读该章节(付费章节是否已购买)
|
||||||
|
.addPathPatterns(ApiRouterConsts.API_FRONT_BOOK_URL_PREFIX + "/content/*");
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,43 @@
|
|||||||
|
package io.github.xxyopen.novel.core.interceptor;
|
||||||
|
|
||||||
|
import io.github.xxyopen.novel.core.auth.UserHolder;
|
||||||
|
import io.github.xxyopen.novel.core.constant.SystemConfigConsts;
|
||||||
|
import io.github.xxyopen.novel.core.util.JwtUtils;
|
||||||
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
|
import jakarta.servlet.http.HttpServletResponse;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
import org.springframework.util.StringUtils;
|
||||||
|
import org.springframework.web.servlet.HandlerInterceptor;
|
||||||
|
import org.springframework.web.servlet.ModelAndView;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Token 解析拦截器
|
||||||
|
*
|
||||||
|
* @author xiongxiaoyang
|
||||||
|
* @date 2022/5/27
|
||||||
|
*/
|
||||||
|
@Component
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public class TokenParseInterceptor implements HandlerInterceptor {
|
||||||
|
|
||||||
|
private final JwtUtils jwtUtils;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
|
||||||
|
// 获取登录 JWT
|
||||||
|
String token = request.getHeader(SystemConfigConsts.HTTP_AUTH_HEADER_NAME);
|
||||||
|
if (StringUtils.hasText(token)) {
|
||||||
|
// 解析 token 并保存
|
||||||
|
UserHolder.setUserId(jwtUtils.parseToken(token, SystemConfigConsts.NOVEL_FRONT_KEY));
|
||||||
|
}
|
||||||
|
return HandlerInterceptor.super.preHandle(request, response, handler);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
|
||||||
|
// 清理当前线程保存的用户数据
|
||||||
|
UserHolder.clear();
|
||||||
|
HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
|
||||||
|
}
|
||||||
|
}
|
@ -346,6 +346,7 @@ public class BookServiceImpl implements BookService {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public RestResp<BookContentAboutRespDto> getBookContentAbout(Long chapterId) {
|
public RestResp<BookContentAboutRespDto> getBookContentAbout(Long chapterId) {
|
||||||
|
log.debug("userId:{}",UserHolder.getUserId());
|
||||||
// 查询章节信息
|
// 查询章节信息
|
||||||
BookChapterRespDto bookChapter = bookChapterCacheManager.getChapter(chapterId);
|
BookChapterRespDto bookChapter = bookChapterCacheManager.getChapter(chapterId);
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user