完成登录校验

This commit is contained in:
none
2023-02-17 12:07:15 +08:00
parent 24a9db11c1
commit bd5cf234f1
15 changed files with 232 additions and 79 deletions

View File

@@ -0,0 +1,63 @@
package xyz.playedu.api.middleware;
import com.alibaba.fastjson2.JSON;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import xyz.playedu.api.PlayEduThreadLocal;
import xyz.playedu.api.bus.BackendBus;
import xyz.playedu.api.constant.SystemConstant;
import xyz.playedu.api.service.JWTService;
import xyz.playedu.api.types.JWTPayload;
import xyz.playedu.api.types.JsonResponse;
import xyz.playedu.api.util.RequestUtil;
import java.io.IOException;
@Component
@Slf4j
public class AdminAuthMiddleware implements HandlerInterceptor {
@Autowired
private JWTService jwtService;
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
if (BackendBus.inUnAuthWhitelist(request.getRequestURI())) {
return HandlerInterceptor.super.preHandle(request, response, handler);
}
String token = RequestUtil.token();
if (token.length() == 0) {
responseTransform(response, 401, "请登录");
return false;
}
try {
JWTPayload payload = jwtService.parse(token, SystemConstant.JWT_PRV_ADMIN_USER);
// 用户信息写入context
PlayEduThreadLocal.setAdminUserId(payload.getSub());
return HandlerInterceptor.super.preHandle(request, response, handler);
} catch (Exception e) {
responseTransform(response, 401, "请重新登录");
return false;
}
}
private void responseTransform(HttpServletResponse response, int code, String msg) throws IOException {
response.setStatus(code);
response.setContentType("application/json;charset=utf-8");
response.getWriter().print(JSON.toJSONString(JsonResponse.error(msg)));
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
PlayEduThreadLocal.remove();
HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
}
}

View File

@@ -1,12 +0,0 @@
package xyz.playedu.api.middleware;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface AuthMiddleware {
String prv();
}

View File

@@ -2,6 +2,7 @@ package xyz.playedu.api.middleware;
import java.lang.annotation.*;
@Documented
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface ImageCaptchaCheckMiddleware {

View File

@@ -1,34 +0,0 @@
package xyz.playedu.api.middleware.impl;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import xyz.playedu.api.middleware.AuthMiddleware;
import xyz.playedu.api.service.JWTService;
@Aspect
@Component
@Slf4j
public class AuthMiddlewareImpl {
@Autowired
private JWTService jwtService;
@Pointcut("@annotation(xyz.playedu.api.middleware.AuthMiddleware)")
private void doPointcut() {
}
@Around("doPointcut()")
public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {
MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
AuthMiddleware authMiddleware = methodSignature.getMethod().getAnnotation(AuthMiddleware.class);
log.info("prv的值:" + authMiddleware.prv());
return joinPoint.proceed();
}
}