mirror of
https://github.com/201206030/novel.git
synced 2025-04-27 07:30:50 +00:00
perf: 优化用户认证流程
This commit is contained in:
parent
dde5654de6
commit
2d7b3790b9
@ -1,5 +1,6 @@
|
||||
package io.github.xxyopen.novel.controller.front;
|
||||
|
||||
import io.github.xxyopen.novel.core.auth.UserHolder;
|
||||
import io.github.xxyopen.novel.core.common.resp.RestResp;
|
||||
import io.github.xxyopen.novel.core.constant.ApiRouterConsts;
|
||||
import io.github.xxyopen.novel.core.constant.SystemConfigConsts;
|
||||
@ -9,6 +10,7 @@ import io.github.xxyopen.novel.dto.req.UserLoginReqDto;
|
||||
import io.github.xxyopen.novel.dto.req.UserRegisterReqDto;
|
||||
import io.github.xxyopen.novel.dto.resp.UserLoginRespDto;
|
||||
import io.github.xxyopen.novel.service.UserService;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.validation.Valid;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
@ -49,8 +51,8 @@ public class UserController {
|
||||
* 用户信息修改接口
|
||||
*/
|
||||
@PutMapping
|
||||
public RestResp<Void> updateUserInfo(@Valid UserInfoUptReqDto dto, @RequestHeader("Authorization") String token) {
|
||||
dto.setUserId(jwtUtils.parseToken(token, SystemConfigConsts.NOVEL_FRONT_KEY));
|
||||
public RestResp<Void> updateUserInfo(@Valid UserInfoUptReqDto dto) {
|
||||
dto.setUserId(UserHolder.getUserId());
|
||||
return userService.updateUserInfo(dto);
|
||||
}
|
||||
|
||||
@ -58,8 +60,8 @@ public class UserController {
|
||||
* 用户反馈
|
||||
*/
|
||||
@PostMapping("feedBack")
|
||||
public RestResp<Void> submitFeedBack(String content, @RequestHeader("Authorization") String token) {
|
||||
return userService.saveFeedBack(jwtUtils.parseToken(token, SystemConfigConsts.NOVEL_FRONT_KEY), content);
|
||||
public RestResp<Void> submitFeedBack(String content) {
|
||||
return userService.saveFeedBack(UserHolder.getUserId(), content);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -48,6 +48,9 @@ public interface AuthStrategy {
|
||||
// 用户不存在
|
||||
throw new BusinessException(ErrorCodeEnum.USER_ACCOUNT_NOT_EXIST);
|
||||
}
|
||||
// 设置 userId 到当前线程
|
||||
UserHolder.setUserId(userId);
|
||||
// 返回 userId
|
||||
return userId;
|
||||
}
|
||||
}
|
||||
|
@ -44,5 +44,8 @@ public class AuthorAuthStrategy implements AuthStrategy {
|
||||
// 作家账号不存在,无权访问作家专区
|
||||
throw new BusinessException(ErrorCodeEnum.USER_UN_AUTH);
|
||||
}
|
||||
|
||||
// 设置作家ID到当前线程
|
||||
UserHolder.setAuthorId(authorInfo.getId());
|
||||
}
|
||||
}
|
@ -0,0 +1,45 @@
|
||||
package io.github.xxyopen.novel.core.auth;
|
||||
|
||||
import lombok.experimental.UtilityClass;
|
||||
|
||||
/**
|
||||
* 用户信息 持有类
|
||||
*
|
||||
* @author xiongxiaoyang
|
||||
* @date 2022/5/18
|
||||
*/
|
||||
@UtilityClass
|
||||
public class UserHolder {
|
||||
|
||||
/**
|
||||
* 当前线程用户ID
|
||||
* */
|
||||
private static final ThreadLocal<Long> userIdTL = new ThreadLocal<>();
|
||||
|
||||
/**
|
||||
* 当前线程作家ID
|
||||
* */
|
||||
private static final ThreadLocal<Long> authorIdTL = new ThreadLocal<>();
|
||||
|
||||
public void setUserId(Long userId) {
|
||||
userIdTL.set(userId);
|
||||
}
|
||||
|
||||
public Long getUserId() {
|
||||
return userIdTL.get();
|
||||
}
|
||||
|
||||
public void setAuthorId(Long authorId) {
|
||||
authorIdTL.set(authorId);
|
||||
}
|
||||
|
||||
public Long getAuthorId() {
|
||||
return authorIdTL.get();
|
||||
}
|
||||
|
||||
public void clear(){
|
||||
userIdTL.remove();
|
||||
authorIdTL.remove();
|
||||
}
|
||||
|
||||
}
|
@ -2,6 +2,7 @@ package io.github.xxyopen.novel.core.intercepter;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.github.xxyopen.novel.core.auth.AuthStrategy;
|
||||
import io.github.xxyopen.novel.core.auth.UserHolder;
|
||||
import io.github.xxyopen.novel.core.common.constant.ErrorCodeEnum;
|
||||
import io.github.xxyopen.novel.core.common.exception.BusinessException;
|
||||
import io.github.xxyopen.novel.core.common.resp.RestResp;
|
||||
@ -10,9 +11,11 @@ import io.github.xxyopen.novel.core.constant.SystemConfigConsts;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.servlet.HandlerInterceptor;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Map;
|
||||
@ -49,15 +52,21 @@ public class AuthInterceptor implements HandlerInterceptor {
|
||||
// 开始认证
|
||||
try {
|
||||
authStrategy.get(authStrategyName).auth(token);
|
||||
return HandlerInterceptor.super.preHandle(request, response, handler);
|
||||
}catch (BusinessException exception){
|
||||
// 认证失败
|
||||
response.setCharacterEncoding(StandardCharsets.UTF_8.name());
|
||||
response.setContentType(MediaType.APPLICATION_JSON_VALUE);
|
||||
response.getWriter().write(objectMapper.writeValueAsString(RestResp.fail(ErrorCodeEnum.USER_LOGIN_EXPIRED)));
|
||||
response.getWriter().write(objectMapper.writeValueAsString(RestResp.fail(exception.getErrorCodeEnum())));
|
||||
return false;
|
||||
}
|
||||
// 认证成功
|
||||
return HandlerInterceptor.super.preHandle(request, response, handler);
|
||||
}
|
||||
|
||||
@SuppressWarnings("NullableProblems")
|
||||
@Override
|
||||
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
|
||||
// 清理当前线程保存的用户数据
|
||||
UserHolder.clear();
|
||||
HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user