Skip to content

Commit

Permalink
Merge branch 'main' into feat/mypage
Browse files Browse the repository at this point in the history
  • Loading branch information
alpakar02 authored Jan 22, 2025
2 parents 73747f0 + 6d0480e commit 5a8bcb9
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 15 deletions.
4 changes: 2 additions & 2 deletions src/main/kotlin/com/toyProject7/karrot/SecurityConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import org.springframework.context.annotation.Configuration
import org.springframework.http.HttpStatus
import org.springframework.security.config.annotation.web.builders.HttpSecurity
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity
import org.springframework.security.oauth2.client.web.OAuth2LoginAuthenticationFilter
import org.springframework.security.web.SecurityFilterChain
import org.springframework.security.web.authentication.HttpStatusEntryPoint
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter
import org.springframework.web.cors.CorsConfiguration
import org.springframework.web.cors.CorsConfigurationSource
import org.springframework.web.cors.UrlBasedCorsConfigurationSource
Expand Down Expand Up @@ -51,7 +51,7 @@ class SecurityConfig(
}
.successHandler(customAuthenticationSuccessHandler)
}
.addFilterBefore(jwtAuthenticationFilter, UsernamePasswordAuthenticationFilter::class.java)
.addFilterBefore(jwtAuthenticationFilter, OAuth2LoginAuthenticationFilter::class.java)
.build()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,21 @@ class ArticleController(
fun postArticle(
@RequestBody request: PostArticleRequest,
@AuthUser user: User,
): ResponseEntity<Article> {
): ResponseEntity<ArticleResponse> {
val article = articleService.postArticle(request, user.id)
return ResponseEntity.ok(article)
val chattingUsers = articleService.getChattingUsersByArticle(article)
return ResponseEntity.ok(ArticleResponse(article, chattingUsers))
}

@PutMapping("/item/edit/{articleId}")
fun editArticle(
@RequestBody request: PostArticleRequest,
@PathVariable articleId: Long,
@AuthUser user: User,
): ResponseEntity<Article> {
): ResponseEntity<ArticleResponse> {
val article = articleService.editArticle(articleId, request, user.id)
return ResponseEntity.ok(article)
val chattingUsers = articleService.getChattingUsersByArticle(article)
return ResponseEntity.ok(ArticleResponse(article, chattingUsers))
}

@DeleteMapping("/item/delete/{articleId}")
Expand Down Expand Up @@ -80,9 +82,10 @@ class ArticleController(
fun getArticle(
@PathVariable articleId: Long,
@AuthUser user: User,
): ResponseEntity<Article> {
): ResponseEntity<ArticleResponse> {
val article = articleService.getArticle(articleId, user.id)
return ResponseEntity.ok(article)
val chattingUsers = articleService.getChattingUsersByArticle(article)
return ResponseEntity.ok(ArticleResponse(article, chattingUsers))
}

@GetMapping("/home")
Expand Down Expand Up @@ -149,3 +152,8 @@ data class PostArticleRequest(
data class UpdateStatusRequest(
val status: Int,
)

data class ArticleResponse(
val article: Article,
val chattingUsers: List<User>,
)
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@ import com.toyProject7.karrot.article.persistence.ArticleEntity
import com.toyProject7.karrot.article.persistence.ArticleLikesEntity
import com.toyProject7.karrot.article.persistence.ArticleLikesRepository
import com.toyProject7.karrot.article.persistence.ArticleRepository
import com.toyProject7.karrot.chatRoom.controller.ChatRoom
import com.toyProject7.karrot.chatRoom.persistence.ChatRoomEntity
import com.toyProject7.karrot.chatRoom.persistence.ChatRoomRepository
import com.toyProject7.karrot.image.persistence.ImageUrlEntity
import com.toyProject7.karrot.image.service.ImageService
import com.toyProject7.karrot.user.controller.User
import com.toyProject7.karrot.user.service.UserService
import org.springframework.context.annotation.Lazy
import org.springframework.data.repository.findByIdOrNull
Expand All @@ -25,6 +29,7 @@ class ArticleService(
private val articleLikesRepository: ArticleLikesRepository,
private val userService: UserService,
@Lazy private val imageService: ImageService,
private val chatRoomRepository: ChatRoomRepository,
) {
@Transactional
fun postArticle(
Expand Down Expand Up @@ -254,4 +259,11 @@ class ArticleService(
fun getItemCount(id: String): Int {
return articleRepository.countBySellerId(id)
}

fun getChattingUsersByArticle(article: Article): List<User> {
val chatRoomEntities: List<ChatRoomEntity> = chatRoomRepository.findAllByArticleId(article.id)
return chatRoomEntities
.map { chatRoomEntity -> ChatRoom.fromEntity(chatRoomEntity, "") }
.map { chatRoom -> chatRoom.buyer }
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.toyProject7.karrot.chatRoom.controller

import com.toyProject7.karrot.article.controller.Article
import com.toyProject7.karrot.chatRoom.service.ChatRoomService
import com.toyProject7.karrot.user.AuthUser
import com.toyProject7.karrot.user.controller.User
Expand Down Expand Up @@ -32,9 +33,9 @@ class ChatRoomController(
@PathVariable chatRoomId: Long,
@AuthUser user: User,
@RequestParam("createdAt") createdAt: Instant,
): ResponseEntity<List<ChatMessage>> {
val chatRoom = chatRoomService.getChatRoom(chatRoomId, user, createdAt)
return ResponseEntity.ok(chatRoom)
): ResponseEntity<ChatRoomResponse> {
val chatRoomResponse = chatRoomService.getChatRoom(chatRoomId, user, createdAt)
return ResponseEntity.ok(chatRoomResponse)
}

@PostMapping("/chat/create")
Expand All @@ -51,3 +52,8 @@ data class CreateChatRoomRequest(
val sellerId: String,
val buyerId: String,
)

data class ChatRoomResponse(
val article: Article,
val messages: List<ChatMessage>,
)
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import org.springframework.data.jpa.repository.JpaRepository
import java.time.Instant

interface ChatMessageRepository : JpaRepository<ChatMessageEntity, Long> {
fun findTop10ByChatRoomIdAndCreatedAtBeforeOrderByCreatedAtDesc(
fun findTop30ByChatRoomIdAndCreatedAtBeforeOrderByCreatedAtDesc(
chatRoomId: Long,
createdAt: Instant,
): List<ChatMessageEntity>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ interface ChatRoomRepository : JpaRepository<ChatRoomEntity, Long> {
buyerId: String,
updatedAt: Instant,
): List<ChatRoomEntity>

fun findAllByArticleId(articleId: Long): List<ChatRoomEntity>
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.toyProject7.karrot.chatRoom.ChatRoomNotFoundException
import com.toyProject7.karrot.chatRoom.ThisRoomIsNotYoursException
import com.toyProject7.karrot.chatRoom.controller.ChatMessage
import com.toyProject7.karrot.chatRoom.controller.ChatRoom
import com.toyProject7.karrot.chatRoom.controller.ChatRoomResponse
import com.toyProject7.karrot.chatRoom.persistence.ChatMessageEntity
import com.toyProject7.karrot.chatRoom.persistence.ChatMessageRepository
import com.toyProject7.karrot.chatRoom.persistence.ChatRoomEntity
Expand Down Expand Up @@ -62,17 +63,21 @@ class ChatRoomService(
chatRoomId: Long,
user: User,
createdAt: Instant,
): List<ChatMessage> {
): ChatRoomResponse {
val chatRoomEntity = chatRoomRepository.findById(chatRoomId).orElseThrow { ChatRoomNotFoundException() }
val chatRoom = ChatRoom.fromEntity(chatRoomEntity, "")
if (chatRoom.buyer != user && chatRoom.seller != user) throw ThisRoomIsNotYoursException()

val chatMessageEntities: List<ChatMessageEntity> =
chatMessageRepository.findTop10ByChatRoomIdAndCreatedAtBeforeOrderByCreatedAtDesc(
chatMessageRepository.findTop30ByChatRoomIdAndCreatedAtBeforeOrderByCreatedAtDesc(
chatRoomId = chatRoomId,
createdAt = createdAt,
)
return chatMessageEntities.map { chatMessageEntity -> ChatMessage.fromEntity(chatMessageEntity) }
val messages = chatMessageEntities.map { chatMessageEntity -> ChatMessage.fromEntity(chatMessageEntity) }
return ChatRoomResponse(
article = chatRoom.article,
messages = messages,
)
}

@Transactional
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import jakarta.servlet.http.HttpServletRequest
import jakarta.servlet.http.HttpServletResponse
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken
import org.springframework.security.core.context.SecurityContextHolder
import org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken
import org.springframework.security.web.authentication.WebAuthenticationDetailsSource
import org.springframework.security.web.util.matcher.AntPathRequestMatcher
import org.springframework.stereotype.Component
Expand Down Expand Up @@ -67,6 +68,28 @@ class JwtAuthenticationFilter(
response.status = HttpServletResponse.SC_UNAUTHORIZED // Set 401 status
return
}
} else if (SecurityContextHolder.getContext().authentication is OAuth2AuthenticationToken) {
// Fallback: Handle cases where OAuth2AuthenticationToken is still present
logger.debug("OAuth2AuthenticationToken detected; forcing JWT authentication fallback")

// Force re-authentication based on the token in the Authorization header
if (authHeader != null && authHeader.startsWith("Bearer ")) {
val token = authHeader.substring(7)
if (UserAccessTokenUtil.validateToken(token)) {
val userId = UserAccessTokenUtil.getUserIdFromToken(token)
val userDetails = userService.loadUserPrincipalById(userId)

val authentication =
UsernamePasswordAuthenticationToken(
userDetails,
null,
userDetails.authorities,
)
authentication.details = WebAuthenticationDetailsSource().buildDetails(request)
SecurityContextHolder.getContext().authentication = authentication
logger.debug("Re-authentication completed for user: $userId")
}
}
}

// Continue the filter chain regardless of authentication
Expand Down

0 comments on commit 5a8bcb9

Please sign in to comment.