|
@@ -1,11 +1,21 @@
|
|
|
package com.gxzc.zen.web.sys.controller
|
|
|
|
|
|
+import com.baomidou.kisso.SSOHelper
|
|
|
+import com.baomidou.kisso.annotation.Action
|
|
|
+import com.baomidou.kisso.annotation.Login
|
|
|
+import com.baomidou.kisso.common.encrypt.MD5Salt
|
|
|
+import com.baomidou.kisso.security.token.SSOToken
|
|
|
+import com.gxzc.zen.api.sys.service.ISysUserService
|
|
|
import com.gxzc.zen.common.base.BaseController
|
|
|
+import com.gxzc.zen.common.dto.RequestDto
|
|
|
import com.gxzc.zen.common.dto.ResultDto
|
|
|
-import org.springframework.web.bind.annotation.PostMapping
|
|
|
-import org.springframework.web.bind.annotation.RequestMapping
|
|
|
-import org.springframework.web.bind.annotation.RestController
|
|
|
-import javax.servlet.http.Cookie
|
|
|
+import com.gxzc.zen.common.exception.ZenException
|
|
|
+import com.gxzc.zen.common.exception.ZenExceptionEnum
|
|
|
+import io.swagger.annotations.ApiOperation
|
|
|
+import org.slf4j.LoggerFactory
|
|
|
+import org.springframework.beans.factory.annotation.Autowired
|
|
|
+import org.springframework.http.ResponseEntity
|
|
|
+import org.springframework.web.bind.annotation.*
|
|
|
|
|
|
/**
|
|
|
* 身份验证相关控制器
|
|
@@ -16,12 +26,43 @@ import javax.servlet.http.Cookie
|
|
|
@RestController
|
|
|
@RequestMapping("auth")
|
|
|
class AuthController : BaseController() {
|
|
|
+ companion object {
|
|
|
+ private val logger = LoggerFactory.getLogger(AuthController::class.java)
|
|
|
+ }
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private lateinit var userService: ISysUserService
|
|
|
+
|
|
|
+ @Login(action = Action.Skip)
|
|
|
+ @ApiOperation(value = "登录")
|
|
|
+ @PostMapping("/login")
|
|
|
+ fun login(@RequestBody data: RequestDto): ResponseEntity<*> {
|
|
|
+ // 验证输入合法性
|
|
|
+ val account = data.data["account"]?.toString()?.trim()
|
|
|
+ val password = data.data["password"]?.toString()
|
|
|
+
|
|
|
+ if (account.isNullOrEmpty() || password.isNullOrEmpty()) {
|
|
|
+ throw ZenException(ZenExceptionEnum.REQUEST_NULL)
|
|
|
+ }
|
|
|
+ // 验证账号密码
|
|
|
+ val user = userService.getUserByAccountCacheable(account!!)
|
|
|
+ ?: throw ZenException(ZenExceptionEnum.AUTH_ACCOUNT_NOT_EXISTS)
|
|
|
+ // 对密码进行盐值处理比对
|
|
|
+ if (user.password != MD5Salt.md5SaltEncode(user.salt, password)) {
|
|
|
+ throw ZenException(ZenExceptionEnum.AUTH_PASSWORD_ERROR)
|
|
|
+ }
|
|
|
+
|
|
|
+ // 生成登陆 token->cookie
|
|
|
+ SSOHelper.setCookie(getRequest(), getResponse(), SSOToken.create().setId(user.id), true)
|
|
|
+
|
|
|
+ // redirectURL
|
|
|
+ return ResponseEntity.ok(ResultDto(200, "success", data.data["redirectURL"]))
|
|
|
+ }
|
|
|
|
|
|
- @PostMapping("login")
|
|
|
- fun login(): ResultDto {
|
|
|
- val req = getRequest()
|
|
|
- val cks = req.cookies
|
|
|
- getResponse().addCookie(Cookie("test", "testCookie"))
|
|
|
- return ResultDto(1, null, "a")
|
|
|
+ @ApiOperation(value = "登出")
|
|
|
+ @DeleteMapping("/logout")
|
|
|
+ fun logout(): ResponseEntity<*> {
|
|
|
+ SSOHelper.logout(getRequest(), getResponse())
|
|
|
+ return ResponseEntity.ok(null)
|
|
|
}
|
|
|
}
|