Skip to content

Commit

Permalink
Merge pull request #319 from fourix4/dev
Browse files Browse the repository at this point in the history
refactor : STOMP Header JWT Token 변경
  • Loading branch information
TaeHoon0 authored Aug 25, 2024
2 parents a35f9f5 + 53ca4fd commit 842a933
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class MessageController {
@MessageMapping("/{chatRoomId}/chat")
@SendTo("/sub/{chatRoomId}/chat")
public MessageResponseDto createMessage(@DestinationVariable long chatRoomId, MessageRequestDto messageRequestDto,
@Header("userId") Long userId) {
return chatService.createMessage(chatRoomId, messageRequestDto, userId);
@Header("Authorization") String token) {
return chatService.createMessage(chatRoomId, messageRequestDto, token);
}
}
4 changes: 2 additions & 2 deletions src/main/java/com/example/CatchStudy/global/jwt/JwtUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,11 @@ public Authentication getAuthentication(String accessToken) {
return new UsernamePasswordAuthenticationToken(user, accessToken, authorities);
}

public String getEmailFromRefreshToken(String refreshToken) {
public String getEmailFromJwtToken(String token) {
Claims claims = Jwts.parserBuilder()
.setSigningKey(key)
.build()
.parseClaimsJws(refreshToken)
.parseClaimsJws(token)
.getBody();

return claims.getSubject();
Expand Down
23 changes: 13 additions & 10 deletions src/main/java/com/example/CatchStudy/service/ChatService.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.example.CatchStudy.global.enums.Author;
import com.example.CatchStudy.global.exception.CatchStudyException;
import com.example.CatchStudy.global.exception.ErrorCode;
import com.example.CatchStudy.global.jwt.JwtUtil;
import com.example.CatchStudy.repository.*;
import lombok.RequiredArgsConstructor;
import org.springframework.context.event.EventListener;
Expand All @@ -34,7 +35,8 @@ public class ChatService {
private final StudyCafeRepository studyCafeRepository;
private final UsersService usersService;
private final ChatNotificationRepository chatNotificationRepository;
private final Map<Long, Map<String, Long>> chatRoomMap = new ConcurrentHashMap<>(); // chatRoomId, <session id, 접속한 유저 id>
private final JwtUtil jwtUtil;
private final Map<Long, Map<String, String>> chatRoomMap = new ConcurrentHashMap<>(); // chatRoomId, <session id, 접속한 유저 email>
private final Map<String, Long> sessionToChatRoom = new ConcurrentHashMap<>();

@Transactional
Expand Down Expand Up @@ -99,9 +101,11 @@ public List<MessageResponseDto> getMessageList(long chatRoomId) {
}

@Transactional
public MessageResponseDto createMessage(long chatRoomId, MessageRequestDto messageRequestDto, long userId) {
Users user = usersRepository.findById(userId).
orElseThrow(() -> new CatchStudyException(ErrorCode.USER_NOT_FOUND));
public MessageResponseDto createMessage(long chatRoomId, MessageRequestDto messageRequestDto, String token) {

String accessToken = token.substring(7);
String email = jwtUtil.getEmailFromJwtToken(accessToken);
Users user = usersRepository.findByEmail(email);
ChatRoom chatRoom = chatRoomRepository.findById(chatRoomId).
orElseThrow(() -> new CatchStudyException(ErrorCode.CHATROOM_NOT_FOUND));

Expand All @@ -127,10 +131,11 @@ public void handleSessionConnect(SessionConnectEvent event) {

long chatRoomId = Long.parseLong((String) ((List) accessor.getNativeHeader("chatRoomId")).get(0));
String sessionId = accessor.getSessionId();
long userId = Long.parseLong((String) ((List) accessor.getNativeHeader("userId")).get(0));
String accessToken = ((String) ((List) accessor.getNativeHeader("Authorization")).get(0));
String email = jwtUtil.getEmailFromJwtToken(accessToken);

Map<String, Long> userMap = chatRoomMap.getOrDefault(chatRoomId, new HashMap<>());
userMap.put(sessionId, userId);
Map<String, String> userMap = chatRoomMap.getOrDefault(chatRoomId, new HashMap<>());
userMap.put(sessionId, email);

chatRoomMap.put(chatRoomId, userMap);
sessionToChatRoom.put(sessionId, chatRoomId);
Expand All @@ -142,13 +147,11 @@ public void handleSessionDisconnect(SessionDisconnectEvent event) {
String sessionId = accessor.getSessionId();
Long chatRoomId = sessionToChatRoom.get(sessionId);

Map<String, Long> userMap = chatRoomMap.get(chatRoomId);
Map<String, String> userMap = chatRoomMap.get(chatRoomId);
userMap.remove(sessionId);
sessionToChatRoom.remove(sessionId);

if (userMap.isEmpty()) chatRoomMap.remove(chatRoomId);
else chatRoomMap.put(chatRoomId, userMap);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

@Slf4j
@Service
@RequiredArgsConstructor
Expand All @@ -41,7 +39,7 @@ public AccessTokenResponseDto reissuanceAccessToken(String token) {
if(refreshToken == null) throw new CatchStudyException(ErrorCode.EXPIRED_LOGIN_ERROR);

refreshTokenRepository.delete(accessToken);
String email = jwtUtil.getEmailFromRefreshToken(refreshToken);
String email = jwtUtil.getEmailFromJwtToken(refreshToken);
Users user = usersRepository.findByEmail(email);

JwtToken jwtToken = jwtUtil.generatedToken(user.getEmail(), user.getAuthor());
Expand Down

0 comments on commit 842a933

Please sign in to comment.