Enhance CORS configuration and WebSocket origin settings to include additional localhost ports. Improve error handling in SftpController and SftpView for better debugging and user feedback.
This commit is contained in:
@@ -62,7 +62,10 @@ public class SecurityConfig {
|
|||||||
@Bean
|
@Bean
|
||||||
public CorsConfigurationSource corsConfigurationSource() {
|
public CorsConfigurationSource corsConfigurationSource() {
|
||||||
CorsConfiguration config = new CorsConfiguration();
|
CorsConfiguration config = new CorsConfiguration();
|
||||||
config.setAllowedOrigins(Arrays.asList("http://localhost:5173", "http://127.0.0.1:5173"));
|
config.setAllowedOrigins(Arrays.asList(
|
||||||
|
"http://localhost:5173", "http://127.0.0.1:5173",
|
||||||
|
"http://localhost:48080", "http://127.0.0.1:48080"
|
||||||
|
));
|
||||||
config.setAllowedMethods(Arrays.asList("GET", "POST", "PUT", "DELETE", "OPTIONS"));
|
config.setAllowedMethods(Arrays.asList("GET", "POST", "PUT", "DELETE", "OPTIONS"));
|
||||||
config.setAllowedHeaders(Arrays.asList("*"));
|
config.setAllowedHeaders(Arrays.asList("*"));
|
||||||
config.setAllowCredentials(true);
|
config.setAllowCredentials(true);
|
||||||
|
|||||||
@@ -23,6 +23,9 @@ public class WebSocketConfig implements WebSocketConfigurer {
|
|||||||
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
|
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
|
||||||
registry.addHandler(terminalWebSocketHandler, "/ws/terminal")
|
registry.addHandler(terminalWebSocketHandler, "/ws/terminal")
|
||||||
.addInterceptors(terminalHandshakeInterceptor)
|
.addInterceptors(terminalHandshakeInterceptor)
|
||||||
.setAllowedOrigins("http://localhost:5173", "http://127.0.0.1:5173");
|
.setAllowedOrigins(
|
||||||
|
"http://localhost:5173", "http://127.0.0.1:5173",
|
||||||
|
"http://localhost:48080", "http://127.0.0.1:48080"
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,6 +6,8 @@ import com.sshmanager.entity.User;
|
|||||||
import com.sshmanager.repository.UserRepository;
|
import com.sshmanager.repository.UserRepository;
|
||||||
import com.sshmanager.service.ConnectionService;
|
import com.sshmanager.service.ConnectionService;
|
||||||
import com.sshmanager.service.SftpService;
|
import com.sshmanager.service.SftpService;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.http.HttpHeaders;
|
import org.springframework.http.HttpHeaders;
|
||||||
import org.springframework.http.MediaType;
|
import org.springframework.http.MediaType;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
@@ -23,6 +25,8 @@ import java.util.stream.Collectors;
|
|||||||
@RequestMapping("/api/sftp")
|
@RequestMapping("/api/sftp")
|
||||||
public class SftpController {
|
public class SftpController {
|
||||||
|
|
||||||
|
private static final Logger log = LoggerFactory.getLogger(SftpController.class);
|
||||||
|
|
||||||
private final ConnectionService connectionService;
|
private final ConnectionService connectionService;
|
||||||
private final UserRepository userRepository;
|
private final UserRepository userRepository;
|
||||||
private final SftpService sftpService;
|
private final SftpService sftpService;
|
||||||
@@ -61,7 +65,7 @@ public class SftpController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/list")
|
@GetMapping("/list")
|
||||||
public ResponseEntity<List<SftpFileInfo>> list(
|
public ResponseEntity<?> list(
|
||||||
@RequestParam Long connectionId,
|
@RequestParam Long connectionId,
|
||||||
@RequestParam(required = false, defaultValue = ".") String path,
|
@RequestParam(required = false, defaultValue = ".") String path,
|
||||||
Authentication authentication) {
|
Authentication authentication) {
|
||||||
@@ -74,7 +78,10 @@ public class SftpController {
|
|||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
return ResponseEntity.ok(dtos);
|
return ResponseEntity.ok(dtos);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
return ResponseEntity.status(500).build();
|
log.warn("SFTP list failed: connectionId={}, path={}", connectionId, path, e);
|
||||||
|
Map<String, String> err = new HashMap<>();
|
||||||
|
err.put("error", e.getMessage() != null ? e.getMessage() : "List failed");
|
||||||
|
return ResponseEntity.status(500).body(err);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -90,7 +97,10 @@ public class SftpController {
|
|||||||
result.put("path", pwd);
|
result.put("path", pwd);
|
||||||
return ResponseEntity.ok(result);
|
return ResponseEntity.ok(result);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
return ResponseEntity.status(500).build();
|
log.warn("SFTP pwd failed: connectionId={}", connectionId, e);
|
||||||
|
Map<String, String> err = new HashMap<>();
|
||||||
|
err.put("error", e.getMessage() != null ? e.getMessage() : "pwd failed");
|
||||||
|
return ResponseEntity.status(500).body(err);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -52,6 +52,13 @@ public class JwtAuthenticationFilter extends OncePerRequestFilter {
|
|||||||
if (StringUtils.hasText(bearerToken) && bearerToken.startsWith("Bearer ")) {
|
if (StringUtils.hasText(bearerToken) && bearerToken.startsWith("Bearer ")) {
|
||||||
return bearerToken.substring(7);
|
return bearerToken.substring(7);
|
||||||
}
|
}
|
||||||
|
// WebSocket handshake sends token as query param
|
||||||
|
if (request.getRequestURI() != null && request.getRequestURI().startsWith("/ws/")) {
|
||||||
|
String token = request.getParameter("token");
|
||||||
|
if (StringUtils.hasText(token)) {
|
||||||
|
return token;
|
||||||
|
}
|
||||||
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -58,7 +58,8 @@ function initPath() {
|
|||||||
currentPath.value = p || '.'
|
currentPath.value = p || '.'
|
||||||
pathParts.value = p === '/' ? [''] : p.split('/').filter(Boolean)
|
pathParts.value = p === '/' ? [''] : p.split('/').filter(Boolean)
|
||||||
loadPath()
|
loadPath()
|
||||||
}).catch(() => {
|
}).catch((err: { response?: { data?: { error?: string } } }) => {
|
||||||
|
error.value = err?.response?.data?.error ?? '获取当前路径失败,请检查连接与认证'
|
||||||
currentPath.value = '.'
|
currentPath.value = '.'
|
||||||
pathParts.value = []
|
pathParts.value = []
|
||||||
loadPath()
|
loadPath()
|
||||||
@@ -76,8 +77,8 @@ function loadPath() {
|
|||||||
return a.name.localeCompare(b.name)
|
return a.name.localeCompare(b.name)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
.catch(() => {
|
.catch((err: { response?: { data?: { error?: string } } }) => {
|
||||||
error.value = '获取文件列表失败'
|
error.value = err?.response?.data?.error ?? '获取文件列表失败'
|
||||||
})
|
})
|
||||||
.finally(() => {
|
.finally(() => {
|
||||||
loading.value = false
|
loading.value = false
|
||||||
|
|||||||
Reference in New Issue
Block a user