Skip to content

Commit

Permalink
feat-登录、退出、Runtime身份读取
Browse files Browse the repository at this point in the history
  • Loading branch information
aruis committed Sep 27, 2024
1 parent 44a29bb commit 0f7912f
Show file tree
Hide file tree
Showing 10 changed files with 215 additions and 8 deletions.
1 change: 1 addition & 0 deletions my-boot/src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 17 additions & 4 deletions my-boot/src/test/java/net/ximatai/muyun/test/plaform/TestUser.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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",
Expand All @@ -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))
Expand Down
1 change: 1 addition & 0 deletions my-core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}

}
14 changes: 13 additions & 1 deletion my-core/src/main/java/net/ximatai/muyun/core/Scaffold.java
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -30,6 +38,10 @@ public EventBus getEventBus() {
return eventBus;
}

public RoutingContext getRoutingContext() {
return routingContext;
}

@PostConstruct
protected void init() {
if (this instanceof ITableCreateAbility ability) {
Expand Down
15 changes: 15 additions & 0 deletions my-core/src/main/java/net/ximatai/muyun/model/IRuntimeUser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package net.ximatai.muyun.model;

public interface IRuntimeUser {

String getId();

String getName();

String getUsername();

String getOrganizationId();

String getDepartmentId();

}
Original file line number Diff line number Diff line change
@@ -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)));
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
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;

import java.util.Map;
import java.util.Objects;

@Path("/sso")
public class SsoController {
public class SsoController implements IRuntimeAbility {

private final Logger logger = LoggerFactory.getLogger(SsoController.class);

Expand All @@ -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");

Expand All @@ -43,7 +51,9 @@ public Map login(Map body) {
if (password.equals(userInDB.get("v_password").toString())) {
if ((boolean) userInDB.get("b_enabled")) {
Map<String, ?> user = userInfoController.view((String) userInDB.get("id"));
return user;
IRuntimeUser runtimeUser = mapToUser(user);
setUser(runtimeUser);
return runtimeUser;
} else {
logger.error("用户已停用,用户名:{}", username);
throw new MyException("用户名或密码错误");
Expand All @@ -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"));
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}

0 comments on commit 0f7912f

Please sign in to comment.