feat: 实现基于JSON的登录API和安全认证
refactor: 重构登录逻辑和会话管理 fix: 修复会话ID类型和WebSocket连接问题 build: 更新项目版本号和添加Servlet API依赖 style: 清理无用导入和注释代码
This commit is contained in:
@@ -17,6 +17,7 @@ import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
||||
|
||||
import com.ecep.contract.controller.BaseController;
|
||||
import com.ecep.contract.controller.HomeWindowController;
|
||||
import com.ecep.contract.controller.OkHttpLoginController;
|
||||
import com.ecep.contract.task.TaskMonitorCenter;
|
||||
import com.ecep.contract.util.TextMessageHolder;
|
||||
@@ -25,7 +26,7 @@ import com.ecep.contract.vm.CurrentEmployee;
|
||||
|
||||
import javafx.application.Application;
|
||||
import javafx.application.Platform;
|
||||
import javafx.beans.property.SimpleIntegerProperty;
|
||||
import javafx.beans.property.SimpleStringProperty;
|
||||
import javafx.fxml.FXMLLoader;
|
||||
import javafx.scene.Node;
|
||||
import javafx.scene.Parent;
|
||||
@@ -36,6 +37,10 @@ import javafx.scene.text.Text;
|
||||
import javafx.stage.Stage;
|
||||
import javafx.stage.StageStyle;
|
||||
import lombok.Getter;
|
||||
import okhttp3.Cookie;
|
||||
import okhttp3.CookieJar;
|
||||
import okhttp3.HttpUrl;
|
||||
import okhttp3.OkHttpClient;
|
||||
|
||||
/**
|
||||
* JavaFx 应用程序
|
||||
@@ -63,9 +68,12 @@ public class Desktop extends Application {
|
||||
private ScheduledExecutorService scheduledExecutorService = null;
|
||||
private final TaskMonitorCenter taskMonitorCenter = new TaskMonitorCenter();
|
||||
|
||||
private final SimpleIntegerProperty sessionId = new SimpleIntegerProperty(0);
|
||||
private final SimpleStringProperty sessionId = new SimpleStringProperty("");
|
||||
@Getter
|
||||
private final CurrentEmployee activeEmployee = new CurrentEmployee();
|
||||
@Getter
|
||||
private OkHttpClient httpClient;
|
||||
|
||||
|
||||
public void setActiveEmployeeId(int activeEmployeeId) {
|
||||
activeEmployee.getId().set(activeEmployeeId);
|
||||
@@ -75,11 +83,11 @@ public class Desktop extends Application {
|
||||
return activeEmployee.getId().get();
|
||||
}
|
||||
|
||||
public int getSessionId() {
|
||||
public String getSessionId() {
|
||||
return sessionId.get();
|
||||
}
|
||||
|
||||
public void setSessionId(int sessionId) {
|
||||
public void setSessionId(String sessionId) {
|
||||
this.sessionId.set(sessionId);
|
||||
}
|
||||
|
||||
@@ -136,7 +144,6 @@ public class Desktop extends Application {
|
||||
// 更新窗口标题
|
||||
Node titleNode = root.lookup("#title");
|
||||
if (titleNode != null) {
|
||||
|
||||
primaryStage.setTitle(((Text) titleNode).getText());
|
||||
}
|
||||
|
||||
@@ -187,16 +194,37 @@ public class Desktop extends Application {
|
||||
});
|
||||
|
||||
try {
|
||||
initHttpClient();
|
||||
|
||||
OkHttpLoginController controller = new OkHttpLoginController();
|
||||
controller.setHttpClient(this.httpClient);
|
||||
controller.setHolder(holder);
|
||||
controller.setPrimaryStage(primaryStage);
|
||||
controller.setProperties(properties);
|
||||
while (true) {
|
||||
controller.tryLogin();
|
||||
if (getActiveEmployeeId() > 0) {
|
||||
break;
|
||||
// while (true) {
|
||||
controller.tryLogin().whenComplete((v, e) -> {
|
||||
if (e != null) {
|
||||
holder.error("登录失败:" + e.getMessage());
|
||||
} else {
|
||||
holder.info("登录成功");
|
||||
try {
|
||||
while (!SpringApp.isRunning()) {
|
||||
System.out.println("等待启动");
|
||||
Thread.sleep(1000);
|
||||
}
|
||||
} catch (InterruptedException ex) {
|
||||
throw new RuntimeException(ex);
|
||||
}
|
||||
|
||||
// 必须要等待启动成功后才能关闭主场景,否则进程结束程序退出
|
||||
HomeWindowController.show().thenRun(() -> Platform.runLater(primaryStage::close));
|
||||
}
|
||||
}
|
||||
});
|
||||
// if (getActiveEmployeeId() > 0) {
|
||||
// break;
|
||||
// }
|
||||
// }
|
||||
|
||||
} catch (Exception e) {
|
||||
holder.error("登录失败:" + e.getMessage());
|
||||
logger.error(e.getMessage(), e);
|
||||
@@ -210,6 +238,26 @@ public class Desktop extends Application {
|
||||
|
||||
}
|
||||
|
||||
private void initHttpClient() {
|
||||
this.httpClient = new OkHttpClient().newBuilder().cookieJar(new CookieJar() {
|
||||
private final List<Cookie> cookies = new java.util.ArrayList<>();
|
||||
|
||||
@Override
|
||||
public void saveFromResponse(HttpUrl url, List<Cookie> cookies) {
|
||||
// 保存服务器返回的Cookie(如JSESSIONID)
|
||||
this.cookies.addAll(cookies);
|
||||
System.out.println("保存Cookie: " + cookies);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Cookie> loadForRequest(HttpUrl url) {
|
||||
// 请求时自动携带Cookie
|
||||
return cookies;
|
||||
}
|
||||
|
||||
}).build();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void stop() throws Exception {
|
||||
if (logger.isDebugEnabled()) {
|
||||
|
||||
Reference in New Issue
Block a user