-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
455 additions
and
14 deletions.
There are no files selected for viewing
145 changes: 145 additions & 0 deletions
145
my-boot/src/test/java/net/ximatai/muyun/test/plaform/TestUser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
package net.ximatai.muyun.test.plaform; | ||
|
||
import io.quarkus.test.common.QuarkusTestResource; | ||
import io.quarkus.test.junit.QuarkusTest; | ||
import io.restassured.common.mapper.TypeRef; | ||
import net.ximatai.muyun.platform.PlatformConst; | ||
import net.ximatai.muyun.test.testcontainers.PostgresTestResource; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.Map; | ||
|
||
import static io.restassured.RestAssured.given; | ||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
@QuarkusTest | ||
@QuarkusTestResource(value = PostgresTestResource.class, restrictToAnnotatedClass = true) | ||
public class TestUser { | ||
String base = PlatformConst.BASE_PATH; | ||
|
||
@Test | ||
void test() { | ||
// 新增人员信息 | ||
String id = given() | ||
.contentType("application/json") | ||
.body(Map.of( | ||
"v_name", "测试", | ||
"dict_user_gender", "0" | ||
)) | ||
.when() | ||
.post("%s/userinfo/create".formatted(base)) | ||
.then() | ||
.statusCode(200) | ||
.extract() | ||
.asString(); | ||
|
||
Map row = given() | ||
.get("%s/userinfo/view/%s".formatted(base, id)) | ||
.then() | ||
.statusCode(200) | ||
.extract() | ||
.as(new TypeRef<>() { | ||
|
||
}); | ||
|
||
assertEquals("测试", row.get("v_name")); | ||
assertFalse((Boolean) row.get("b_user")); | ||
|
||
// 设置用户 | ||
given() | ||
.contentType("application/json") | ||
.body(Map.of( | ||
"v_username", "test", | ||
"v_password", "pw", | ||
"v_password2", "pw" | ||
)) | ||
.when() | ||
.post("%s/userinfo/setUser/%s".formatted(base, id)) | ||
.then() | ||
.statusCode(200) | ||
.extract() | ||
.asString(); | ||
|
||
Map row2 = given() | ||
.get("%s/userinfo/view/%s".formatted(base, id)) | ||
.then() | ||
.statusCode(200) | ||
.extract() | ||
.as(new TypeRef<>() { | ||
|
||
}); | ||
|
||
assertTrue((Boolean) row2.get("b_user")); | ||
|
||
// 登录 | ||
Map loginUser = given() | ||
.contentType("application/json") | ||
.body(Map.of( | ||
"username", "test", | ||
"password", "pw" | ||
)) | ||
.when() | ||
.post("/sso/login") | ||
.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)) | ||
.then() | ||
.statusCode(200); | ||
|
||
given() | ||
.contentType("application/json") | ||
.body(Map.of( | ||
"username", "test", | ||
"password", "pw" | ||
)) | ||
.when() | ||
.post("/sso/login") | ||
.then() | ||
.statusCode(500); | ||
|
||
// 启用用户 | ||
given() | ||
.get("%s/userinfo/enableUser/%s".formatted(base, id)) | ||
.then() | ||
.statusCode(200); | ||
|
||
given() | ||
.contentType("application/json") | ||
.body(Map.of( | ||
"username", "test", | ||
"password", "pw" | ||
)) | ||
.when() | ||
.post("/sso/login") | ||
.then() | ||
.statusCode(200); | ||
|
||
// 删除用户 | ||
given() | ||
.get("%s/userinfo/delete/%s".formatted(base, id)) | ||
.then() | ||
.statusCode(200); | ||
|
||
given() | ||
.contentType("application/json") | ||
.body(Map.of( | ||
"username", "test", | ||
"password", "pw" | ||
)) | ||
.when() | ||
.post("/sso/login") | ||
.then() | ||
.statusCode(500); | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
my-platform/src/main/java/net/ximatai/muyun/platform/controller/SsoController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package net.ximatai.muyun.platform.controller; | ||
|
||
import jakarta.inject.Inject; | ||
import jakarta.ws.rs.POST; | ||
import jakarta.ws.rs.Path; | ||
import net.ximatai.muyun.core.exception.MyException; | ||
import net.ximatai.muyun.model.PageResult; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.util.Map; | ||
import java.util.Objects; | ||
|
||
@Path("/sso") | ||
public class SsoController { | ||
|
||
private final Logger logger = LoggerFactory.getLogger(SsoController.class); | ||
|
||
@Inject | ||
UserController userController; | ||
|
||
@Inject | ||
UserInfoController userInfoController; | ||
|
||
@POST | ||
@Path("/login") | ||
public Map login(Map body) { | ||
String username = (String) body.get("username"); | ||
String password = (String) body.get("password"); | ||
|
||
Objects.requireNonNull(username); | ||
Objects.requireNonNull(password); | ||
|
||
PageResult pageResult = userController.query(Map.of("v_username", username)); | ||
|
||
if (pageResult.getSize() == 0) { | ||
logger.error("不存在的用户信息进行登录:{}", username); | ||
throw new MyException("用户名或密码错误"); | ||
} | ||
|
||
Map userInDB = (Map) pageResult.getList().getFirst(); | ||
|
||
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; | ||
} else { | ||
logger.error("用户已停用,用户名:{}", username); | ||
throw new MyException("用户名或密码错误"); | ||
} | ||
} else { | ||
logger.error("用户密码验证失败,用户名:{}", username); | ||
throw new MyException("用户名或密码错误"); | ||
} | ||
|
||
} | ||
|
||
} |
70 changes: 70 additions & 0 deletions
70
my-platform/src/main/java/net/ximatai/muyun/platform/controller/UserController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package net.ximatai.muyun.platform.controller; | ||
|
||
import jakarta.enterprise.context.ApplicationScoped; | ||
import jakarta.inject.Inject; | ||
import net.ximatai.muyun.ability.IReferableAbility; | ||
import net.ximatai.muyun.ability.ISecurityAbility; | ||
import net.ximatai.muyun.ability.curd.std.IQueryAbility; | ||
import net.ximatai.muyun.core.database.MyTableWrapper; | ||
import net.ximatai.muyun.core.security.AbstractEncryptor; | ||
import net.ximatai.muyun.core.security.SMEncryptor; | ||
import net.ximatai.muyun.database.builder.Column; | ||
import net.ximatai.muyun.database.builder.TableWrapper; | ||
import net.ximatai.muyun.model.QueryItem; | ||
import net.ximatai.muyun.platform.ScaffoldForPlatform; | ||
|
||
import java.util.List; | ||
|
||
@ApplicationScoped | ||
public class UserController extends ScaffoldForPlatform implements IQueryAbility, ISecurityAbility, IReferableAbility { | ||
|
||
@Override | ||
public String getMainTable() { | ||
return "auth_user"; | ||
} | ||
|
||
@Inject | ||
SMEncryptor smEncryptor; | ||
|
||
@Override | ||
public TableWrapper getTableWrapper() { | ||
return new MyTableWrapper(this) | ||
.setPrimaryKey(Column.ID_POSTGRES) | ||
.addColumn("v_username") | ||
.addColumn("v_password") | ||
.addColumn("t_create") | ||
.addColumn("t_update") | ||
.addColumn("t_last_login") | ||
.addColumn("t_this_login") | ||
.addColumn(Column.of("b_enabled").setDefaultValue(true)) | ||
.addIndex("v_username", true); | ||
} | ||
|
||
@Override | ||
public List<QueryItem> queryItemList() { | ||
return List.of( | ||
QueryItem.of("v_username"), | ||
QueryItem.of("b_enabled") | ||
); | ||
} | ||
|
||
@Override | ||
public List<String> getColumnsForSigning() { | ||
return List.of(); | ||
} | ||
|
||
@Override | ||
public List<String> getColumnsForEncryption() { | ||
return List.of("v_password"); | ||
} | ||
|
||
@Override | ||
public AbstractEncryptor getAEncryptor() { | ||
return smEncryptor; | ||
} | ||
|
||
@Override | ||
public String getLabelColumn() { | ||
return "v_username"; | ||
} | ||
} |
Oops, something went wrong.