mirror of
https://github.com/PlayEdu/PlayEdu
synced 2025-12-23 19:19:32 +08:00
后台权限控制
This commit is contained in:
@@ -0,0 +1,14 @@
|
||||
package xyz.playedu.api.middleware;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
|
||||
/**
|
||||
* @Author 杭州白书科技有限公司
|
||||
* @create 2023/2/21 16:40
|
||||
*/
|
||||
@Documented
|
||||
@Target({ElementType.METHOD})
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface BackendPermissionMiddleware {
|
||||
String slug() default "";
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
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.PlayEduThreadLocal;
|
||||
import xyz.playedu.api.bus.BackendBus;
|
||||
import xyz.playedu.api.middleware.BackendPermissionMiddleware;
|
||||
import xyz.playedu.api.types.JsonResponse;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
/**
|
||||
* @Author 杭州白书科技有限公司
|
||||
* @create 2023/2/21 16:42
|
||||
*/
|
||||
@Aspect
|
||||
@Component
|
||||
@Slf4j
|
||||
public class BackendPermissionMiddlewareImpl {
|
||||
|
||||
@Autowired
|
||||
private BackendBus backendBus;
|
||||
|
||||
@Pointcut("@annotation(xyz.playedu.api.middleware.BackendPermissionMiddleware)")
|
||||
private void doPointcut() {
|
||||
}
|
||||
|
||||
@Around("doPointcut()")
|
||||
public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {
|
||||
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
|
||||
BackendPermissionMiddleware middleware = signature.getMethod().getAnnotation(BackendPermissionMiddleware.class);
|
||||
Integer adminUserId = PlayEduThreadLocal.getAdminUserID();
|
||||
HashMap<String, Boolean> permissions = backendBus.adminUserPermissions(adminUserId);
|
||||
if (permissions.get(middleware.slug()) == null) {
|
||||
return JsonResponse.error("权限不足", 403);
|
||||
}
|
||||
return joinPoint.proceed();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user