From d771dbe9ed6d9c1e44d4438c7980fd33e1dec73d Mon Sep 17 00:00:00 2001 From: van1164 Date: Wed, 5 Jun 2024 22:32:12 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20security=20=EC=99=80=20comment=20do?= =?UTF-8?q?main=20=ED=8C=A8=ED=82=A4=EC=A7=80=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../kotlin/com/van1164/comment/CommentController.kt | 6 ++---- .../com/van1164/comment/CommentReadRepository.kt | 1 - .../kotlin/com/van1164/comment/CommentRepository.kt | 7 +------ .../kotlin/com/van1164/comment/CommentService.kt | 6 ++---- .../van1164/comment/like/CommentLikeRepository.kt | 2 +- .../security/JwtServerAuthenticationConverter.kt | 3 ++- .../kotlin/com/van1164/security/JwtTokenProvider.kt | 7 ++----- .../van1164/security/OAuthSuccessHandlerReactive.kt | 2 +- .../security/PrincipalOauthUserServiceReactive.kt | 1 + .../van1164/security/PrincipalServiceReactive.kt | 1 + .../kotlin/com/van1164/common/domain/SubComment.kt | 1 + .../com/van1164/common/domain/UserSubscribe.kt | 13 +++++++++++++ .../main/kotlin/com/van1164/common/domain/Video.kt | 2 +- .../com/van1164/common/domain/comment/Comment.kt | 2 +- .../com/van1164/common/domain/comment/CommentBad.kt | 2 +- .../van1164/common/domain/comment/CommentLike.kt | 2 +- .../van1164/common/dto/VideoDetailResponseDTO.kt | 2 +- .../com/van1164/common/exception/CustomException.kt | 5 +++++ .../com/van1164/common/response/DetailResponse.kt | 1 - .../com/van1164/common/security/PrincipalDetails.kt | 2 +- .../kotlin/com/van1164/common/security/TokenInfo.kt | 2 +- .../com/van1164/video/like/VideoLikeController.kt | 4 +--- .../com/van1164/video/upload/VideoController.kt | 6 +++--- .../video/view/controller/VideoViewController.kt | 2 +- 24 files changed, 44 insertions(+), 38 deletions(-) create mode 100644 util/src/main/kotlin/com/van1164/common/domain/UserSubscribe.kt create mode 100644 util/src/main/kotlin/com/van1164/common/exception/CustomException.kt diff --git a/comment/src/main/kotlin/com/van1164/comment/CommentController.kt b/comment/src/main/kotlin/com/van1164/comment/CommentController.kt index 6bf2982..37bf7a4 100644 --- a/comment/src/main/kotlin/com/van1164/comment/CommentController.kt +++ b/comment/src/main/kotlin/com/van1164/comment/CommentController.kt @@ -1,16 +1,14 @@ package com.van1164.comment -import com.van1164.common.domain.Comment -import com.van1164.security.PrincipalDetails +import com.van1164.common.domain.comment.Comment +import com.van1164.common.security.PrincipalDetails import com.van1164.user.UserService import mu.KotlinLogging import org.springframework.http.ResponseEntity import org.springframework.security.access.prepost.PreAuthorize import org.springframework.security.core.annotation.AuthenticationPrincipal -import org.springframework.security.core.context.ReactiveSecurityContextHolder import org.springframework.security.core.context.SecurityContextHolder import org.springframework.web.bind.annotation.GetMapping -import org.springframework.web.bind.annotation.PatchMapping import org.springframework.web.bind.annotation.PathVariable import org.springframework.web.bind.annotation.PostMapping import org.springframework.web.bind.annotation.RequestBody diff --git a/comment/src/main/kotlin/com/van1164/comment/CommentReadRepository.kt b/comment/src/main/kotlin/com/van1164/comment/CommentReadRepository.kt index 17502b3..2e175e7 100644 --- a/comment/src/main/kotlin/com/van1164/comment/CommentReadRepository.kt +++ b/comment/src/main/kotlin/com/van1164/comment/CommentReadRepository.kt @@ -1,6 +1,5 @@ package com.van1164.comment -import com.van1164.common.domain.Comment import com.van1164.common.dto.CommentDTO import org.springframework.data.r2dbc.repository.Query import org.springframework.data.r2dbc.repository.R2dbcRepository diff --git a/comment/src/main/kotlin/com/van1164/comment/CommentRepository.kt b/comment/src/main/kotlin/com/van1164/comment/CommentRepository.kt index 35bb51d..644cb89 100644 --- a/comment/src/main/kotlin/com/van1164/comment/CommentRepository.kt +++ b/comment/src/main/kotlin/com/van1164/comment/CommentRepository.kt @@ -1,13 +1,8 @@ package com.van1164.comment -import com.van1164.common.domain.Comment -import com.van1164.common.dto.CommentDTO -import org.springframework.data.r2dbc.repository.Query +import com.van1164.common.domain.comment.Comment import org.springframework.data.r2dbc.repository.R2dbcRepository -import org.springframework.data.relational.core.sql.LockMode -import org.springframework.data.relational.repository.Lock import org.springframework.stereotype.Repository -import reactor.core.publisher.Mono @Repository interface CommentRepository : R2dbcRepository { diff --git a/comment/src/main/kotlin/com/van1164/comment/CommentService.kt b/comment/src/main/kotlin/com/van1164/comment/CommentService.kt index 44c7c86..95b8658 100644 --- a/comment/src/main/kotlin/com/van1164/comment/CommentService.kt +++ b/comment/src/main/kotlin/com/van1164/comment/CommentService.kt @@ -1,17 +1,15 @@ package com.van1164.comment import com.van1164.comment.like.CommentLikeRepository -import com.van1164.common.domain.Comment -import com.van1164.common.domain.CommentLike +import com.van1164.common.domain.comment.Comment +import com.van1164.common.domain.comment.CommentLike import com.van1164.common.util.Utils.logger import com.van1164.main.video.VideoReadRepository import org.springframework.data.r2dbc.repository.config.EnableR2dbcRepositories import org.springframework.http.ResponseEntity import org.springframework.stereotype.Service -import org.springframework.transaction.annotation.Isolation import org.springframework.transaction.annotation.Transactional import reactor.core.publisher.Mono -import io.r2dbc.spi.IsolationLevel.* import reactor.kotlin.core.publisher.toMono @Service diff --git a/comment/src/main/kotlin/com/van1164/comment/like/CommentLikeRepository.kt b/comment/src/main/kotlin/com/van1164/comment/like/CommentLikeRepository.kt index b856769..541376d 100644 --- a/comment/src/main/kotlin/com/van1164/comment/like/CommentLikeRepository.kt +++ b/comment/src/main/kotlin/com/van1164/comment/like/CommentLikeRepository.kt @@ -1,6 +1,6 @@ package com.van1164.comment.like -import com.van1164.common.domain.CommentLike +import com.van1164.common.domain.comment.CommentLike import org.springframework.data.r2dbc.repository.R2dbcRepository import reactor.core.publisher.Flux import reactor.core.publisher.Mono diff --git a/security/src/main/kotlin/com/van1164/security/JwtServerAuthenticationConverter.kt b/security/src/main/kotlin/com/van1164/security/JwtServerAuthenticationConverter.kt index 96e3d93..19710b0 100644 --- a/security/src/main/kotlin/com/van1164/security/JwtServerAuthenticationConverter.kt +++ b/security/src/main/kotlin/com/van1164/security/JwtServerAuthenticationConverter.kt @@ -12,7 +12,8 @@ class JwtServerAuthenticationConverter( ) : ServerAuthenticationConverter { override fun convert(exchange: ServerWebExchange?): Mono { return Mono.justOrEmpty(exchange?.request?.headers?.getFirst("authorization")) - .filter { it.startsWith("Bearer ") }.map { it.substring((7)) } + .filter{ it != null} + .filter { it!!.startsWith("Bearer ") }.map { it!!.substring((7)) } .filter { jwtTokenProvider.validateToken(it) } .flatMap { jwt -> jwtTokenProvider.getAuthentication(jwt) } } diff --git a/security/src/main/kotlin/com/van1164/security/JwtTokenProvider.kt b/security/src/main/kotlin/com/van1164/security/JwtTokenProvider.kt index a00c3cb..83a4d7b 100644 --- a/security/src/main/kotlin/com/van1164/security/JwtTokenProvider.kt +++ b/security/src/main/kotlin/com/van1164/security/JwtTokenProvider.kt @@ -2,8 +2,9 @@ package com.van1164.security import com.nimbusds.oauth2.sdk.Role import com.van1164.common.redis.RedisService +import com.van1164.common.security.PrincipalDetails +import com.van1164.common.security.TokenInfo import com.van1164.user.UserService -import com.van1164.util.JWT_PREFIX import io.jsonwebtoken.Claims import io.jsonwebtoken.Jwts import io.jsonwebtoken.SignatureAlgorithm @@ -12,15 +13,11 @@ import io.jsonwebtoken.security.Keys import lombok.RequiredArgsConstructor import org.springframework.beans.factory.annotation.Value import org.springframework.security.authentication.UsernamePasswordAuthenticationToken -import org.springframework.security.core.Authentication import org.springframework.security.core.GrantedAuthority import org.springframework.security.core.authority.SimpleGrantedAuthority -import org.springframework.security.core.userdetails.User -import org.springframework.security.core.userdetails.UserDetails import org.springframework.stereotype.Component import reactor.core.publisher.Mono import java.util.* -import kotlin.collections.LinkedHashMap @Component @RequiredArgsConstructor diff --git a/security/src/main/kotlin/com/van1164/security/OAuthSuccessHandlerReactive.kt b/security/src/main/kotlin/com/van1164/security/OAuthSuccessHandlerReactive.kt index b53b480..813b4c3 100644 --- a/security/src/main/kotlin/com/van1164/security/OAuthSuccessHandlerReactive.kt +++ b/security/src/main/kotlin/com/van1164/security/OAuthSuccessHandlerReactive.kt @@ -1,11 +1,11 @@ package com.van1164.security import com.van1164.common.redis.RedisR2dbcService +import com.van1164.common.security.PrincipalDetails import com.van1164.common.util.Utils.logger import com.van1164.user.UserService import org.springframework.http.HttpStatus import org.springframework.security.core.Authentication -import org.springframework.security.web.server.DefaultServerRedirectStrategy import org.springframework.security.web.server.WebFilterExchange import org.springframework.security.web.server.authentication.ServerAuthenticationSuccessHandler import org.springframework.stereotype.Component diff --git a/security/src/main/kotlin/com/van1164/security/PrincipalOauthUserServiceReactive.kt b/security/src/main/kotlin/com/van1164/security/PrincipalOauthUserServiceReactive.kt index 9ebff9b..96126e8 100644 --- a/security/src/main/kotlin/com/van1164/security/PrincipalOauthUserServiceReactive.kt +++ b/security/src/main/kotlin/com/van1164/security/PrincipalOauthUserServiceReactive.kt @@ -3,6 +3,7 @@ package com.van1164.security import com.van1164.common.dto.* import com.van1164.common.domain.UserR2dbc +import com.van1164.common.security.PrincipalDetails import com.van1164.common.util.Utils.logger import com.van1164.user.UserService import org.springframework.security.oauth2.client.userinfo.DefaultReactiveOAuth2UserService diff --git a/security/src/main/kotlin/com/van1164/security/PrincipalServiceReactive.kt b/security/src/main/kotlin/com/van1164/security/PrincipalServiceReactive.kt index d2a135a..88a3171 100644 --- a/security/src/main/kotlin/com/van1164/security/PrincipalServiceReactive.kt +++ b/security/src/main/kotlin/com/van1164/security/PrincipalServiceReactive.kt @@ -1,5 +1,6 @@ package com.van1164.security +import com.van1164.common.security.PrincipalDetails import com.van1164.user.UserService import lombok.RequiredArgsConstructor import org.springframework.context.annotation.ComponentScan diff --git a/util/src/main/kotlin/com/van1164/common/domain/SubComment.kt b/util/src/main/kotlin/com/van1164/common/domain/SubComment.kt index 67ea095..5188801 100644 --- a/util/src/main/kotlin/com/van1164/common/domain/SubComment.kt +++ b/util/src/main/kotlin/com/van1164/common/domain/SubComment.kt @@ -1,5 +1,6 @@ package com.van1164.common.domain +import com.van1164.common.domain.comment.Comment import jakarta.persistence.* import lombok.ToString import java.util.* diff --git a/util/src/main/kotlin/com/van1164/common/domain/UserSubscribe.kt b/util/src/main/kotlin/com/van1164/common/domain/UserSubscribe.kt new file mode 100644 index 0000000..1bf8d07 --- /dev/null +++ b/util/src/main/kotlin/com/van1164/common/domain/UserSubscribe.kt @@ -0,0 +1,13 @@ +package com.van1164.common.domain + +import org.springframework.data.annotation.Id + +data class UserSubscribe( + + val fromUserId : String, + + val toUserId : String, + + @Id + val id : Long? = null +) diff --git a/util/src/main/kotlin/com/van1164/common/domain/Video.kt b/util/src/main/kotlin/com/van1164/common/domain/Video.kt index a7fefa2..43f96a6 100644 --- a/util/src/main/kotlin/com/van1164/common/domain/Video.kt +++ b/util/src/main/kotlin/com/van1164/common/domain/Video.kt @@ -1,6 +1,6 @@ package com.van1164.common.domain -import com.van1164.common.domain.Comment +import com.van1164.common.domain.comment.Comment import jakarta.persistence.* import lombok.ToString import org.springframework.data.annotation.CreatedDate diff --git a/util/src/main/kotlin/com/van1164/common/domain/comment/Comment.kt b/util/src/main/kotlin/com/van1164/common/domain/comment/Comment.kt index 32d24ab..7627b75 100644 --- a/util/src/main/kotlin/com/van1164/common/domain/comment/Comment.kt +++ b/util/src/main/kotlin/com/van1164/common/domain/comment/Comment.kt @@ -1,4 +1,4 @@ -package com.van1164.common.domain +package com.van1164.common.domain.comment import org.springframework.data.annotation.CreatedDate diff --git a/util/src/main/kotlin/com/van1164/common/domain/comment/CommentBad.kt b/util/src/main/kotlin/com/van1164/common/domain/comment/CommentBad.kt index f0725d1..7243077 100644 --- a/util/src/main/kotlin/com/van1164/common/domain/comment/CommentBad.kt +++ b/util/src/main/kotlin/com/van1164/common/domain/comment/CommentBad.kt @@ -1,4 +1,4 @@ -package com.van1164.common.domain +package com.van1164.common.domain.comment import org.springframework.data.annotation.Id import org.springframework.data.relational.core.mapping.Table diff --git a/util/src/main/kotlin/com/van1164/common/domain/comment/CommentLike.kt b/util/src/main/kotlin/com/van1164/common/domain/comment/CommentLike.kt index 4468c5b..3d44565 100644 --- a/util/src/main/kotlin/com/van1164/common/domain/comment/CommentLike.kt +++ b/util/src/main/kotlin/com/van1164/common/domain/comment/CommentLike.kt @@ -1,4 +1,4 @@ -package com.van1164.common.domain +package com.van1164.common.domain.comment import org.springframework.data.annotation.Id import org.springframework.data.relational.core.mapping.Table diff --git a/util/src/main/kotlin/com/van1164/common/dto/VideoDetailResponseDTO.kt b/util/src/main/kotlin/com/van1164/common/dto/VideoDetailResponseDTO.kt index 99b0a1c..a9eddb0 100644 --- a/util/src/main/kotlin/com/van1164/common/dto/VideoDetailResponseDTO.kt +++ b/util/src/main/kotlin/com/van1164/common/dto/VideoDetailResponseDTO.kt @@ -1,6 +1,6 @@ package com.van1164.common.dto -import com.van1164.common.domain.Comment +import com.van1164.common.domain.comment.Comment data class VideoDetailResponseDTO( diff --git a/util/src/main/kotlin/com/van1164/common/exception/CustomException.kt b/util/src/main/kotlin/com/van1164/common/exception/CustomException.kt new file mode 100644 index 0000000..5226683 --- /dev/null +++ b/util/src/main/kotlin/com/van1164/common/exception/CustomException.kt @@ -0,0 +1,5 @@ +package com.van1164.common.exception + +import java.lang.Exception + +class AlreadySubscribeException : Exception() \ No newline at end of file diff --git a/util/src/main/kotlin/com/van1164/common/response/DetailResponse.kt b/util/src/main/kotlin/com/van1164/common/response/DetailResponse.kt index 50fe56b..04df438 100644 --- a/util/src/main/kotlin/com/van1164/common/response/DetailResponse.kt +++ b/util/src/main/kotlin/com/van1164/common/response/DetailResponse.kt @@ -1,6 +1,5 @@ package com.van1164.common.response -import com.van1164.common.domain.Comment import com.van1164.common.domain.VideoR2dbc import com.van1164.common.dto.CommentDTO diff --git a/util/src/main/kotlin/com/van1164/common/security/PrincipalDetails.kt b/util/src/main/kotlin/com/van1164/common/security/PrincipalDetails.kt index 3a69265..c2a121e 100644 --- a/util/src/main/kotlin/com/van1164/common/security/PrincipalDetails.kt +++ b/util/src/main/kotlin/com/van1164/common/security/PrincipalDetails.kt @@ -1,4 +1,4 @@ -package com.van1164.security +package com.van1164.common.security import com.van1164.common.domain.UserR2dbc import com.van1164.common.dto.OAuthProvider diff --git a/util/src/main/kotlin/com/van1164/common/security/TokenInfo.kt b/util/src/main/kotlin/com/van1164/common/security/TokenInfo.kt index 14f5586..496b559 100644 --- a/util/src/main/kotlin/com/van1164/common/security/TokenInfo.kt +++ b/util/src/main/kotlin/com/van1164/common/security/TokenInfo.kt @@ -1,4 +1,4 @@ -package com.van1164.security +package com.van1164.common.security data class TokenInfo( val grantType : String, diff --git a/video/src/main/kotlin/com/van1164/video/like/VideoLikeController.kt b/video/src/main/kotlin/com/van1164/video/like/VideoLikeController.kt index 83f62b6..37081a1 100644 --- a/video/src/main/kotlin/com/van1164/video/like/VideoLikeController.kt +++ b/video/src/main/kotlin/com/van1164/video/like/VideoLikeController.kt @@ -1,10 +1,8 @@ package com.van1164.video.like -import com.van1164.common.redis.RedisR2dbcRepository -import com.van1164.security.PrincipalDetails +import com.van1164.common.security.PrincipalDetails import org.springframework.http.ResponseEntity import org.springframework.security.core.annotation.AuthenticationPrincipal -import org.springframework.web.bind.annotation.GetMapping import org.springframework.web.bind.annotation.PathVariable import org.springframework.web.bind.annotation.PostMapping import org.springframework.web.bind.annotation.RequestMapping diff --git a/video/src/main/kotlin/com/van1164/video/upload/VideoController.kt b/video/src/main/kotlin/com/van1164/video/upload/VideoController.kt index 566d69d..8c29da3 100644 --- a/video/src/main/kotlin/com/van1164/video/upload/VideoController.kt +++ b/video/src/main/kotlin/com/van1164/video/upload/VideoController.kt @@ -1,5 +1,5 @@ package com.van1164.video.upload -import com.van1164.security.PrincipalDetails +import com.van1164.common.security.PrincipalDetails import com.van1164.common.dto.UploadVideoDataDTO import com.van1164.common.dto.UploadVideoPartDTO import io.swagger.v3.oas.annotations.Operation @@ -49,10 +49,10 @@ class VideoController( @ResponseBody @PostMapping("/saveVideoData") fun saveVideoData( - @AuthenticationPrincipal user : Mono, + @AuthenticationPrincipal principal : Mono, @RequestBody uploadVideoDataDTO: UploadVideoDataDTO ): Mono> { - return user.flatMap { user-> + return principal.flatMap { user-> videoService.saveVideoData(uploadVideoDataDTO.title,uploadVideoDataDTO.fileUUID,user.name) } } diff --git a/video/src/main/kotlin/com/van1164/video/view/controller/VideoViewController.kt b/video/src/main/kotlin/com/van1164/video/view/controller/VideoViewController.kt index 56fe7c2..4ee602d 100644 --- a/video/src/main/kotlin/com/van1164/video/view/controller/VideoViewController.kt +++ b/video/src/main/kotlin/com/van1164/video/view/controller/VideoViewController.kt @@ -1,6 +1,6 @@ package com.van1164.video.view.controller -import com.van1164.security.PrincipalDetails +import com.van1164.common.security.PrincipalDetails import com.van1164.video.view.service.VideoViewService import org.springframework.http.ResponseEntity import org.springframework.security.core.annotation.AuthenticationPrincipal