Optimize code
This commit is contained in:
@@ -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)
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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))
|
||||
)
|
||||
}
|
||||
@@ -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 {
|
||||
|
||||
@@ -4,17 +4,13 @@ import io.swagger.v3.oas.annotations.media.Schema
|
||||
import java.io.Serializable
|
||||
|
||||
class ResponseResult<T> 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 <T> build(code: ResponseCode, success: Boolean, msg: String, data: T?) =
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
@@ -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
|
||||
@@ -1,4 +1,4 @@
|
||||
package top.fatweb.api.vo
|
||||
package top.fatweb.api.vo.authentication
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema
|
||||
|
||||
Reference in New Issue
Block a user