From f927851cb0c8886c106bf6391e9f0aaa7e20657c Mon Sep 17 00:00:00 2001 From: FatttSnake Date: Mon, 23 Oct 2023 15:39:54 +0800 Subject: [PATCH] Optimize code --- .../top/fatweb/api/config/DataFormatConfig.kt | 6 +-- .../top/fatweb/api/config/SecurityConfig.kt | 6 +-- .../permission/AuthenticationController.kt | 39 +++++++++---------- .../top/fatweb/api/converter/UserConverter.kt | 4 +- .../api/entity/common/ResponseResult.kt | 12 ++---- .../top/fatweb/api/filter/ExceptionFilter.kt | 4 +- .../filter/JwtAuthenticationTokenFilter.kt | 4 +- .../api/handler/JwtAccessDeniedHandler.kt | 4 +- .../JwtAuthenticationEntryPointHandler.kt | 4 +- .../param/{ => authentication}/LoginParam.kt | 2 +- .../permission/IAuthenticationService.kt | 4 +- .../impl/AuthenticationServiceImpl.kt | 4 +- .../kotlin/top/fatweb/api/util/JwtUtil.kt | 3 +- .../api/vo/{ => authentication}/LoginVo.kt | 2 +- .../api/vo/{ => authentication}/TokenVo.kt | 2 +- 15 files changed, 40 insertions(+), 60 deletions(-) rename src/main/kotlin/top/fatweb/api/param/{ => authentication}/LoginParam.kt (92%) rename src/main/kotlin/top/fatweb/api/vo/{ => authentication}/LoginVo.kt (94%) rename src/main/kotlin/top/fatweb/api/vo/{ => authentication}/TokenVo.kt (91%) diff --git a/src/main/kotlin/top/fatweb/api/config/DataFormatConfig.kt b/src/main/kotlin/top/fatweb/api/config/DataFormatConfig.kt index eaa73a7..3a7b76c 100644 --- a/src/main/kotlin/top/fatweb/api/config/DataFormatConfig.kt +++ b/src/main/kotlin/top/fatweb/api/config/DataFormatConfig.kt @@ -27,10 +27,8 @@ class DataFormatConfig { val tz = timeZone val df: DateFormat = SimpleDateFormat(dataFormat) df.timeZone = tz - builder.failOnEmptyBeans(false) - .failOnUnknownProperties(false) - .featuresToDisable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) - .dateFormat(df) + builder.failOnEmptyBeans(false).failOnUnknownProperties(false) + .featuresToDisable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS).dateFormat(df) } diff --git a/src/main/kotlin/top/fatweb/api/config/SecurityConfig.kt b/src/main/kotlin/top/fatweb/api/config/SecurityConfig.kt index 017f2d4..3c9602d 100644 --- a/src/main/kotlin/top/fatweb/api/config/SecurityConfig.kt +++ b/src/main/kotlin/top/fatweb/api/config/SecurityConfig.kt @@ -20,9 +20,9 @@ import top.fatweb.api.handler.JwtAuthenticationEntryPointHandler @Configuration @EnableMethodSecurity class SecurityConfig( - val jwtAuthenticationTokenFilter: JwtAuthenticationTokenFilter, - val authenticationEntryPointHandler: JwtAuthenticationEntryPointHandler, - val accessDeniedHandler: JwtAccessDeniedHandler + private val jwtAuthenticationTokenFilter: JwtAuthenticationTokenFilter, + private val authenticationEntryPointHandler: JwtAuthenticationEntryPointHandler, + private val accessDeniedHandler: JwtAccessDeniedHandler ) { @Bean fun passwordEncoder() = BCryptPasswordEncoder() diff --git a/src/main/kotlin/top/fatweb/api/controller/permission/AuthenticationController.kt b/src/main/kotlin/top/fatweb/api/controller/permission/AuthenticationController.kt index 2f04f9e..0c75325 100644 --- a/src/main/kotlin/top/fatweb/api/controller/permission/AuthenticationController.kt +++ b/src/main/kotlin/top/fatweb/api/controller/permission/AuthenticationController.kt @@ -8,41 +8,38 @@ import org.springframework.web.bind.annotation.GetMapping import org.springframework.web.bind.annotation.PostMapping import org.springframework.web.bind.annotation.RequestBody import org.springframework.web.bind.annotation.RestController -import top.fatweb.api.annotation.ApiVersion import top.fatweb.api.converter.UserConverter import top.fatweb.api.entity.common.ResponseCode import top.fatweb.api.entity.common.ResponseResult -import top.fatweb.api.param.LoginParam +import top.fatweb.api.param.authentication.LoginParam import top.fatweb.api.service.permission.IAuthenticationService import top.fatweb.api.util.WebUtil @Tag(name = "身份认证", description = "身份认证相关接口") -@ApiVersion(2) @RestController -class AuthenticationController(val authenticationService: IAuthenticationService, val userConverter: UserConverter) { +class AuthenticationController( + private val authenticationService: IAuthenticationService +) { @Operation(summary = "登录") @PostMapping("/login") - fun login(request: HttpServletRequest, @Valid @RequestBody loginParam: LoginParam) = - ResponseResult.success( - ResponseCode.SYSTEM_LOGIN_SUCCESS, - "Login success", - authenticationService.login(request, userConverter.loginParamToUser(loginParam)) - ) + fun login(request: HttpServletRequest, @Valid @RequestBody loginParam: LoginParam) = ResponseResult.success( + ResponseCode.SYSTEM_LOGIN_SUCCESS, + "Login success", + authenticationService.login(request, UserConverter.loginParamToUser(loginParam)) + ) @Operation(summary = "登出") @PostMapping("/logout") - fun logout(request: HttpServletRequest) = - when (authenticationService.logout(WebUtil.getToken(request))) { - true -> ResponseResult.success(ResponseCode.SYSTEM_LOGOUT_SUCCESS, "Logout success", null) - false -> ResponseResult.fail(ResponseCode.SYSTEM_LOGOUT_FAILED, "Logout failed", null) - } + fun logout(request: HttpServletRequest) = when (authenticationService.logout(WebUtil.getToken(request))) { + true -> ResponseResult.success(ResponseCode.SYSTEM_LOGOUT_SUCCESS, "Logout success", null) + false -> ResponseResult.fail(ResponseCode.SYSTEM_LOGOUT_FAILED, "Logout failed", null) + } @Operation(summary = "更新 Token") @GetMapping("/token") - fun renewToken(request: HttpServletRequest) = - ResponseResult.success( - ResponseCode.SYSTEM_TOKEN_RENEW_SUCCESS, - "Token renew success", - authenticationService.renewToken(WebUtil.getToken(request)) - ) + fun renewToken(request: HttpServletRequest) = ResponseResult.success( + ResponseCode.SYSTEM_TOKEN_RENEW_SUCCESS, + "Token renew success", + authenticationService.renewToken(WebUtil.getToken(request)) + ) } \ No newline at end of file diff --git a/src/main/kotlin/top/fatweb/api/converter/UserConverter.kt b/src/main/kotlin/top/fatweb/api/converter/UserConverter.kt index 1bfd381..f975039 100644 --- a/src/main/kotlin/top/fatweb/api/converter/UserConverter.kt +++ b/src/main/kotlin/top/fatweb/api/converter/UserConverter.kt @@ -1,10 +1,8 @@ package top.fatweb.api.converter -import org.springframework.stereotype.Component import top.fatweb.api.entity.permission.User -import top.fatweb.api.param.LoginParam +import top.fatweb.api.param.authentication.LoginParam -@Component object UserConverter { fun loginParamToUser(loginParam: LoginParam): User { val user = User().apply { diff --git a/src/main/kotlin/top/fatweb/api/entity/common/ResponseResult.kt b/src/main/kotlin/top/fatweb/api/entity/common/ResponseResult.kt index 9559c1d..bbfde54 100644 --- a/src/main/kotlin/top/fatweb/api/entity/common/ResponseResult.kt +++ b/src/main/kotlin/top/fatweb/api/entity/common/ResponseResult.kt @@ -4,17 +4,13 @@ import io.swagger.v3.oas.annotations.media.Schema import java.io.Serializable class ResponseResult private constructor( - @Schema(description = "响应码", defaultValue = "200") - val code: Int, + @Schema(description = "响应码", defaultValue = "200") val code: Int, - @Schema(description = "是否调用成功") - val success: Boolean, + @Schema(description = "是否调用成功") val success: Boolean, - @Schema(description = "信息") - val msg: String, + @Schema(description = "信息") val msg: String, - @Schema(description = "数据") - val data: T? + @Schema(description = "数据") val data: T? ) : Serializable { companion object { fun build(code: ResponseCode, success: Boolean, msg: String, data: T?) = diff --git a/src/main/kotlin/top/fatweb/api/filter/ExceptionFilter.kt b/src/main/kotlin/top/fatweb/api/filter/ExceptionFilter.kt index 3097036..0d4ac53 100644 --- a/src/main/kotlin/top/fatweb/api/filter/ExceptionFilter.kt +++ b/src/main/kotlin/top/fatweb/api/filter/ExceptionFilter.kt @@ -9,9 +9,7 @@ import org.springframework.stereotype.Component @Component class ExceptionFilter : Filter { override fun doFilter( - servletRequest: ServletRequest?, - servletResponse: ServletResponse?, - filterChain: FilterChain? + servletRequest: ServletRequest?, servletResponse: ServletResponse?, filterChain: FilterChain? ) { try { filterChain!!.doFilter(servletRequest, servletResponse) diff --git a/src/main/kotlin/top/fatweb/api/filter/JwtAuthenticationTokenFilter.kt b/src/main/kotlin/top/fatweb/api/filter/JwtAuthenticationTokenFilter.kt index 048aa39..fbdf03b 100644 --- a/src/main/kotlin/top/fatweb/api/filter/JwtAuthenticationTokenFilter.kt +++ b/src/main/kotlin/top/fatweb/api/filter/JwtAuthenticationTokenFilter.kt @@ -18,9 +18,7 @@ import top.fatweb.api.util.WebUtil @Component class JwtAuthenticationTokenFilter(private val redisUtil: RedisUtil) : OncePerRequestFilter() { override fun doFilterInternal( - request: HttpServletRequest, - response: HttpServletResponse, - filterChain: FilterChain + request: HttpServletRequest, response: HttpServletResponse, filterChain: FilterChain ) { val tokenWithPrefix = request.getHeader(SecurityConstants.headerString) diff --git a/src/main/kotlin/top/fatweb/api/handler/JwtAccessDeniedHandler.kt b/src/main/kotlin/top/fatweb/api/handler/JwtAccessDeniedHandler.kt index 22e03c6..a47a8c9 100644 --- a/src/main/kotlin/top/fatweb/api/handler/JwtAccessDeniedHandler.kt +++ b/src/main/kotlin/top/fatweb/api/handler/JwtAccessDeniedHandler.kt @@ -9,9 +9,7 @@ import org.springframework.stereotype.Component @Component class JwtAccessDeniedHandler : AccessDeniedHandler { override fun handle( - request: HttpServletRequest?, - response: HttpServletResponse?, - accessDeniedException: AccessDeniedException? + request: HttpServletRequest?, response: HttpServletResponse?, accessDeniedException: AccessDeniedException? ) { request?.setAttribute("filter.error", accessDeniedException) request?.getRequestDispatcher("/error/thrown")?.forward(request, response) diff --git a/src/main/kotlin/top/fatweb/api/handler/JwtAuthenticationEntryPointHandler.kt b/src/main/kotlin/top/fatweb/api/handler/JwtAuthenticationEntryPointHandler.kt index cf5b808..4725a66 100644 --- a/src/main/kotlin/top/fatweb/api/handler/JwtAuthenticationEntryPointHandler.kt +++ b/src/main/kotlin/top/fatweb/api/handler/JwtAuthenticationEntryPointHandler.kt @@ -9,9 +9,7 @@ import org.springframework.stereotype.Component @Component class JwtAuthenticationEntryPointHandler : AuthenticationEntryPoint { override fun commence( - request: HttpServletRequest?, - response: HttpServletResponse?, - authException: AuthenticationException? + request: HttpServletRequest?, response: HttpServletResponse?, authException: AuthenticationException? ) { request?.setAttribute("filter.error", authException) request?.getRequestDispatcher("/error/thrown")?.forward(request, response) diff --git a/src/main/kotlin/top/fatweb/api/param/LoginParam.kt b/src/main/kotlin/top/fatweb/api/param/authentication/LoginParam.kt similarity index 92% rename from src/main/kotlin/top/fatweb/api/param/LoginParam.kt rename to src/main/kotlin/top/fatweb/api/param/authentication/LoginParam.kt index 701ab12..84d15e1 100644 --- a/src/main/kotlin/top/fatweb/api/param/LoginParam.kt +++ b/src/main/kotlin/top/fatweb/api/param/authentication/LoginParam.kt @@ -1,4 +1,4 @@ -package top.fatweb.api.param +package top.fatweb.api.param.authentication import io.swagger.v3.oas.annotations.media.Schema import jakarta.validation.constraints.NotBlank diff --git a/src/main/kotlin/top/fatweb/api/service/permission/IAuthenticationService.kt b/src/main/kotlin/top/fatweb/api/service/permission/IAuthenticationService.kt index 5704e7d..5fb8e07 100644 --- a/src/main/kotlin/top/fatweb/api/service/permission/IAuthenticationService.kt +++ b/src/main/kotlin/top/fatweb/api/service/permission/IAuthenticationService.kt @@ -2,8 +2,8 @@ package top.fatweb.api.service.permission import jakarta.servlet.http.HttpServletRequest import top.fatweb.api.entity.permission.User -import top.fatweb.api.vo.LoginVo -import top.fatweb.api.vo.TokenVo +import top.fatweb.api.vo.authentication.LoginVo +import top.fatweb.api.vo.authentication.TokenVo interface IAuthenticationService { fun login(request: HttpServletRequest, user: User): LoginVo diff --git a/src/main/kotlin/top/fatweb/api/service/permission/impl/AuthenticationServiceImpl.kt b/src/main/kotlin/top/fatweb/api/service/permission/impl/AuthenticationServiceImpl.kt index 139ab2b..eb8c765 100644 --- a/src/main/kotlin/top/fatweb/api/service/permission/impl/AuthenticationServiceImpl.kt +++ b/src/main/kotlin/top/fatweb/api/service/permission/impl/AuthenticationServiceImpl.kt @@ -16,8 +16,8 @@ import top.fatweb.api.service.permission.IAuthenticationService import top.fatweb.api.util.JwtUtil import top.fatweb.api.util.RedisUtil import top.fatweb.api.util.WebUtil -import top.fatweb.api.vo.LoginVo -import top.fatweb.api.vo.TokenVo +import top.fatweb.api.vo.authentication.LoginVo +import top.fatweb.api.vo.authentication.TokenVo import java.time.LocalDateTime import java.time.ZoneOffset diff --git a/src/main/kotlin/top/fatweb/api/util/JwtUtil.kt b/src/main/kotlin/top/fatweb/api/util/JwtUtil.kt index ea7c5aa..2e4f0f8 100644 --- a/src/main/kotlin/top/fatweb/api/util/JwtUtil.kt +++ b/src/main/kotlin/top/fatweb/api/util/JwtUtil.kt @@ -62,6 +62,5 @@ object JwtUtil { * @param jwt jwt 串 * @return 解析内容 */ - fun parseJwt(jwt: String): DecodedJWT = - JWT.require(algorithm()).build().verify(jwt) + fun parseJwt(jwt: String): DecodedJWT = JWT.require(algorithm()).build().verify(jwt) } \ No newline at end of file diff --git a/src/main/kotlin/top/fatweb/api/vo/LoginVo.kt b/src/main/kotlin/top/fatweb/api/vo/authentication/LoginVo.kt similarity index 94% rename from src/main/kotlin/top/fatweb/api/vo/LoginVo.kt rename to src/main/kotlin/top/fatweb/api/vo/authentication/LoginVo.kt index f3e76cc..8d1d96b 100644 --- a/src/main/kotlin/top/fatweb/api/vo/LoginVo.kt +++ b/src/main/kotlin/top/fatweb/api/vo/authentication/LoginVo.kt @@ -1,4 +1,4 @@ -package top.fatweb.api.vo +package top.fatweb.api.vo.authentication import io.swagger.v3.oas.annotations.media.Schema import java.time.LocalDateTime diff --git a/src/main/kotlin/top/fatweb/api/vo/TokenVo.kt b/src/main/kotlin/top/fatweb/api/vo/authentication/TokenVo.kt similarity index 91% rename from src/main/kotlin/top/fatweb/api/vo/TokenVo.kt rename to src/main/kotlin/top/fatweb/api/vo/authentication/TokenVo.kt index c17ea01..3dc7af1 100644 --- a/src/main/kotlin/top/fatweb/api/vo/TokenVo.kt +++ b/src/main/kotlin/top/fatweb/api/vo/authentication/TokenVo.kt @@ -1,4 +1,4 @@ -package top.fatweb.api.vo +package top.fatweb.api.vo.authentication import io.swagger.v3.oas.annotations.media.Schema