From 0f7912f1a36974a4768fb81a019083ea919fa837 Mon Sep 17 00:00:00 2001 From: Liu Rui Date: Fri, 27 Sep 2024 14:05:16 +0800 Subject: [PATCH] =?UTF-8?q?feat-=E7=99=BB=E5=BD=95=E3=80=81=E9=80=80?= =?UTF-8?q?=E5=87=BA=E3=80=81Runtime=E8=BA=AB=E4=BB=BD=E8=AF=BB=E5=8F=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/resources/application.properties | 1 + .../ximatai/muyun/test/plaform/TestUser.java | 21 ++++-- my-core/build.gradle | 1 + .../muyun/ability/IRuntimeAbility.java | 24 +++++++ .../java/net/ximatai/muyun/core/Scaffold.java | 14 +++- .../net/ximatai/muyun/model/IRuntimeUser.java | 15 +++++ .../muyun/server/VertxRouterSetup.java | 17 +++++ .../controller/RuntimeController.java | 28 ++++++++ .../platform/controller/SsoController.java | 35 +++++++++- .../muyun/platform/model/RuntimeUser.java | 67 +++++++++++++++++++ 10 files changed, 215 insertions(+), 8 deletions(-) create mode 100644 my-core/src/main/java/net/ximatai/muyun/ability/IRuntimeAbility.java create mode 100644 my-core/src/main/java/net/ximatai/muyun/model/IRuntimeUser.java create mode 100644 my-core/src/main/java/net/ximatai/muyun/server/VertxRouterSetup.java create mode 100644 my-platform/src/main/java/net/ximatai/muyun/platform/controller/RuntimeController.java create mode 100644 my-platform/src/main/java/net/ximatai/muyun/platform/model/RuntimeUser.java diff --git a/my-boot/src/main/resources/application.properties b/my-boot/src/main/resources/application.properties index 279c9056..9107f923 100644 --- a/my-boot/src/main/resources/application.properties +++ b/my-boot/src/main/resources/application.properties @@ -2,6 +2,7 @@ server.debug=false quarkus.http.port=8080 #quarkus.http.root-path=/api quarkus.rest.path=/api + quarkus.datasource.db-kind=postgresql quarkus.datasource.username=postgres quarkus.datasource.password=muyun2024 diff --git a/my-boot/src/test/java/net/ximatai/muyun/test/plaform/TestUser.java b/my-boot/src/test/java/net/ximatai/muyun/test/plaform/TestUser.java index 7fd4be8b..863bcaa4 100644 --- a/my-boot/src/test/java/net/ximatai/muyun/test/plaform/TestUser.java +++ b/my-boot/src/test/java/net/ximatai/muyun/test/plaform/TestUser.java @@ -3,7 +3,9 @@ import io.quarkus.test.common.QuarkusTestResource; import io.quarkus.test.junit.QuarkusTest; import io.restassured.common.mapper.TypeRef; +import io.restassured.response.Response; import net.ximatai.muyun.platform.PlatformConst; +import net.ximatai.muyun.platform.model.RuntimeUser; import net.ximatai.muyun.test.testcontainers.PostgresTestResource; import org.junit.jupiter.api.Test; @@ -72,7 +74,7 @@ void test() { assertTrue((Boolean) row2.get("b_user")); // 登录 - Map loginUser = given() + Response response = given() .contentType("application/json") .body(Map.of( "username", "test", @@ -83,13 +85,24 @@ void test() { .then() .statusCode(200) .extract() + .response(); + + RuntimeUser loginUser = response.getBody().as(RuntimeUser.class); + response.getCookies(); + + assertEquals("test", loginUser.getUsername()); + assertEquals("测试", loginUser.getName()); + + given() + .cookies(response.getCookies()) + .get("%s/runtime/whoami".formatted(base)) + .then() + .statusCode(200) + .extract() .as(new TypeRef<>() { }); - assertEquals("test", loginUser.get("v_username_at_auth_user")); - assertEquals("测试", loginUser.get("v_name")); - // 停用用户 given() .get("%s/userinfo/disableUser/%s".formatted(base, id)) diff --git a/my-core/build.gradle b/my-core/build.gradle index c25b0ca2..b6dcdd88 100644 --- a/my-core/build.gradle +++ b/my-core/build.gradle @@ -14,6 +14,7 @@ dependencies { api 'io.quarkus:quarkus-rest' api 'io.quarkus:quarkus-arc' api 'io.quarkus:quarkus-vertx' + api 'io.quarkus:quarkus-undertow' api 'io.quarkus:quarkus-reactive-routes' api 'io.quarkus:quarkus-scheduler' api 'io.quarkus:quarkus-rest-jackson' diff --git a/my-core/src/main/java/net/ximatai/muyun/ability/IRuntimeAbility.java b/my-core/src/main/java/net/ximatai/muyun/ability/IRuntimeAbility.java new file mode 100644 index 00000000..2b5d4204 --- /dev/null +++ b/my-core/src/main/java/net/ximatai/muyun/ability/IRuntimeAbility.java @@ -0,0 +1,24 @@ +package net.ximatai.muyun.ability; + +import io.vertx.ext.web.RoutingContext; +import net.ximatai.muyun.model.IRuntimeUser; + +public interface IRuntimeAbility { + + String SESSION_USER_KEY = "user"; + + RoutingContext getRoutingContext(); + + default IRuntimeUser getUser() { + return getRoutingContext().session().get(SESSION_USER_KEY); + } + + default void setUser(IRuntimeUser user) { + getRoutingContext().session().put(SESSION_USER_KEY, user); + } + + default void destroy() { + getRoutingContext().session().destroy(); + } + +} diff --git a/my-core/src/main/java/net/ximatai/muyun/core/Scaffold.java b/my-core/src/main/java/net/ximatai/muyun/core/Scaffold.java index 926762d7..12c00f1e 100644 --- a/my-core/src/main/java/net/ximatai/muyun/core/Scaffold.java +++ b/my-core/src/main/java/net/ximatai/muyun/core/Scaffold.java @@ -1,16 +1,24 @@ package net.ximatai.muyun.core; import io.vertx.core.eventbus.EventBus; +import io.vertx.ext.web.RoutingContext; import jakarta.annotation.PostConstruct; import jakarta.inject.Inject; import net.ximatai.muyun.ability.IDatabaseAbility; +import net.ximatai.muyun.ability.IRuntimeAbility; import net.ximatai.muyun.ability.ITableCreateAbility; import net.ximatai.muyun.database.IDatabaseOperations; -public abstract class Scaffold implements IDatabaseAbility { +public abstract class Scaffold implements IDatabaseAbility, IRuntimeAbility { private IDatabaseOperations databaseOperations; private EventBus eventBus; + private RoutingContext routingContext; + + @Inject + public void setRoutingContext(RoutingContext routingContext) { + this.routingContext = routingContext; + } @Inject public void setDatabaseOperations(IDatabaseOperations databaseOperations) { @@ -30,6 +38,10 @@ public EventBus getEventBus() { return eventBus; } + public RoutingContext getRoutingContext() { + return routingContext; + } + @PostConstruct protected void init() { if (this instanceof ITableCreateAbility ability) { diff --git a/my-core/src/main/java/net/ximatai/muyun/model/IRuntimeUser.java b/my-core/src/main/java/net/ximatai/muyun/model/IRuntimeUser.java new file mode 100644 index 00000000..bc314130 --- /dev/null +++ b/my-core/src/main/java/net/ximatai/muyun/model/IRuntimeUser.java @@ -0,0 +1,15 @@ +package net.ximatai.muyun.model; + +public interface IRuntimeUser { + + String getId(); + + String getName(); + + String getUsername(); + + String getOrganizationId(); + + String getDepartmentId(); + +} diff --git a/my-core/src/main/java/net/ximatai/muyun/server/VertxRouterSetup.java b/my-core/src/main/java/net/ximatai/muyun/server/VertxRouterSetup.java new file mode 100644 index 00000000..ca3b15ec --- /dev/null +++ b/my-core/src/main/java/net/ximatai/muyun/server/VertxRouterSetup.java @@ -0,0 +1,17 @@ +package net.ximatai.muyun.server; + +import io.quarkus.runtime.StartupEvent; +import io.vertx.core.Vertx; +import io.vertx.ext.web.Router; +import io.vertx.ext.web.handler.SessionHandler; +import io.vertx.ext.web.sstore.LocalSessionStore; +import jakarta.enterprise.context.ApplicationScoped; +import jakarta.enterprise.event.Observes; + +@ApplicationScoped +public class VertxRouterSetup { + + public void init(@Observes StartupEvent ev, Vertx vertx, Router router) { + router.route().handler(SessionHandler.create(LocalSessionStore.create(vertx))); + } +} diff --git a/my-platform/src/main/java/net/ximatai/muyun/platform/controller/RuntimeController.java b/my-platform/src/main/java/net/ximatai/muyun/platform/controller/RuntimeController.java new file mode 100644 index 00000000..d4e016b7 --- /dev/null +++ b/my-platform/src/main/java/net/ximatai/muyun/platform/controller/RuntimeController.java @@ -0,0 +1,28 @@ +package net.ximatai.muyun.platform.controller; + +import io.vertx.ext.web.RoutingContext; +import jakarta.inject.Inject; +import jakarta.ws.rs.GET; +import jakarta.ws.rs.Path; +import net.ximatai.muyun.ability.IRuntimeAbility; +import net.ximatai.muyun.model.IRuntimeUser; + +import static net.ximatai.muyun.platform.PlatformConst.BASE_PATH; + +@Path(BASE_PATH + "/runtime") +public class RuntimeController implements IRuntimeAbility { + + @Inject + RoutingContext routingContext; + + @GET + @Path("/whoami") + public IRuntimeUser whoami() { + return getUser(); + } + + @Override + public RoutingContext getRoutingContext() { + return routingContext; + } +} diff --git a/my-platform/src/main/java/net/ximatai/muyun/platform/controller/SsoController.java b/my-platform/src/main/java/net/ximatai/muyun/platform/controller/SsoController.java index e3c342c0..14181e20 100644 --- a/my-platform/src/main/java/net/ximatai/muyun/platform/controller/SsoController.java +++ b/my-platform/src/main/java/net/ximatai/muyun/platform/controller/SsoController.java @@ -1,10 +1,15 @@ package net.ximatai.muyun.platform.controller; +import io.vertx.ext.web.RoutingContext; import jakarta.inject.Inject; +import jakarta.ws.rs.GET; import jakarta.ws.rs.POST; import jakarta.ws.rs.Path; +import net.ximatai.muyun.ability.IRuntimeAbility; import net.ximatai.muyun.core.exception.MyException; +import net.ximatai.muyun.model.IRuntimeUser; import net.ximatai.muyun.model.PageResult; +import net.ximatai.muyun.platform.model.RuntimeUser; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -12,7 +17,7 @@ import java.util.Objects; @Path("/sso") -public class SsoController { +public class SsoController implements IRuntimeAbility { private final Logger logger = LoggerFactory.getLogger(SsoController.class); @@ -22,9 +27,12 @@ public class SsoController { @Inject UserInfoController userInfoController; + @Inject + RoutingContext routingContext; + @POST @Path("/login") - public Map login(Map body) { + public IRuntimeUser login(Map body) { String username = (String) body.get("username"); String password = (String) body.get("password"); @@ -43,7 +51,9 @@ public Map login(Map body) { if (password.equals(userInDB.get("v_password").toString())) { if ((boolean) userInDB.get("b_enabled")) { Map user = userInfoController.view((String) userInDB.get("id")); - return user; + IRuntimeUser runtimeUser = mapToUser(user); + setUser(runtimeUser); + return runtimeUser; } else { logger.error("用户已停用,用户名:{}", username); throw new MyException("用户名或密码错误"); @@ -52,7 +62,26 @@ public Map login(Map body) { logger.error("用户密码验证失败,用户名:{}", username); throw new MyException("用户名或密码错误"); } + } + @GET + @Path("/logout") + public boolean logout() { + this.destroy(); + return true; } + @Override + public RoutingContext getRoutingContext() { + return routingContext; + } + + private IRuntimeUser mapToUser(Map user) { + return new RuntimeUser() + .setUsername((String) user.get("v_username_at_auth_user")) + .setId((String) user.get("id")) + .setName((String) user.get("v_name")) + .setDepartmentId((String) user.get("id_at_org_department")) + .setOrganizationId((String) user.get("id_at_organization")); + } } diff --git a/my-platform/src/main/java/net/ximatai/muyun/platform/model/RuntimeUser.java b/my-platform/src/main/java/net/ximatai/muyun/platform/model/RuntimeUser.java new file mode 100644 index 00000000..03e016fe --- /dev/null +++ b/my-platform/src/main/java/net/ximatai/muyun/platform/model/RuntimeUser.java @@ -0,0 +1,67 @@ +package net.ximatai.muyun.platform.model; + +import net.ximatai.muyun.model.IRuntimeUser; + +public class RuntimeUser implements IRuntimeUser { + + private String id; + private String name; + private String username; + private boolean isAdmin; + private String organizationId; + private String departmentId; + + public String getId() { + return id; + } + + public RuntimeUser setId(String id) { + this.id = id; + return this; + } + + public String getName() { + return name; + } + + public RuntimeUser setName(String name) { + this.name = name; + return this; + } + + public String getUsername() { + return username; + } + + public RuntimeUser setUsername(String username) { + this.username = username; + return this; + } + + public boolean isAdmin() { + return isAdmin; + } + + public RuntimeUser setAdmin(boolean admin) { + isAdmin = admin; + return this; + } + + public String getOrganizationId() { + return organizationId; + } + + public RuntimeUser setOrganizationId(String organizationId) { + this.organizationId = organizationId; + return this; + } + + public String getDepartmentId() { + return departmentId; + } + + public RuntimeUser setDepartmentId(String departmentId) { + this.departmentId = departmentId; + return this; + } +}