feat: 更新安全配置和日志输出

主要修改:
1. 在SecurityConfig中允许所有CORS预检请求(OPTIONS)公开访问。
2. 更新日志输出,增加对OPTIONS请求的权限配置说明。

技术细节:
- 通过允许OPTIONS请求,增强了跨域资源共享(CORS)的支持,提升了前端与后端的交互能力。
This commit is contained in:
zyh
2025-08-27 18:52:00 +08:00
parent 4daf71f62b
commit 1377c25847
3 changed files with 24 additions and 15 deletions

View File

@@ -39,6 +39,7 @@ public class SecurityConfig {
.authenticationManager(authenticationManager())
.authorizeExchange(ex -> ex
.pathMatchers("/actuator/**").permitAll()
.pathMatchers(HttpMethod.OPTIONS, "/**").permitAll() // 允许所有CORS预检请求
.pathMatchers(HttpMethod.POST, "/api/auth/login").permitAll()
.pathMatchers(HttpMethod.GET, "/api/auth/me").permitAll()
.pathMatchers(HttpMethod.GET, "/api/link/status").permitAll() // 用户端获取链接状态接口,公开访问
@@ -62,6 +63,7 @@ public class SecurityConfig {
log.info(" - JWT过滤器: 已集成到Security链中 (AUTHENTICATION位置)");
log.info(" - 路径权限配置:");
log.info(" * /actuator/** -> 允许所有");
log.info(" * OPTIONS /** -> 允许所有 (CORS预检请求)");
log.info(" * POST /api/auth/login -> 允许所有");
log.info(" * GET /api/auth/me -> 允许所有");
log.info(" * GET /api/link/status -> 允许所有 (用户端公开接口)");

View File

@@ -3,7 +3,7 @@ spring:
name: gameplatform-server
datasource:
url: jdbc:mysql://localhost:3306/login_task_db?useSSL=false&serverTimezone=UTC&characterEncoding=utf8&allowPublicKeyRetrieval=true
url: jdbc:mysql://192.140.164.137:3306/login_task_db?useSSL=false&serverTimezone=UTC&characterEncoding=utf8&allowPublicKeyRetrieval=true
username: login_task_db
password: 3MaXfeWJ4d6cGMrL
driver-class-name: com.mysql.cj.jdbc.Driver
@@ -70,7 +70,7 @@ script:
# 服务器配置
app:
base-url: "http://localhost:18080" # 生产环境需要配置为实际域名
base-url: "http://192.140.164.137:18080" # 生产环境需要配置为实际域名
image-save-path: "./images" # 图片保存路径
link:

View File

@@ -4,15 +4,18 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import com.gameplatform.server.mapper.agent.LinkTaskMapper;
import com.gameplatform.server.model.dto.device.DeviceStatusResponse;
import com.gameplatform.server.model.entity.agent.LinkTask;
import com.gameplatform.server.service.external.ScriptClient;
import com.gameplatform.server.service.image.ImageSaveService;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import reactor.core.publisher.Mono;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;
@@ -29,7 +32,7 @@ public class DeviceTaskUpdateServiceTest {
private LinkTaskMapper linkTaskMapper;
@Mock
private ScriptClient scriptClient;
private ImageSaveService imageSaveService;
private ObjectMapper objectMapper;
@@ -37,17 +40,7 @@ public class DeviceTaskUpdateServiceTest {
void setUp() {
MockitoAnnotations.openMocks(this);
objectMapper = new ObjectMapper();
deviceTaskUpdateService = new DeviceTaskUpdateService(linkTaskMapper, scriptClient, objectMapper);
// 设置ScriptClient mock返回值
when(scriptClient.getResourceUrl(eq("f1"), eq("首次主页.png")))
.thenReturn("http://36.138.184.60:12345/f1/首次主页.png");
when(scriptClient.getResourceUrl(eq("f1"), eq("首次赏金.png")))
.thenReturn("http://36.138.184.60:12345/f1/首次赏金.png");
when(scriptClient.getResourceUrl(eq("f1"), eq("中途赏金.png")))
.thenReturn("http://36.138.184.60:12345/f1/中途赏金.png");
when(scriptClient.getResourceUrl(eq("f1"), eq("结束赏金.png")))
.thenReturn("http://36.138.184.60:12345/f1/结束赏金.png");
deviceTaskUpdateService = new DeviceTaskUpdateService(linkTaskMapper, objectMapper, imageSaveService);
}
@Test
@@ -92,6 +85,13 @@ public class DeviceTaskUpdateServiceTest {
when(linkTaskMapper.findByMachineIdAndStatus("f1", "LOGGED_IN")).thenReturn(tasks);
when(linkTaskMapper.update(any(LinkTask.class))).thenReturn(1);
Map<String, String> mockImages = new HashMap<>();
mockImages.put("homepage", "首次主页.png");
mockImages.put("firstReward", "首次赏金.png");
mockImages.put("midReward", "中途赏金.png");
mockImages.put("endReward", "结束赏金.png");
when(imageSaveService.downloadAndSaveCompletionImages(anyString(), anyString()))
.thenReturn(Mono.just(mockImages));
// 执行测试
deviceTaskUpdateService.updateTaskByDeviceStatus(deviceInfo);
@@ -124,6 +124,13 @@ public class DeviceTaskUpdateServiceTest {
when(linkTaskMapper.findByMachineIdAndStatus("f1", "LOGGED_IN")).thenReturn(tasks);
when(linkTaskMapper.update(any(LinkTask.class))).thenReturn(1);
Map<String, String> mockImages = new HashMap<>();
mockImages.put("homepage", "首次主页.png");
mockImages.put("firstReward", "首次赏金.png");
mockImages.put("midReward", "中途赏金.png");
mockImages.put("endReward", "结束赏金.png");
when(imageSaveService.downloadAndSaveCompletionImages(anyString(), anyString()))
.thenReturn(Mono.just(mockImages));
// 执行测试
deviceTaskUpdateService.updateTaskByDeviceStatus(deviceInfo);