修复切换家用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 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 DEFAULT_UPLOADER = "ADMIN";
|
||||||
|
|
||||||
public static final String UNKNOWN_LOCAL_RELA_PATH = "UNKNOWN";
|
public static final String UNKNOWN_LOCAL_RELA_PATH = "UNKNOWN";
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 以下是AnythingLLM相关
|
* 以下是AnythingLLM相关
|
||||||
*/
|
*/
|
||||||
@ -38,6 +37,5 @@ public class ConstantConfig {
|
|||||||
public static final String KEYWORD_PATTERN_LEFT = "#";
|
public static final String KEYWORD_PATTERN_LEFT = "#";
|
||||||
public static final String KEYWORD_PATTERN_RIGHT = "#";
|
public static final String KEYWORD_PATTERN_RIGHT = "#";
|
||||||
|
|
||||||
|
|
||||||
public static final String OS_HOST = "s3.wisdompulse.cn";
|
public static final String OS_HOST = "s3.wisdompulse.cn";
|
||||||
}
|
}
|
||||||
@ -12,12 +12,16 @@ public class CorsConfig implements WebMvcConfigurer {
|
|||||||
@Override
|
@Override
|
||||||
public void addCorsMappings(CorsRegistry registry) {
|
public void addCorsMappings(CorsRegistry registry) {
|
||||||
registry.addMapping("/**")
|
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")
|
.allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
|
||||||
.allowedHeaders("*")
|
.allowedHeaders("*")
|
||||||
.allowCredentials(true)
|
.allowCredentials(true)
|
||||||
.maxAge(3600);
|
.maxAge(3600);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -30,7 +30,7 @@ public class DynamicConfig {
|
|||||||
@Value("${micro.saas.doc.parser.recordMsgBody:Y}")
|
@Value("${micro.saas.doc.parser.recordMsgBody:Y}")
|
||||||
private String recordMsgBody;
|
private String recordMsgBody;
|
||||||
|
|
||||||
@Value("${cookie.domain.name:wisdompulse.cn}")
|
@Value("${cookie.domain.name:.wisdompulse.cn}")
|
||||||
private String cookieDomainName;
|
private String cookieDomainName;
|
||||||
|
|
||||||
@Value("${import.schedule.cron:* 0/30 * * * ?}")
|
@Value("${import.schedule.cron:* 0/30 * * * ?}")
|
||||||
|
|||||||
@ -9,10 +9,11 @@ import com.knowledge.base.infrastructure.north.dto.user.UserTokenDTO;
|
|||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.http.HttpHeaders;
|
||||||
|
import org.springframework.http.ResponseCookie;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
import javax.servlet.http.Cookie;
|
|
||||||
import javax.servlet.http.HttpServletResponse;
|
import javax.servlet.http.HttpServletResponse;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
@ -50,7 +51,7 @@ public class UserWriteController {
|
|||||||
|
|
||||||
@DeleteMapping
|
@DeleteMapping
|
||||||
public ResponseEntity<?> deleteUser(@RequestParam Long id) {
|
public ResponseEntity<?> deleteUser(@RequestParam Long id) {
|
||||||
if(Objects.isNull(id)) {
|
if (Objects.isNull(id)) {
|
||||||
return ResponseEntity.badRequest().body(Map.of("msg", "userId 不能为空"));
|
return ResponseEntity.badRequest().body(Map.of("msg", "userId 不能为空"));
|
||||||
}
|
}
|
||||||
return ResponseEntity.ok(userAppService.deleteUser(id));
|
return ResponseEntity.ok(userAppService.deleteUser(id));
|
||||||
@ -58,7 +59,7 @@ public class UserWriteController {
|
|||||||
|
|
||||||
@PutMapping("/{userId}")
|
@PutMapping("/{userId}")
|
||||||
public ResponseEntity<?> updateUser(@PathVariable Long userId, @RequestBody UserDTO userDTO) {
|
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.badRequest().body(Map.of("msg", "参数错误:id或用户信息为空!"));
|
||||||
}
|
}
|
||||||
return ResponseEntity.ok(userAppService.updateUser(userId, userDTO));
|
return ResponseEntity.ok(userAppService.updateUser(userId, userDTO));
|
||||||
@ -66,13 +67,19 @@ public class UserWriteController {
|
|||||||
|
|
||||||
@PostMapping("/change-pwd")
|
@PostMapping("/change-pwd")
|
||||||
public ResponseEntity<?> changeUserPassword(@RequestBody Map<String, String> body) {
|
public ResponseEntity<?> changeUserPassword(@RequestBody Map<String, String> body) {
|
||||||
if(MapUtil.isEmpty(body)) {
|
if (MapUtil.isEmpty(body)) {
|
||||||
return ResponseEntity.badRequest().body(Map.of("msg", "参数错误,body为空"));
|
return ResponseEntity.badRequest().body(Map.of("msg", "参数错误,body为空"));
|
||||||
}
|
}
|
||||||
body.getOrDefault("userId", "");
|
body.getOrDefault("userId", "");
|
||||||
body.getOrDefault("oldPassword", "");
|
body.getOrDefault("oldPassword", "");
|
||||||
body.getOrDefault("newPassword", "");
|
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")
|
@PostMapping("/login")
|
||||||
@ -87,14 +94,14 @@ public class UserWriteController {
|
|||||||
|
|
||||||
UserDTO user = userOpt.get();
|
UserDTO user = userOpt.get();
|
||||||
long expireMs = dynamicConfig.getTokenExpireTime();
|
long expireMs = dynamicConfig.getTokenExpireTime();
|
||||||
|
long maxAgeSeconds = expireMs / 1000;
|
||||||
UserTokenDTO token = userAppService.createToken(user.getId(), expireMs);
|
UserTokenDTO token = userAppService.createToken(user.getId(), expireMs);
|
||||||
|
|
||||||
Cookie cookie = new Cookie(ConstantConfig.COOKIE_KEY, token.getToken());
|
ResponseCookie authCookie = buildAuthCookie(token.getToken(), maxAgeSeconds);
|
||||||
cookie.setPath("/");
|
response.addHeader(HttpHeaders.SET_COOKIE, authCookie.toString());
|
||||||
cookie.setDomain(dynamicConfig.getCookieDomainName());
|
|
||||||
cookie.setHttpOnly(true);
|
LOGGER.info("[登录] 用户[{}]登录成功,已写入认证cookie,domain={}, maxAge={}s",
|
||||||
cookie.setMaxAge((int) (expireMs / 1000));
|
user.getUsername(), dynamicConfig.getCookieDomainName(), maxAgeSeconds);
|
||||||
response.addCookie(cookie);
|
|
||||||
|
|
||||||
return ResponseEntity.ok(Map.of(
|
return ResponseEntity.ok(Map.of(
|
||||||
"token", token.getToken(),
|
"token", token.getToken(),
|
||||||
@ -109,21 +116,18 @@ public class UserWriteController {
|
|||||||
HttpServletResponse response
|
HttpServletResponse response
|
||||||
) {
|
) {
|
||||||
String token = headerToken != null ? headerToken : cookieToken;
|
String token = headerToken != null ? headerToken : cookieToken;
|
||||||
if (token != null) {
|
if (token != null && !token.isBlank()) {
|
||||||
userAppService.removeToken(token);
|
userAppService.removeToken(token);
|
||||||
}
|
}
|
||||||
|
|
||||||
Cookie cookie = new Cookie(ConstantConfig.COOKIE_KEY, "");
|
ResponseCookie clearCookie = buildAuthCookie("", 0);
|
||||||
cookie.setPath("/");
|
response.addHeader(HttpHeaders.SET_COOKIE, clearCookie.toString());
|
||||||
cookie.setDomain(dynamicConfig.getCookieDomainName());
|
|
||||||
cookie.setHttpOnly(true);
|
LOGGER.info("[登出] 已清理认证cookie,domain={}", dynamicConfig.getCookieDomainName());
|
||||||
cookie.setMaxAge(0);
|
|
||||||
response.addCookie(cookie);
|
|
||||||
|
|
||||||
return ResponseEntity.ok(Map.of("msg", "已登出"));
|
return ResponseEntity.ok(Map.of("msg", "已登出"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@PostMapping("/add-user-role")
|
@PostMapping("/add-user-role")
|
||||||
public ResponseEntity<?> addUserRole(@RequestParam Long userId, @RequestParam Long roleId) {
|
public ResponseEntity<?> addUserRole(@RequestParam Long userId, @RequestParam Long roleId) {
|
||||||
return ResponseEntity.ok(userAppService.addUserRole(userId, roleId));
|
return ResponseEntity.ok(userAppService.addUserRole(userId, roleId));
|
||||||
@ -133,4 +137,20 @@ public class UserWriteController {
|
|||||||
public ResponseEntity<?> removeUserRole(@RequestParam Long userId, @RequestParam Long roleId) {
|
public ResponseEntity<?> removeUserRole(@RequestParam Long userId, @RequestParam Long roleId) {
|
||||||
return ResponseEntity.ok(userAppService.removeUserRole(userId, 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