From 402e9e04cd7e02390b4050381bb6ae1e8d3616cf Mon Sep 17 00:00:00 2001 From: msksbr515 Date: Wed, 20 May 2026 17:29:04 +0800 Subject: [PATCH] refactor(api): restructure auth endpoints with DTO validation and unified response - Add Result generic response template for standardized API output - Introduce UserLoginDTO with validation annotations for login requests - Migrate AuthController to use DTO binding and return Result responses - Update AuthService interface to accept UserLoginDTO and return Boolean - Add Jackson configuration (snake_case, non-null, date format) - Include jackson-module-kotlin and spring-boot-starter-validation deps --- build.gradle.kts | 2 ++ .../bookmgr/controller/AuthController.kt | 35 ++++++++++++++----- .../com/msksbr/bookmgr/dto/UserLoginDTO.kt | 11 ++++++ .../com/msksbr/bookmgr/service/AuthService.kt | 4 ++- .../bookmgr/service/impl/AuthServiceImpl.kt | 3 +- .../com/msksbr/bookmgr/template/Result.kt | 26 ++++++++++++++ src/main/resources/application.yaml | 9 +++-- 7 files changed, 77 insertions(+), 13 deletions(-) create mode 100644 src/main/kotlin/com/msksbr/bookmgr/dto/UserLoginDTO.kt create mode 100644 src/main/kotlin/com/msksbr/bookmgr/template/Result.kt diff --git a/build.gradle.kts b/build.gradle.kts index 06a1f3c..dd24c86 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -27,6 +27,8 @@ dependencies { implementation("org.bouncycastle:bcprov-jdk18on:1.84") implementation("com.mysql:mysql-connector-j") implementation("com.baomidou:mybatis-plus-spring-boot4-starter:3.5.15") + implementation("com.fasterxml.jackson.module:jackson-module-kotlin") + implementation("org.springframework.boot:spring-boot-starter-validation") testImplementation("org.springframework.boot:spring-boot-starter-test") testImplementation("org.jetbrains.kotlin:kotlin-test-junit5") testRuntimeOnly("org.junit.platform:junit-platform-launcher") diff --git a/src/main/kotlin/com/msksbr/bookmgr/controller/AuthController.kt b/src/main/kotlin/com/msksbr/bookmgr/controller/AuthController.kt index 4b9d9a0..5ae33a8 100644 --- a/src/main/kotlin/com/msksbr/bookmgr/controller/AuthController.kt +++ b/src/main/kotlin/com/msksbr/bookmgr/controller/AuthController.kt @@ -1,24 +1,43 @@ package com.msksbr.bookmgr.controller +import com.msksbr.bookmgr.dto.UserLoginDTO import com.msksbr.bookmgr.service.AuthService import jakarta.servlet.http.HttpSession -import org.springframework.beans.factory.annotation.Autowired import org.springframework.web.bind.annotation.PostMapping +import org.springframework.web.bind.annotation.RequestMapping import org.springframework.web.bind.annotation.RestController +import com.msksbr.bookmgr.template.Result +import jakarta.validation.Valid -// 管理登录、登出 +/* +* 登录接口 +* 接收DTO,返回json +*/ @RestController +@RequestMapping("/api/auth") class AuthController( val authService: AuthService ) { - @PostMapping("/api/auth/login") - fun login(username: String?, password: String?) :String? { - return TODO("提供返回值") + @PostMapping("/login") + fun login( + @Valid + loginDTO: UserLoginDTO, + session: HttpSession + ) : Result { + // 调用service验证 + val success=authService.login(loginDTO) + return if (success) { + // 登录成功,存入session + session.setAttribute("loginUser", loginDTO.username) + Result.success("login success") + }else{ + Result.error("username or password not match") + } } - @PostMapping("/api/auth/logout") - fun logout(session: HttpSession): String? { + @PostMapping("/logout") + fun logout(session: HttpSession): Result { // 直接销毁session session.invalidate() - return TODO("Need json template") + return Result.success("logout successfully") } } \ No newline at end of file diff --git a/src/main/kotlin/com/msksbr/bookmgr/dto/UserLoginDTO.kt b/src/main/kotlin/com/msksbr/bookmgr/dto/UserLoginDTO.kt new file mode 100644 index 0000000..da24535 --- /dev/null +++ b/src/main/kotlin/com/msksbr/bookmgr/dto/UserLoginDTO.kt @@ -0,0 +1,11 @@ +package com.msksbr.bookmgr.dto + +import jakarta.validation.constraints.NotBlank + + +data class UserLoginDTO( + @NotBlank(message = "username is required") + var username: String, + @NotBlank(message = "password is required") + var password: String +) \ No newline at end of file diff --git a/src/main/kotlin/com/msksbr/bookmgr/service/AuthService.kt b/src/main/kotlin/com/msksbr/bookmgr/service/AuthService.kt index 0ab2058..32b0b9d 100644 --- a/src/main/kotlin/com/msksbr/bookmgr/service/AuthService.kt +++ b/src/main/kotlin/com/msksbr/bookmgr/service/AuthService.kt @@ -1,5 +1,7 @@ package com.msksbr.bookmgr.service +import com.msksbr.bookmgr.dto.UserLoginDTO + interface AuthService { - fun login(username: String?, password: String?): String? + fun login(loginDTO: UserLoginDTO): Boolean } \ No newline at end of file diff --git a/src/main/kotlin/com/msksbr/bookmgr/service/impl/AuthServiceImpl.kt b/src/main/kotlin/com/msksbr/bookmgr/service/impl/AuthServiceImpl.kt index fcacb49..6f1a38e 100644 --- a/src/main/kotlin/com/msksbr/bookmgr/service/impl/AuthServiceImpl.kt +++ b/src/main/kotlin/com/msksbr/bookmgr/service/impl/AuthServiceImpl.kt @@ -1,11 +1,12 @@ package com.msksbr.bookmgr.service.impl +import com.msksbr.bookmgr.dto.UserLoginDTO import com.msksbr.bookmgr.service.AuthService import org.springframework.stereotype.Service @Service class AuthServiceImpl: AuthService { - override fun login(username: String?, password: String?): String? { + override fun login(loginDTO: UserLoginDTO): Boolean { TODO("Not yet implemented") } } \ No newline at end of file diff --git a/src/main/kotlin/com/msksbr/bookmgr/template/Result.kt b/src/main/kotlin/com/msksbr/bookmgr/template/Result.kt new file mode 100644 index 0000000..95aa2ab --- /dev/null +++ b/src/main/kotlin/com/msksbr/bookmgr/template/Result.kt @@ -0,0 +1,26 @@ +package com.msksbr.bookmgr.template + +data class Result( + var code: Int, + var message: String, + var data: T? +) { + companion object { + // 成功 + fun success(data: T): Result { + return Result( + code = 200, + message = "success", + data = data + ) + } + // 失败 + fun error(message: String): Result { + return Result( + code = 500, + message = message, + data = null + ) + } + } +} \ No newline at end of file diff --git a/src/main/resources/application.yaml b/src/main/resources/application.yaml index 053b9e2..f6af5d2 100644 --- a/src/main/resources/application.yaml +++ b/src/main/resources/application.yaml @@ -8,10 +8,13 @@ spring: url: jdbc:${DB_TYPE}://${DB_URL}:${DB_PORT}/${DB_NAME} username: ${DB_USER} password: ${DB_PASSWORD} + jackson: + default-property-inclusion: non_null + property-naming-strategy: SNAKE_CASE + date-format: ${JSON_DATE_FORMAT} + time-zone: ${JSON_TIME_ZONE} mybatis-plus: configuration: # 开启驼峰命名法 - map-underscore-to-camel-case: true - # 开启日志输出sql语句 - log-impl: org.apache.ibatis.logging.commons.JakartaCommonsLoggingImpl \ No newline at end of file + map-underscore-to-camel-case: true \ No newline at end of file