mirror of
https://github.com/PlayEdu/PlayEdu
synced 2025-06-28 08:17:30 +08:00
update
This commit is contained in:
parent
0221f99ec6
commit
c254a42cc6
@ -6,7 +6,7 @@ import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class PlayEduBackendThreadLocal {
|
||||
public class PlayEduBContext {
|
||||
|
||||
private static final java.lang.ThreadLocal<LinkedHashMap<String, Object>> THREAD_LOCAL = new java.lang.ThreadLocal<>();
|
||||
|
||||
@ -15,7 +15,7 @@ public class PlayEduBackendThreadLocal {
|
||||
public final static String KEY_ADMIN_PER = "admin_per";
|
||||
public final static String KEY_CONFIG = "config";
|
||||
|
||||
public PlayEduBackendThreadLocal() {
|
||||
public PlayEduBContext() {
|
||||
}
|
||||
|
||||
public static void put(String key, Object val) {
|
||||
@ -31,6 +31,10 @@ public class PlayEduBackendThreadLocal {
|
||||
return THREAD_LOCAL.get().getOrDefault(key, null);
|
||||
}
|
||||
|
||||
public static void remove() {
|
||||
THREAD_LOCAL.remove();
|
||||
}
|
||||
|
||||
public static Integer getAdminUserID() {
|
||||
return (Integer) get(KEY_ADMIN_USER_ID);
|
||||
}
|
||||
@ -55,10 +59,6 @@ public class PlayEduBackendThreadLocal {
|
||||
return (HashMap<String, Boolean>) get(KEY_ADMIN_PER);
|
||||
}
|
||||
|
||||
public static void remove() {
|
||||
THREAD_LOCAL.remove();
|
||||
}
|
||||
|
||||
public static void setConfig(Map<String, String> config) {
|
||||
put(KEY_CONFIG, config);
|
||||
}
|
42
src/main/java/xyz/playedu/api/PlayEduFContext.java
Normal file
42
src/main/java/xyz/playedu/api/PlayEduFContext.java
Normal file
@ -0,0 +1,42 @@
|
||||
package xyz.playedu.api;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
|
||||
/**
|
||||
* @Author 杭州白书科技有限公司
|
||||
* @create 2023/3/13 09:24
|
||||
*/
|
||||
public class PlayEduFContext {
|
||||
private static final java.lang.ThreadLocal<LinkedHashMap<String, Object>> THREAD_LOCAL = new java.lang.ThreadLocal<>();
|
||||
|
||||
private static final String KEY_USER_ID = "user_id";
|
||||
private static final String KEY_USER = "user";
|
||||
|
||||
public PlayEduFContext() {
|
||||
}
|
||||
|
||||
public static void put(String key, Object val) {
|
||||
LinkedHashMap<String, Object> hashMap = THREAD_LOCAL.get();
|
||||
if (hashMap == null) {
|
||||
hashMap = new LinkedHashMap<>();
|
||||
}
|
||||
hashMap.put(key, val);
|
||||
THREAD_LOCAL.set(hashMap);
|
||||
}
|
||||
|
||||
public static Object get(String key) {
|
||||
return THREAD_LOCAL.get().getOrDefault(key, null);
|
||||
}
|
||||
|
||||
public static void remove() {
|
||||
THREAD_LOCAL.remove();
|
||||
}
|
||||
|
||||
public static void setUserId(Integer id) {
|
||||
put(KEY_USER_ID, id);
|
||||
}
|
||||
|
||||
public static Integer getUserId() {
|
||||
return (Integer) get(KEY_USER_ID);
|
||||
}
|
||||
}
|
@ -2,7 +2,7 @@ package xyz.playedu.api.bus;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
import xyz.playedu.api.PlayEduBackendThreadLocal;
|
||||
import xyz.playedu.api.PlayEduBContext;
|
||||
import xyz.playedu.api.constant.BackendConstant;
|
||||
import xyz.playedu.api.domain.AdminRole;
|
||||
import xyz.playedu.api.service.AdminPermissionService;
|
||||
@ -59,7 +59,7 @@ public class BackendBus {
|
||||
}
|
||||
|
||||
public static String valueHidden(String permissionSlug, String type, String value) {
|
||||
HashMap<String, Boolean> permissions = PlayEduBackendThreadLocal.getAdminPer();
|
||||
HashMap<String, Boolean> permissions = PlayEduBContext.getAdminPer();
|
||||
if (permissions.get(permissionSlug) != null) {
|
||||
return value;
|
||||
}
|
||||
|
@ -13,11 +13,11 @@ import xyz.playedu.api.middleware.AdminMiddleware;
|
||||
public class WebMvcConfig implements WebMvcConfigurer {
|
||||
|
||||
@Resource
|
||||
private AdminMiddleware adminAuthMiddleware;
|
||||
private AdminMiddleware adminMiddleware;
|
||||
|
||||
@Override
|
||||
public void addInterceptors(InterceptorRegistry registry) {
|
||||
registry.addInterceptor(adminAuthMiddleware).addPathPatterns("/backend/**");
|
||||
registry.addInterceptor(adminMiddleware).addPathPatterns("/backend/**");
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -4,7 +4,7 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import xyz.playedu.api.PlayEduBackendThreadLocal;
|
||||
import xyz.playedu.api.PlayEduBContext;
|
||||
import xyz.playedu.api.constant.BPermissionConstant;
|
||||
import xyz.playedu.api.domain.CourseChapter;
|
||||
import xyz.playedu.api.event.CourseChapterDestroyEvent;
|
||||
@ -74,7 +74,7 @@ public class CourseChapterController {
|
||||
public JsonResponse destroy(@PathVariable(name = "courseId") Integer courseId, @PathVariable(name = "id") Integer id) throws NotFoundException {
|
||||
CourseChapter chapter = chapterService.findOrFail(id, courseId);
|
||||
chapterService.removeById(chapter.getId());
|
||||
ctx.publishEvent(new CourseChapterDestroyEvent(this, PlayEduBackendThreadLocal.getAdminUserID(), chapter.getCourseId(), chapter.getId(), new Date()));
|
||||
ctx.publishEvent(new CourseChapterDestroyEvent(this, PlayEduBContext.getAdminUserID(), chapter.getCourseId(), chapter.getId(), new Date()));
|
||||
return JsonResponse.success();
|
||||
}
|
||||
}
|
||||
|
@ -7,7 +7,7 @@ import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import xyz.playedu.api.PlayEduBackendThreadLocal;
|
||||
import xyz.playedu.api.PlayEduBContext;
|
||||
import xyz.playedu.api.constant.BPermissionConstant;
|
||||
import xyz.playedu.api.domain.*;
|
||||
import xyz.playedu.api.event.CourseDestroyEvent;
|
||||
@ -108,7 +108,7 @@ public class CourseController {
|
||||
@DeleteMapping("/{id}")
|
||||
public JsonResponse destroy(@PathVariable(name = "id") Integer id) {
|
||||
courseService.removeById(id);
|
||||
ctx.publishEvent(new CourseDestroyEvent(this, PlayEduBackendThreadLocal.getAdminUserID(), id, new Date()));
|
||||
ctx.publishEvent(new CourseDestroyEvent(this, PlayEduBContext.getAdminUserID(), id, new Date()));
|
||||
return JsonResponse.success();
|
||||
}
|
||||
|
||||
|
@ -1,12 +1,10 @@
|
||||
package xyz.playedu.api.controller.backend;
|
||||
|
||||
import lombok.Data;
|
||||
import org.apache.ibatis.annotations.Delete;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import xyz.playedu.api.PlayEduBackendThreadLocal;
|
||||
import xyz.playedu.api.PlayEduBContext;
|
||||
import xyz.playedu.api.constant.BPermissionConstant;
|
||||
import xyz.playedu.api.constant.BackendConstant;
|
||||
import xyz.playedu.api.domain.CourseChapter;
|
||||
@ -82,7 +80,7 @@ public class CourseHourController {
|
||||
chapterService.findOrFail(chapterId, courseId);
|
||||
|
||||
CourseHour courseHour = hourService.create(courseId, chapterId, req.getTitle(), type, req.getDuration(), req.getPublishedAt());
|
||||
ctx.publishEvent(new CourseHourCreatedEvent(this, PlayEduBackendThreadLocal.getAdminUserID(), courseHour.getCourseId(), courseHour.getChapterId(), courseHour.getId(), new Date()));
|
||||
ctx.publishEvent(new CourseHourCreatedEvent(this, PlayEduBContext.getAdminUserID(), courseHour.getCourseId(), courseHour.getChapterId(), courseHour.getId(), new Date()));
|
||||
return JsonResponse.success();
|
||||
}
|
||||
|
||||
@ -110,7 +108,7 @@ public class CourseHourController {
|
||||
public JsonResponse destroy(@PathVariable(name = "courseId") Integer courseId, @PathVariable(name = "id") Integer id) throws NotFoundException {
|
||||
CourseHour courseHour = hourService.findOrFail(id, courseId);
|
||||
hourService.removeById(courseHour.getId());
|
||||
ctx.publishEvent(new CourseHourDestroyEvent(this, PlayEduBackendThreadLocal.getAdminUserID(), courseHour.getCourseId(), courseHour.getChapterId(), courseHour.getId(), new Date()));
|
||||
ctx.publishEvent(new CourseHourDestroyEvent(this, PlayEduBContext.getAdminUserID(), courseHour.getCourseId(), courseHour.getChapterId(), courseHour.getId(), new Date()));
|
||||
return JsonResponse.success();
|
||||
}
|
||||
|
||||
|
@ -5,7 +5,7 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import xyz.playedu.api.PlayEduBackendThreadLocal;
|
||||
import xyz.playedu.api.PlayEduBContext;
|
||||
import xyz.playedu.api.constant.BPermissionConstant;
|
||||
import xyz.playedu.api.domain.Department;
|
||||
import xyz.playedu.api.event.DepartmentDestroyEvent;
|
||||
@ -87,7 +87,7 @@ public class DepartmentController {
|
||||
public JsonResponse destroy(@PathVariable Integer id) throws NotFoundException {
|
||||
Department department = departmentService.findOrFail(id);
|
||||
departmentService.deleteById(department.getId());
|
||||
ctx.publishEvent(new DepartmentDestroyEvent(this, PlayEduBackendThreadLocal.getAdminUserID(), department.getId(), new Date()));
|
||||
ctx.publishEvent(new DepartmentDestroyEvent(this, PlayEduBContext.getAdminUserID(), department.getId(), new Date()));
|
||||
return JsonResponse.success();
|
||||
}
|
||||
|
||||
|
@ -4,7 +4,7 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import xyz.playedu.api.PlayEduBackendThreadLocal;
|
||||
import xyz.playedu.api.PlayEduBContext;
|
||||
import xyz.playedu.api.bus.BackendBus;
|
||||
import xyz.playedu.api.constant.SystemConstant;
|
||||
import xyz.playedu.api.domain.AdminUser;
|
||||
@ -23,7 +23,6 @@ import xyz.playedu.api.util.RequestUtil;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/backend/v1/auth")
|
||||
@ -76,7 +75,7 @@ public class LoginController {
|
||||
|
||||
@GetMapping("/detail")
|
||||
public JsonResponse detail() {
|
||||
AdminUser user = PlayEduBackendThreadLocal.getAdminUser();
|
||||
AdminUser user = PlayEduBContext.getAdminUser();
|
||||
HashMap<String, Boolean> permissions = backendBus.adminUserPermissions(user.getId());
|
||||
|
||||
HashMap<String, Object> data = new HashMap<>();
|
||||
@ -88,7 +87,7 @@ public class LoginController {
|
||||
|
||||
@PutMapping("/password")
|
||||
public JsonResponse changePassword(@RequestBody @Validated PasswordChangeRequest req) {
|
||||
AdminUser user = PlayEduBackendThreadLocal.getAdminUser();
|
||||
AdminUser user = PlayEduBContext.getAdminUser();
|
||||
String password = HelperUtil.MD5(req.getOldPassword() + user.getSalt());
|
||||
if (!password.equals(user.getPassword())) {
|
||||
return JsonResponse.error("原密码不正确");
|
||||
|
@ -4,7 +4,7 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import xyz.playedu.api.PlayEduBackendThreadLocal;
|
||||
import xyz.playedu.api.PlayEduBContext;
|
||||
import xyz.playedu.api.constant.BPermissionConstant;
|
||||
import xyz.playedu.api.domain.ResourceCategory;
|
||||
import xyz.playedu.api.event.ResourceCategoryDestroyEvent;
|
||||
@ -84,7 +84,7 @@ public class ResourceCategoryController {
|
||||
public JsonResponse destroy(@PathVariable Integer id) throws NotFoundException {
|
||||
ResourceCategory category = categoryService.findOrFail(id);
|
||||
categoryService.deleteById(category.getId());
|
||||
ctx.publishEvent(new ResourceCategoryDestroyEvent(this, PlayEduBackendThreadLocal.getAdminUserID(), category.getId(), new Date()));
|
||||
ctx.publishEvent(new ResourceCategoryDestroyEvent(this, PlayEduBContext.getAdminUserID(), category.getId(), new Date()));
|
||||
return JsonResponse.success();
|
||||
}
|
||||
|
||||
|
@ -4,7 +4,7 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import xyz.playedu.api.PlayEduBackendThreadLocal;
|
||||
import xyz.playedu.api.PlayEduBContext;
|
||||
import xyz.playedu.api.service.ImageCaptchaService;
|
||||
import xyz.playedu.api.types.ImageCaptchaResult;
|
||||
import xyz.playedu.api.types.JsonResponse;
|
||||
@ -33,7 +33,7 @@ public class SystemController {
|
||||
|
||||
@GetMapping("/config")
|
||||
public JsonResponse config() {
|
||||
Map<String, String> data = PlayEduBackendThreadLocal.getConfig();
|
||||
Map<String, String> data = PlayEduBContext.getConfig();
|
||||
return JsonResponse.data(data);
|
||||
}
|
||||
|
||||
|
@ -0,0 +1,27 @@
|
||||
package xyz.playedu.api.controller.frontend;
|
||||
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import xyz.playedu.api.request.frontend.ChangePasswordRequest;
|
||||
import xyz.playedu.api.types.JsonResponse;
|
||||
|
||||
/**
|
||||
* @Author 杭州白书科技有限公司
|
||||
* @create 2023/3/13 09:21
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/api/v1/user")
|
||||
public class UserController {
|
||||
|
||||
@GetMapping("/detail")
|
||||
public JsonResponse detail() {
|
||||
return JsonResponse.data(null);
|
||||
}
|
||||
|
||||
@PutMapping("/password")
|
||||
public JsonResponse changePassword(@RequestBody @Validated ChangePasswordRequest req) {
|
||||
return JsonResponse.success();
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -6,7 +6,7 @@ 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.PlayEduBackendThreadLocal;
|
||||
import xyz.playedu.api.PlayEduBContext;
|
||||
import xyz.playedu.api.bus.AppBus;
|
||||
import xyz.playedu.api.bus.BackendBus;
|
||||
import xyz.playedu.api.constant.SystemConstant;
|
||||
@ -49,7 +49,7 @@ public class AdminMiddleware implements HandlerInterceptor {
|
||||
|
||||
// 读取全局配置
|
||||
Map<String, String> systemConfig = configService.keyValues();
|
||||
PlayEduBackendThreadLocal.setConfig(systemConfig);
|
||||
PlayEduBContext.setConfig(systemConfig);
|
||||
|
||||
if (BackendBus.inUnAuthWhitelist(request.getRequestURI())) {
|
||||
return HandlerInterceptor.super.preHandle(request, response, handler);
|
||||
@ -71,9 +71,9 @@ public class AdminMiddleware implements HandlerInterceptor {
|
||||
return responseTransform(response, 403, "当前管理员禁止登录");
|
||||
}
|
||||
|
||||
PlayEduBackendThreadLocal.setAdminUserId(payload.getSub());
|
||||
PlayEduBackendThreadLocal.setAdminUser(adminUser);
|
||||
PlayEduBackendThreadLocal.setAdminPer(backendBus.adminUserPermissions(adminUser.getId()));
|
||||
PlayEduBContext.setAdminUserId(payload.getSub());
|
||||
PlayEduBContext.setAdminUser(adminUser);
|
||||
PlayEduBContext.setAdminPer(backendBus.adminUserPermissions(adminUser.getId()));
|
||||
|
||||
return HandlerInterceptor.super.preHandle(request, response, handler);
|
||||
} catch (Exception e) {
|
||||
@ -93,7 +93,7 @@ public class AdminMiddleware implements HandlerInterceptor {
|
||||
|
||||
@Override
|
||||
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
|
||||
PlayEduBackendThreadLocal.remove();
|
||||
PlayEduBContext.remove();
|
||||
HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,42 @@
|
||||
package xyz.playedu.api.middleware;
|
||||
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.servlet.HandlerInterceptor;
|
||||
import xyz.playedu.api.PlayEduFContext;
|
||||
import xyz.playedu.api.types.JsonResponse;
|
||||
import xyz.playedu.api.util.HelperUtil;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* @Author 杭州白书科技有限公司
|
||||
* @create 2023/3/13 09:40
|
||||
*/
|
||||
@Component
|
||||
@Slf4j
|
||||
public class FrontMiddleware implements HandlerInterceptor {
|
||||
|
||||
@Override
|
||||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
|
||||
if ("OPTIONS".equals(request.getMethod())) {
|
||||
return HandlerInterceptor.super.preHandle(request, response, handler);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private boolean responseTransform(HttpServletResponse response, int code, String msg) throws IOException {
|
||||
response.setStatus(code);
|
||||
response.setContentType("application/json;charset=utf-8");
|
||||
response.getWriter().print(HelperUtil.toJsonStr(JsonResponse.error(msg)));
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
|
||||
PlayEduFContext.remove();
|
||||
HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
|
||||
}
|
||||
}
|
@ -8,7 +8,7 @@ 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.PlayEduBackendThreadLocal;
|
||||
import xyz.playedu.api.PlayEduBContext;
|
||||
import xyz.playedu.api.bus.BackendBus;
|
||||
import xyz.playedu.api.middleware.BackendPermissionMiddleware;
|
||||
import xyz.playedu.api.types.JsonResponse;
|
||||
@ -35,7 +35,7 @@ public class BackendPermissionMiddlewareImpl {
|
||||
public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {
|
||||
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
|
||||
BackendPermissionMiddleware middleware = signature.getMethod().getAnnotation(BackendPermissionMiddleware.class);
|
||||
Integer adminUserId = PlayEduBackendThreadLocal.getAdminUserID();
|
||||
Integer adminUserId = PlayEduBContext.getAdminUserID();
|
||||
HashMap<String, Boolean> permissions = backendBus.adminUserPermissions(adminUserId);
|
||||
if (permissions.get(middleware.slug()) == null) {
|
||||
return JsonResponse.error("权限不足", 403);
|
||||
|
@ -0,0 +1,22 @@
|
||||
package xyz.playedu.api.request.frontend;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* @Author 杭州白书科技有限公司
|
||||
* @create 2023/3/13 09:22
|
||||
*/
|
||||
@Data
|
||||
public class ChangePasswordRequest {
|
||||
|
||||
@NotBlank(message = "请输入原密码")
|
||||
@JsonProperty("old_password")
|
||||
private String oldPassword;
|
||||
|
||||
@NotBlank(message = "请输入新密码")
|
||||
@JsonProperty("new_password")
|
||||
private String newPassword;
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user