Optimize code

This commit is contained in:
2023-10-23 15:39:54 +08:00
parent 5e21963610
commit f927851cb0
15 changed files with 40 additions and 60 deletions

View File

@@ -27,10 +27,8 @@ class DataFormatConfig {
val tz = timeZone val tz = timeZone
val df: DateFormat = SimpleDateFormat(dataFormat) val df: DateFormat = SimpleDateFormat(dataFormat)
df.timeZone = tz df.timeZone = tz
builder.failOnEmptyBeans(false) builder.failOnEmptyBeans(false).failOnUnknownProperties(false)
.failOnUnknownProperties(false) .featuresToDisable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS).dateFormat(df)
.featuresToDisable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
.dateFormat(df)
} }

View File

@@ -20,9 +20,9 @@ import top.fatweb.api.handler.JwtAuthenticationEntryPointHandler
@Configuration @Configuration
@EnableMethodSecurity @EnableMethodSecurity
class SecurityConfig( class SecurityConfig(
val jwtAuthenticationTokenFilter: JwtAuthenticationTokenFilter, private val jwtAuthenticationTokenFilter: JwtAuthenticationTokenFilter,
val authenticationEntryPointHandler: JwtAuthenticationEntryPointHandler, private val authenticationEntryPointHandler: JwtAuthenticationEntryPointHandler,
val accessDeniedHandler: JwtAccessDeniedHandler private val accessDeniedHandler: JwtAccessDeniedHandler
) { ) {
@Bean @Bean
fun passwordEncoder() = BCryptPasswordEncoder() fun passwordEncoder() = BCryptPasswordEncoder()

View File

@@ -8,39 +8,36 @@ import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PostMapping import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestBody import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RestController import org.springframework.web.bind.annotation.RestController
import top.fatweb.api.annotation.ApiVersion
import top.fatweb.api.converter.UserConverter import top.fatweb.api.converter.UserConverter
import top.fatweb.api.entity.common.ResponseCode import top.fatweb.api.entity.common.ResponseCode
import top.fatweb.api.entity.common.ResponseResult 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.service.permission.IAuthenticationService
import top.fatweb.api.util.WebUtil import top.fatweb.api.util.WebUtil
@Tag(name = "身份认证", description = "身份认证相关接口") @Tag(name = "身份认证", description = "身份认证相关接口")
@ApiVersion(2)
@RestController @RestController
class AuthenticationController(val authenticationService: IAuthenticationService, val userConverter: UserConverter) { class AuthenticationController(
private val authenticationService: IAuthenticationService
) {
@Operation(summary = "登录") @Operation(summary = "登录")
@PostMapping("/login") @PostMapping("/login")
fun login(request: HttpServletRequest, @Valid @RequestBody loginParam: LoginParam) = fun login(request: HttpServletRequest, @Valid @RequestBody loginParam: LoginParam) = ResponseResult.success(
ResponseResult.success(
ResponseCode.SYSTEM_LOGIN_SUCCESS, ResponseCode.SYSTEM_LOGIN_SUCCESS,
"Login success", "Login success",
authenticationService.login(request, userConverter.loginParamToUser(loginParam)) authenticationService.login(request, UserConverter.loginParamToUser(loginParam))
) )
@Operation(summary = "登出") @Operation(summary = "登出")
@PostMapping("/logout") @PostMapping("/logout")
fun logout(request: HttpServletRequest) = fun logout(request: HttpServletRequest) = when (authenticationService.logout(WebUtil.getToken(request))) {
when (authenticationService.logout(WebUtil.getToken(request))) {
true -> ResponseResult.success(ResponseCode.SYSTEM_LOGOUT_SUCCESS, "Logout success", null) true -> ResponseResult.success(ResponseCode.SYSTEM_LOGOUT_SUCCESS, "Logout success", null)
false -> ResponseResult.fail(ResponseCode.SYSTEM_LOGOUT_FAILED, "Logout failed", null) false -> ResponseResult.fail(ResponseCode.SYSTEM_LOGOUT_FAILED, "Logout failed", null)
} }
@Operation(summary = "更新 Token") @Operation(summary = "更新 Token")
@GetMapping("/token") @GetMapping("/token")
fun renewToken(request: HttpServletRequest) = fun renewToken(request: HttpServletRequest) = ResponseResult.success(
ResponseResult.success(
ResponseCode.SYSTEM_TOKEN_RENEW_SUCCESS, ResponseCode.SYSTEM_TOKEN_RENEW_SUCCESS,
"Token renew success", "Token renew success",
authenticationService.renewToken(WebUtil.getToken(request)) authenticationService.renewToken(WebUtil.getToken(request))

View File

@@ -1,10 +1,8 @@
package top.fatweb.api.converter package top.fatweb.api.converter
import org.springframework.stereotype.Component
import top.fatweb.api.entity.permission.User import top.fatweb.api.entity.permission.User
import top.fatweb.api.param.LoginParam import top.fatweb.api.param.authentication.LoginParam
@Component
object UserConverter { object UserConverter {
fun loginParamToUser(loginParam: LoginParam): User { fun loginParamToUser(loginParam: LoginParam): User {
val user = User().apply { val user = User().apply {

View File

@@ -4,17 +4,13 @@ import io.swagger.v3.oas.annotations.media.Schema
import java.io.Serializable import java.io.Serializable
class ResponseResult<T> private constructor( class ResponseResult<T> private constructor(
@Schema(description = "响应码", defaultValue = "200") @Schema(description = "响应码", defaultValue = "200") val code: Int,
val code: Int,
@Schema(description = "是否调用成功") @Schema(description = "是否调用成功") val success: Boolean,
val success: Boolean,
@Schema(description = "信息") @Schema(description = "信息") val msg: String,
val msg: String,
@Schema(description = "数据") @Schema(description = "数据") val data: T?
val data: T?
) : Serializable { ) : Serializable {
companion object { companion object {
fun <T> build(code: ResponseCode, success: Boolean, msg: String, data: T?) = fun <T> build(code: ResponseCode, success: Boolean, msg: String, data: T?) =

View File

@@ -9,9 +9,7 @@ import org.springframework.stereotype.Component
@Component @Component
class ExceptionFilter : Filter { class ExceptionFilter : Filter {
override fun doFilter( override fun doFilter(
servletRequest: ServletRequest?, servletRequest: ServletRequest?, servletResponse: ServletResponse?, filterChain: FilterChain?
servletResponse: ServletResponse?,
filterChain: FilterChain?
) { ) {
try { try {
filterChain!!.doFilter(servletRequest, servletResponse) filterChain!!.doFilter(servletRequest, servletResponse)

View File

@@ -18,9 +18,7 @@ import top.fatweb.api.util.WebUtil
@Component @Component
class JwtAuthenticationTokenFilter(private val redisUtil: RedisUtil) : OncePerRequestFilter() { class JwtAuthenticationTokenFilter(private val redisUtil: RedisUtil) : OncePerRequestFilter() {
override fun doFilterInternal( override fun doFilterInternal(
request: HttpServletRequest, request: HttpServletRequest, response: HttpServletResponse, filterChain: FilterChain
response: HttpServletResponse,
filterChain: FilterChain
) { ) {
val tokenWithPrefix = request.getHeader(SecurityConstants.headerString) val tokenWithPrefix = request.getHeader(SecurityConstants.headerString)

View File

@@ -9,9 +9,7 @@ import org.springframework.stereotype.Component
@Component @Component
class JwtAccessDeniedHandler : AccessDeniedHandler { class JwtAccessDeniedHandler : AccessDeniedHandler {
override fun handle( override fun handle(
request: HttpServletRequest?, request: HttpServletRequest?, response: HttpServletResponse?, accessDeniedException: AccessDeniedException?
response: HttpServletResponse?,
accessDeniedException: AccessDeniedException?
) { ) {
request?.setAttribute("filter.error", accessDeniedException) request?.setAttribute("filter.error", accessDeniedException)
request?.getRequestDispatcher("/error/thrown")?.forward(request, response) request?.getRequestDispatcher("/error/thrown")?.forward(request, response)

View File

@@ -9,9 +9,7 @@ import org.springframework.stereotype.Component
@Component @Component
class JwtAuthenticationEntryPointHandler : AuthenticationEntryPoint { class JwtAuthenticationEntryPointHandler : AuthenticationEntryPoint {
override fun commence( override fun commence(
request: HttpServletRequest?, request: HttpServletRequest?, response: HttpServletResponse?, authException: AuthenticationException?
response: HttpServletResponse?,
authException: AuthenticationException?
) { ) {
request?.setAttribute("filter.error", authException) request?.setAttribute("filter.error", authException)
request?.getRequestDispatcher("/error/thrown")?.forward(request, response) request?.getRequestDispatcher("/error/thrown")?.forward(request, response)

View File

@@ -1,4 +1,4 @@
package top.fatweb.api.param package top.fatweb.api.param.authentication
import io.swagger.v3.oas.annotations.media.Schema import io.swagger.v3.oas.annotations.media.Schema
import jakarta.validation.constraints.NotBlank import jakarta.validation.constraints.NotBlank

View File

@@ -2,8 +2,8 @@ package top.fatweb.api.service.permission
import jakarta.servlet.http.HttpServletRequest import jakarta.servlet.http.HttpServletRequest
import top.fatweb.api.entity.permission.User import top.fatweb.api.entity.permission.User
import top.fatweb.api.vo.LoginVo import top.fatweb.api.vo.authentication.LoginVo
import top.fatweb.api.vo.TokenVo import top.fatweb.api.vo.authentication.TokenVo
interface IAuthenticationService { interface IAuthenticationService {
fun login(request: HttpServletRequest, user: User): LoginVo fun login(request: HttpServletRequest, user: User): LoginVo

View File

@@ -16,8 +16,8 @@ import top.fatweb.api.service.permission.IAuthenticationService
import top.fatweb.api.util.JwtUtil import top.fatweb.api.util.JwtUtil
import top.fatweb.api.util.RedisUtil import top.fatweb.api.util.RedisUtil
import top.fatweb.api.util.WebUtil import top.fatweb.api.util.WebUtil
import top.fatweb.api.vo.LoginVo import top.fatweb.api.vo.authentication.LoginVo
import top.fatweb.api.vo.TokenVo import top.fatweb.api.vo.authentication.TokenVo
import java.time.LocalDateTime import java.time.LocalDateTime
import java.time.ZoneOffset import java.time.ZoneOffset

View File

@@ -62,6 +62,5 @@ object JwtUtil {
* @param jwt jwt 串 * @param jwt jwt 串
* @return 解析内容 * @return 解析内容
*/ */
fun parseJwt(jwt: String): DecodedJWT = fun parseJwt(jwt: String): DecodedJWT = JWT.require(algorithm()).build().verify(jwt)
JWT.require(algorithm()).build().verify(jwt)
} }

View File

@@ -1,4 +1,4 @@
package top.fatweb.api.vo package top.fatweb.api.vo.authentication
import io.swagger.v3.oas.annotations.media.Schema import io.swagger.v3.oas.annotations.media.Schema
import java.time.LocalDateTime import java.time.LocalDateTime

View File

@@ -1,4 +1,4 @@
package top.fatweb.api.vo package top.fatweb.api.vo.authentication
import io.swagger.v3.oas.annotations.media.Schema import io.swagger.v3.oas.annotations.media.Schema