1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
|
@RestController @RequestMapping("/bastion") public class BastionHostController { @Autowired private SSHConnectionManager sshConnectionManager; @Autowired private PermissionManager permissionManager; @Autowired private AuditLogService auditLogService;
@PostMapping("/connect") public ResponseEntity<Map<String, Object>> connectSSH( @RequestParam Long serverId, @RequestParam Long userId) { try { if (!permissionManager.checkPermission(userId, serverId, "READ")) { throw new BusinessException("没有访问权限"); } TargetServer targetServer = getTargetServer(serverId); SSHSession session = sshConnectionManager.createSSHConnection(userId, serverId, targetServer); auditLogService.recordAuditLog(userId, "USER", "SSH_CONNECT", "server:" + serverId, "SUCCESS", "建立SSH连接"); Map<String, Object> response = new HashMap<>(); response.put("success", true); response.put("sessionId", session.getSessionId()); response.put("message", "SSH连接建立成功"); return ResponseEntity.ok(response); } catch (Exception e) { log.error("建立SSH连接失败", e); Map<String, Object> response = new HashMap<>(); response.put("success", false); response.put("message", "建立SSH连接失败: " + e.getMessage()); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response); } }
@PostMapping("/execute") public ResponseEntity<Map<String, Object>> executeCommand( @RequestParam String sessionId, @RequestParam String command, @RequestParam Long userId) { try { SSHSession session = sshConnectionManager.getActiveSessions().stream() .filter(s -> s.getSessionId().equals(sessionId)) .findFirst() .orElse(null); if (session == null) { throw new BusinessException("会话不存在"); } if (!permissionManager.checkPermission(userId, session.getServerId(), "EXECUTE")) { throw new BusinessException("没有执行权限"); } CommandResult result = sshConnectionManager.executeCommand(sessionId, command); auditLogService.recordAuditLog(userId, "USER", "COMMAND_EXECUTE", "command:" + command, result.isSuccess() ? "SUCCESS" : "FAILED", "命令执行结果: " + result.getOutput()); Map<String, Object> response = new HashMap<>(); response.put("success", result.isSuccess()); response.put("output", result.getOutput()); response.put("error", result.getError()); response.put("exitStatus", result.getExitStatus()); return ResponseEntity.ok(response); } catch (Exception e) { log.error("执行SSH命令失败", e); Map<String, Object> response = new HashMap<>(); response.put("success", false); response.put("message", "执行SSH命令失败: " + e.getMessage()); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response); } }
@PostMapping("/disconnect") public ResponseEntity<Map<String, Object>> disconnectSSH( @RequestParam String sessionId, @RequestParam Long userId) { try { sshConnectionManager.closeSSHConnection(sessionId); auditLogService.recordAuditLog(userId, "USER", "SSH_DISCONNECT", "session:" + sessionId, "SUCCESS", "关闭SSH连接"); Map<String, Object> response = new HashMap<>(); response.put("success", true); response.put("message", "SSH连接关闭成功"); return ResponseEntity.ok(response); } catch (Exception e) { log.error("关闭SSH连接失败", e); Map<String, Object> response = new HashMap<>(); response.put("success", false); response.put("message", "关闭SSH连接失败: " + e.getMessage()); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response); } }
@GetMapping("/sessions") public ResponseEntity<Map<String, Object>> getActiveSessions() { try { List<SSHSession> sessions = sshConnectionManager.getActiveSessions(); Map<String, Object> response = new HashMap<>(); response.put("success", true); response.put("sessions", sessions); return ResponseEntity.ok(response); } catch (Exception e) { log.error("获取活跃会话列表失败", e); Map<String, Object> response = new HashMap<>(); response.put("success", false); response.put("message", "获取活跃会话列表失败: " + e.getMessage()); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response); } }
@GetMapping("/audit-logs") public ResponseEntity<Map<String, Object>> getAuditLogs( @RequestParam(required = false) Long userId, @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime startTime, @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime) { try { List<AuditLog> logs = auditLogService.getAuditLogs(userId, startTime, endTime); Map<String, Object> response = new HashMap<>(); response.put("success", true); response.put("logs", logs); return ResponseEntity.ok(response); } catch (Exception e) { log.error("获取审计日志失败", e); Map<String, Object> response = new HashMap<>(); response.put("success", false); response.put("message", "获取审计日志失败: " + e.getMessage()); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response); } }
private TargetServer getTargetServer(Long serverId) { return TargetServer.builder() .id(serverId) .name("测试服务器") .host("192.168.1.100") .port(22) .username("root") .password("password") .build(); } }
|