-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
221 additions
and
1 deletion.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
src/main/java/com/ku/covigator/config/WebSocketConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package com.ku.covigator.config; | ||
|
||
import com.ku.covigator.security.jwt.StompJwtInterceptor; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.messaging.simp.config.ChannelRegistration; | ||
import org.springframework.messaging.simp.config.MessageBrokerRegistry; | ||
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker; | ||
import org.springframework.web.socket.config.annotation.StompEndpointRegistry; | ||
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer; | ||
|
||
@Configuration | ||
@EnableWebSocketMessageBroker | ||
@RequiredArgsConstructor | ||
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer { | ||
|
||
private final StompJwtInterceptor stompJwtInterceptor; | ||
|
||
@Override | ||
public void registerStompEndpoints(StompEndpointRegistry registry) { | ||
|
||
registry.addEndpoint("/ws-chat") | ||
.setAllowedOriginPatterns("*") | ||
.withSockJS(); | ||
} | ||
|
||
@Override | ||
public void configureMessageBroker(MessageBrokerRegistry registry) { | ||
|
||
registry.enableSimpleBroker("/topic"); | ||
registry.setApplicationDestinationPrefixes("/app"); | ||
} | ||
|
||
@Override | ||
public void configureClientInboundChannel(ChannelRegistration registration) { | ||
registration.interceptors(stompJwtInterceptor); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
src/main/java/com/ku/covigator/controller/WebSocketController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.ku.covigator.controller; | ||
|
||
import com.ku.covigator.dto.request.ChatMessageRequest; | ||
import com.ku.covigator.dto.response.SaveMessageResponse; | ||
import com.ku.covigator.service.ChatService; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.messaging.handler.annotation.*; | ||
import org.springframework.messaging.simp.SimpMessageHeaderAccessor; | ||
import org.springframework.messaging.simp.SimpMessageSendingOperations; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
public class WebSocketController { | ||
|
||
private final SimpMessageSendingOperations simpleMessageSendingOperations; | ||
private final ChatService chatService; | ||
|
||
@MessageMapping("/chat/{course_id}") | ||
@SendTo("/topic/chat/{course_id}") | ||
public void sendMessage(@DestinationVariable(value = "course_id") Long courseId, | ||
SimpMessageHeaderAccessor accessor, | ||
@Payload ChatMessageRequest request) { | ||
Long memberId = Long.parseLong(accessor.getSessionAttributes().get("memberId").toString()); | ||
SaveMessageResponse saveMessageResponse = chatService.saveMessage(memberId, courseId, request.message()); | ||
simpleMessageSendingOperations.convertAndSend("/topic/chat/" + courseId, saveMessageResponse); | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
src/main/java/com/ku/covigator/dto/request/ChatMessageRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package com.ku.covigator.dto.request; | ||
|
||
public record ChatMessageRequest(String message) { | ||
|
||
} |
17 changes: 17 additions & 0 deletions
17
src/main/java/com/ku/covigator/dto/response/SaveMessageResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.ku.covigator.dto.response; | ||
|
||
import lombok.Builder; | ||
|
||
import java.sql.Timestamp; | ||
|
||
@Builder | ||
public record SaveMessageResponse(String sender, String message, String timestamp) { | ||
|
||
public static SaveMessageResponse of(String sender, String message) { | ||
return SaveMessageResponse.builder() | ||
.sender(sender) | ||
.message(message) | ||
.timestamp(String.valueOf(new Timestamp(System.currentTimeMillis()))) | ||
.build(); | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
src/main/java/com/ku/covigator/security/jwt/StompJwtInterceptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package com.ku.covigator.security.jwt; | ||
|
||
import com.ku.covigator.exception.jwt.JwtExpiredException; | ||
import com.ku.covigator.exception.jwt.JwtInvalidException; | ||
import com.ku.covigator.exception.jwt.JwtNotFoundException; | ||
import com.ku.covigator.exception.jwt.JwtUnsupportedTokenException; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.messaging.Message; | ||
import org.springframework.messaging.MessageChannel; | ||
import org.springframework.messaging.simp.stomp.StompCommand; | ||
import org.springframework.messaging.simp.stomp.StompHeaderAccessor; | ||
import org.springframework.messaging.support.ChannelInterceptor; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class StompJwtInterceptor implements ChannelInterceptor { | ||
|
||
private final JwtProvider jwtProvider; | ||
|
||
@Override | ||
public Message<?> preSend(Message<?> message, MessageChannel channel) { | ||
|
||
StompHeaderAccessor accessor = StompHeaderAccessor.wrap(message); | ||
|
||
if (accessor.getCommand() == StompCommand.CONNECT) { | ||
String authorizationHeader = accessor.getFirstNativeHeader("Authorization"); | ||
|
||
String token = jwtProvider.getTokenFromRequestHeader(authorizationHeader); | ||
|
||
if(jwtProvider.validateToken(token)) { | ||
String memberId = jwtProvider.getPrincipal(token); | ||
accessor.getSessionAttributes().put("memberId", memberId); | ||
} | ||
} | ||
|
||
return message; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters