修复切换家用ipv6问题“
This commit is contained in:
parent
499530575d
commit
147654f2e5
@ -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";
|
||||
}
|
||||
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@ -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 * * * ?}")
|
||||
|
||||
@ -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;
|
||||
@ -72,7 +73,13 @@ public class UserWriteController {
|
||||
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();
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user