新增系统配置api接口

This commit is contained in:
none 2023-03-09 16:34:32 +08:00
parent c85467f62f
commit aabd41ce01
6 changed files with 37 additions and 10 deletions

View File

@ -4,6 +4,7 @@ import xyz.playedu.api.domain.AdminUser;
import java.util.HashMap; import java.util.HashMap;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.Map;
public class PlayEduBackendThreadLocal { public class PlayEduBackendThreadLocal {
@ -12,6 +13,7 @@ public class PlayEduBackendThreadLocal {
public final static String KEY_ADMIN_USER_ID = "admin_id"; public final static String KEY_ADMIN_USER_ID = "admin_id";
public final static String KEY_ADMIN_USER = "admin_user"; public final static String KEY_ADMIN_USER = "admin_user";
public final static String KEY_ADMIN_PER = "admin_per"; public final static String KEY_ADMIN_PER = "admin_per";
public final static String KEY_CONFIG = "config";
public PlayEduBackendThreadLocal() { public PlayEduBackendThreadLocal() {
} }
@ -57,4 +59,12 @@ public class PlayEduBackendThreadLocal {
THREAD_LOCAL.remove(); THREAD_LOCAL.remove();
} }
public static void setConfig(Map<String, String> config) {
put(KEY_CONFIG, config);
}
public static Map<String, String> getConfig() {
return (Map<String, String>) get(KEY_CONFIG);
}
} }

View File

@ -6,14 +6,14 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry; import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import xyz.playedu.api.middleware.AdminAuthMiddleware; import xyz.playedu.api.middleware.AdminMiddleware;
@Configuration @Configuration
@Slf4j @Slf4j
public class WebMvcConfig implements WebMvcConfigurer { public class WebMvcConfig implements WebMvcConfigurer {
@Resource @Resource
private AdminAuthMiddleware adminAuthMiddleware; private AdminMiddleware adminAuthMiddleware;
@Override @Override
public void addInterceptors(InterceptorRegistry registry) { public void addInterceptors(InterceptorRegistry registry) {

View File

@ -4,13 +4,14 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import xyz.playedu.api.config.MinioConfig; import xyz.playedu.api.PlayEduBackendThreadLocal;
import xyz.playedu.api.service.ImageCaptchaService; import xyz.playedu.api.service.ImageCaptchaService;
import xyz.playedu.api.types.ImageCaptchaResult; import xyz.playedu.api.types.ImageCaptchaResult;
import xyz.playedu.api.types.JsonResponse; import xyz.playedu.api.types.JsonResponse;
import java.io.IOException; import java.io.IOException;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map;
@RestController @RestController
@RequestMapping("/backend/v1/system") @RequestMapping("/backend/v1/system")
@ -19,9 +20,6 @@ public class SystemController {
@Autowired @Autowired
private ImageCaptchaService imageCaptchaService; private ImageCaptchaService imageCaptchaService;
@Autowired
private MinioConfig minioConfig;
@GetMapping("/image-captcha") @GetMapping("/image-captcha")
public JsonResponse imageCaptcha() throws IOException { public JsonResponse imageCaptcha() throws IOException {
ImageCaptchaResult imageCaptchaResult = imageCaptchaService.generate(); ImageCaptchaResult imageCaptchaResult = imageCaptchaService.generate();
@ -35,8 +33,7 @@ public class SystemController {
@GetMapping("/config") @GetMapping("/config")
public JsonResponse config() { public JsonResponse config() {
HashMap<String, Object> data = new HashMap<>(); Map<String, String> data = PlayEduBackendThreadLocal.getConfig();
data.put("minio_endpoint", minioConfig.getEndPoint());
return JsonResponse.data(data); return JsonResponse.data(data);
} }

View File

@ -12,6 +12,7 @@ import xyz.playedu.api.bus.BackendBus;
import xyz.playedu.api.constant.SystemConstant; import xyz.playedu.api.constant.SystemConstant;
import xyz.playedu.api.domain.AdminUser; import xyz.playedu.api.domain.AdminUser;
import xyz.playedu.api.service.AdminUserService; import xyz.playedu.api.service.AdminUserService;
import xyz.playedu.api.service.AppConfigService;
import xyz.playedu.api.service.JWTService; import xyz.playedu.api.service.JWTService;
import xyz.playedu.api.types.JWTPayload; import xyz.playedu.api.types.JWTPayload;
import xyz.playedu.api.types.JsonResponse; import xyz.playedu.api.types.JsonResponse;
@ -19,10 +20,11 @@ import xyz.playedu.api.util.HelperUtil;
import xyz.playedu.api.util.RequestUtil; import xyz.playedu.api.util.RequestUtil;
import java.io.IOException; import java.io.IOException;
import java.util.Map;
@Component @Component
@Slf4j @Slf4j
public class AdminAuthMiddleware implements HandlerInterceptor { public class AdminMiddleware implements HandlerInterceptor {
@Autowired @Autowired
private JWTService jwtService; private JWTService jwtService;
@ -36,9 +38,20 @@ public class AdminAuthMiddleware implements HandlerInterceptor {
@Autowired @Autowired
private BackendBus backendBus; private BackendBus backendBus;
@Autowired
private AppConfigService configService;
@Override @Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
if ("OPTIONS".equals(request.getMethod()) || BackendBus.inUnAuthWhitelist(request.getRequestURI())) { if ("OPTIONS".equals(request.getMethod())) {
return HandlerInterceptor.super.preHandle(request, response, handler);
}
// 读取全局配置
Map<String, String> systemConfig = configService.keyValues();
PlayEduBackendThreadLocal.setConfig(systemConfig);
if (BackendBus.inUnAuthWhitelist(request.getRequestURI())) {
return HandlerInterceptor.super.preHandle(request, response, handler); return HandlerInterceptor.super.preHandle(request, response, handler);
} }

View File

@ -20,4 +20,6 @@ public interface AppConfigService extends IService<AppConfig> {
void saveFromMap(HashMap<String, String> data); void saveFromMap(HashMap<String, String> data);
Map<String, String> keyValues();
} }

View File

@ -57,6 +57,11 @@ public class AppConfigServiceImpl extends ServiceImpl<AppConfigMapper, AppConfig
updateBatchById(list); updateBatchById(list);
} }
} }
@Override
public Map<String, String> keyValues() {
return list(query().getWrapper().eq("is_hidden", 0)).stream().collect(Collectors.toMap(AppConfig::getKeyName, AppConfig::getKeyValue));
}
} }