refactor(api): restructure auth endpoints with DTO validation and unified response
- Add Result<T> 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
This commit is contained in:
@@ -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")
|
||||
|
||||
@@ -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<String> {
|
||||
// 调用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<String> {
|
||||
// 直接销毁session
|
||||
session.invalidate()
|
||||
return TODO("Need json template")
|
||||
return Result.success("logout successfully")
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
)
|
||||
@@ -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
|
||||
}
|
||||
@@ -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")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.msksbr.bookmgr.template
|
||||
|
||||
data class Result<T>(
|
||||
var code: Int,
|
||||
var message: String,
|
||||
var data: T?
|
||||
) {
|
||||
companion object {
|
||||
// 成功
|
||||
fun <T> success(data: T): Result<T> {
|
||||
return Result(
|
||||
code = 200,
|
||||
message = "success",
|
||||
data = data
|
||||
)
|
||||
}
|
||||
// 失败
|
||||
fun <T> error(message: String): Result<T> {
|
||||
return Result(
|
||||
code = 500,
|
||||
message = message,
|
||||
data = null
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
Reference in New Issue
Block a user