diff --git a/src/main/java/com/knowledge/base/infrastructure/config/ConstantConfig.java b/src/main/java/com/knowledge/base/infrastructure/config/ConstantConfig.java index 9d85a53..37ca3ad 100644 --- a/src/main/java/com/knowledge/base/infrastructure/config/ConstantConfig.java +++ b/src/main/java/com/knowledge/base/infrastructure/config/ConstantConfig.java @@ -23,13 +23,12 @@ public class ConstantConfig { public static final long USER_CACHE_EXPIRED_MINUTES = Duration.ofDays(10).toMinutes(); - public static final String SHARE_BASE_URL = "http://share.wisdompulse.cn/public"; + public static final String SHARE_BASE_URL = "https://share.wisdompulse.cn/public"; public static final String DEFAULT_UPLOADER = "ADMIN"; public static final String UNKNOWN_LOCAL_RELA_PATH = "UNKNOWN"; - /** * 以下是AnythingLLM相关 */ @@ -38,6 +37,5 @@ public class ConstantConfig { public static final String KEYWORD_PATTERN_LEFT = "#"; public static final String KEYWORD_PATTERN_RIGHT = "#"; - public static final String OS_HOST = "s3.wisdompulse.cn"; -} +} \ No newline at end of file diff --git a/src/main/java/com/knowledge/base/infrastructure/config/CorsConfig.java b/src/main/java/com/knowledge/base/infrastructure/config/CorsConfig.java index b14f3e8..3bbc87b 100644 --- a/src/main/java/com/knowledge/base/infrastructure/config/CorsConfig.java +++ b/src/main/java/com/knowledge/base/infrastructure/config/CorsConfig.java @@ -12,12 +12,16 @@ public class CorsConfig implements WebMvcConfigurer { @Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping("/**") - .allowedOriginPatterns("*") // 或指定域名如 https://admin.wisdompulse.cn + .allowedOriginPatterns( + "chrome-extension://*", + "http://app.wisdompulse.cn", + "https://share.wisdompulse.cn", + "http://share.wisdompulse.cn", + "http://app-test.wisdompulse.cn" + ) .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS") .allowedHeaders("*") .allowCredentials(true) .maxAge(3600); } -} - - +} \ No newline at end of file diff --git a/src/main/java/com/knowledge/base/infrastructure/config/DynamicConfig.java b/src/main/java/com/knowledge/base/infrastructure/config/DynamicConfig.java index 0061ba3..0155b3f 100644 --- a/src/main/java/com/knowledge/base/infrastructure/config/DynamicConfig.java +++ b/src/main/java/com/knowledge/base/infrastructure/config/DynamicConfig.java @@ -30,7 +30,7 @@ public class DynamicConfig { @Value("${micro.saas.doc.parser.recordMsgBody:Y}") private String recordMsgBody; - @Value("${cookie.domain.name:wisdompulse.cn}") + @Value("${cookie.domain.name:.wisdompulse.cn}") private String cookieDomainName; @Value("${import.schedule.cron:* 0/30 * * * ?}") diff --git a/src/main/java/com/knowledge/base/infrastructure/north/controller/UserWriteController.java b/src/main/java/com/knowledge/base/infrastructure/north/controller/UserWriteController.java index 878a3cc..ce46a0c 100644 --- a/src/main/java/com/knowledge/base/infrastructure/north/controller/UserWriteController.java +++ b/src/main/java/com/knowledge/base/infrastructure/north/controller/UserWriteController.java @@ -9,10 +9,11 @@ import com.knowledge.base.infrastructure.north.dto.user.UserTokenDTO; import lombok.RequiredArgsConstructor; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.http.HttpHeaders; +import org.springframework.http.ResponseCookie; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; -import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletResponse; import java.util.List; import java.util.Map; @@ -50,7 +51,7 @@ public class UserWriteController { @DeleteMapping public ResponseEntity deleteUser(@RequestParam Long id) { - if(Objects.isNull(id)) { + if (Objects.isNull(id)) { return ResponseEntity.badRequest().body(Map.of("msg", "userId 不能为空")); } return ResponseEntity.ok(userAppService.deleteUser(id)); @@ -58,7 +59,7 @@ public class UserWriteController { @PutMapping("/{userId}") public ResponseEntity updateUser(@PathVariable Long userId, @RequestBody UserDTO userDTO) { - if(Objects.isNull(userId) || Objects.isNull(userDTO)) { + if (Objects.isNull(userId) || Objects.isNull(userDTO)) { return ResponseEntity.badRequest().body(Map.of("msg", "参数错误:id或用户信息为空!")); } return ResponseEntity.ok(userAppService.updateUser(userId, userDTO)); @@ -66,13 +67,19 @@ public class UserWriteController { @PostMapping("/change-pwd") public ResponseEntity changeUserPassword(@RequestBody Map body) { - if(MapUtil.isEmpty(body)) { + if (MapUtil.isEmpty(body)) { return ResponseEntity.badRequest().body(Map.of("msg", "参数错误,body为空")); } body.getOrDefault("userId", ""); body.getOrDefault("oldPassword", ""); body.getOrDefault("newPassword", ""); - return ResponseEntity.ok(userAppService.changePassword(Long.parseLong(body.get("userId")), body.get("oldPassword"), body.get("newPassword"))); + return ResponseEntity.ok( + userAppService.changePassword( + Long.parseLong(body.get("userId")), + body.get("oldPassword"), + body.get("newPassword") + ) + ); } @PostMapping("/login") @@ -87,14 +94,14 @@ public class UserWriteController { UserDTO user = userOpt.get(); long expireMs = dynamicConfig.getTokenExpireTime(); + long maxAgeSeconds = expireMs / 1000; UserTokenDTO token = userAppService.createToken(user.getId(), expireMs); - Cookie cookie = new Cookie(ConstantConfig.COOKIE_KEY, token.getToken()); - cookie.setPath("/"); - cookie.setDomain(dynamicConfig.getCookieDomainName()); - cookie.setHttpOnly(true); - cookie.setMaxAge((int) (expireMs / 1000)); - response.addCookie(cookie); + ResponseCookie authCookie = buildAuthCookie(token.getToken(), maxAgeSeconds); + response.addHeader(HttpHeaders.SET_COOKIE, authCookie.toString()); + + LOGGER.info("[登录] 用户[{}]登录成功,已写入认证cookie,domain={}, maxAge={}s", + user.getUsername(), dynamicConfig.getCookieDomainName(), maxAgeSeconds); return ResponseEntity.ok(Map.of( "token", token.getToken(), @@ -109,21 +116,18 @@ public class UserWriteController { HttpServletResponse response ) { String token = headerToken != null ? headerToken : cookieToken; - if (token != null) { + if (token != null && !token.isBlank()) { userAppService.removeToken(token); } - Cookie cookie = new Cookie(ConstantConfig.COOKIE_KEY, ""); - cookie.setPath("/"); - cookie.setDomain(dynamicConfig.getCookieDomainName()); - cookie.setHttpOnly(true); - cookie.setMaxAge(0); - response.addCookie(cookie); + ResponseCookie clearCookie = buildAuthCookie("", 0); + response.addHeader(HttpHeaders.SET_COOKIE, clearCookie.toString()); + + LOGGER.info("[登出] 已清理认证cookie,domain={}", dynamicConfig.getCookieDomainName()); return ResponseEntity.ok(Map.of("msg", "已登出")); } - @PostMapping("/add-user-role") public ResponseEntity addUserRole(@RequestParam Long userId, @RequestParam Long roleId) { return ResponseEntity.ok(userAppService.addUserRole(userId, roleId)); @@ -133,4 +137,20 @@ public class UserWriteController { public ResponseEntity removeUserRole(@RequestParam Long userId, @RequestParam Long roleId) { return ResponseEntity.ok(userAppService.removeUserRole(userId, roleId)); } -} + + private ResponseCookie buildAuthCookie(String token, long maxAgeSeconds) { + ResponseCookie.ResponseCookieBuilder builder = ResponseCookie + .from(ConstantConfig.COOKIE_KEY, token == null ? "" : token) + .path("/") + .httpOnly(true) + .sameSite("Lax") + .maxAge(maxAgeSeconds); + + String cookieDomainName = dynamicConfig.getCookieDomainName(); + if (cookieDomainName != null && !cookieDomainName.trim().isEmpty()) { + builder.domain(cookieDomainName.trim()); + } + + return builder.build(); + } +} \ No newline at end of file