Skip to content

Commit

Permalink
[debug] #71 발신자 닉네임 변경 안되는 문제
Browse files Browse the repository at this point in the history
  • Loading branch information
hyeesw committed Oct 1, 2024
1 parent 7e875cb commit 22e183d
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,13 @@ public UserDetails loadUserByUsername(String username) throws UsernameNotFoundEx
.orElseGet(() -> userRepository.findByAppleId(username)
.orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND)));

return org.springframework.security.core.userdetails.User.builder()
UserDetails userDetails = org.springframework.security.core.userdetails.User.builder()
.username(username)
.password("unused")
.authorities("ROLE_USER")
.build();

log.trace("CustomUserDetailsService > loadUserByUsername() > userDetails: {}", userDetails);
return userDetails;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,6 @@ public class MessageController {
private final RequestValidate requestValidate;
private final GoalService goalService;

// HTTP 단방향 채팅 저장
@Operation(summary="메세지 저장 (HTTP)", description="임시 채팅 저장 API 입니다. opentalkId, type, text 를 입력으로 받아 저장 결과를 반환합니다.",
parameters = {@Parameter(name = "opentalkId", description = "오픈톡 DB ID"), @Parameter(name = "type", description = "메세지 타입 (text, img)"), @Parameter(name = "content", description = "메세지 내용(goal 인 경우 null)")},
responses = {@ApiResponse(responseCode = "200", content = @Content(schema = @Schema(implementation = MessageResponseDto.class)),
description = MessageResponseDto.description)})
@PostMapping("/api/message/save")
public ResponseEntity<?> httpChat(@RequestParam Long opentalkId, String type, String content){
requestValidate.isValidType(type);

MessageResponseDto response = messageService.saveHttpMessage(opentalkId, type, content);
return responseTemplate.success(response, HttpStatus.OK);
}

// 목표 공유하기
@Operation(summary="목표 공유하기", description="opentalkId, isbn 을 입력으로 받아 해당 목표가 있는지 확인 후 채팅방에 전송합니다.",
parameters = {@Parameter(name = "opentalkId", description = "오픈톡 DB ID"), @Parameter(name = "isbn", description = "책 ISBN")},
Expand Down Expand Up @@ -86,6 +73,8 @@ public ResponseEntity<?> getChat(@RequestParam Long opentalkId, int pageNo, int
// 채팅 저장하기 (apic 으로 테스트)
@MessageMapping("/message")
public void chat(MessageRequestDto messageRequestDto) {
log.trace("---------------------------------------------");
log.trace("stomp URL : /pub/message"); // Endpoint URL 로깅
RequestLogger.body(messageRequestDto);
MessageResponseDto response = messageService.saveMessage(messageRequestDto);
sendingOperations.convertAndSend("/sub/message/" + messageRequestDto.getOpentalkId(), response); // 수신자들에게 전송
Expand All @@ -102,4 +91,25 @@ public void chat(MessageRequestDto messageRequestDto) {
public void chatForSwagger(MessageRequestDto messageRequestDto) {
return;
}

// HTTP 단방향 채팅 저장
@Operation(summary="메세지 저장 (HTTP)", description="임시 채팅 저장 API 입니다. opentalkId, type, text 를 입력으로 받아 저장 결과를 반환합니다.",
parameters = {@Parameter(name = "opentalkId", description = "오픈톡 DB ID"), @Parameter(name = "type", description = "메세지 타입 (text, img)"), @Parameter(name = "content", description = "메세지 내용(goal 인 경우 null)")},
responses = {@ApiResponse(responseCode = "200", content = @Content(schema = @Schema(implementation = MessageResponseDto.class)),
description = MessageResponseDto.description)})

@PostMapping("/api/message/save")
public ResponseEntity<?> httpChat(@RequestParam Long opentalkId, String type, String content, String jwtToken){
requestValidate.isValidType(type);
MessageRequestDto messageRequestDto = MessageRequestDto.builder()
.opentalkId(opentalkId)
.type(type)
.content(content)
.jwtToken(jwtToken)
.build();
MessageResponseDto response = messageService.saveMessage(messageRequestDto);

// MessageResponseDto response = messageService.saveHttpMessage(opentalkId, type, content);
return responseTemplate.success(response, HttpStatus.OK);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ public MessageResponseDto saveMessage(MessageRequestDto messageRequestDto){
validateToken(token);

// message DB에 저장
log.trace("saveMessage() 유저 이름 : {}", userService.loadLoggedinUser());
Message message = messageMapper.convertToMessage(messageRequestDto);
try{
messageRepository.save(message);
Expand All @@ -105,6 +106,7 @@ public MessageResponseDto saveMessage(MessageRequestDto messageRequestDto){


public void validateToken(String token) {
log.trace("MessageService > validateToken()");
try{
String username = jwtUtil.getUsernameFromToken(token); // username 가져옴
// 현재 SecurityContextHolder에 인증객체가 있는지 확인
Expand All @@ -113,10 +115,13 @@ public void validateToken(String token) {
userDetails = userDetailsService.loadUserByUsername(username);

// 토큰 유효성 검증
if (jwtUtil.isValidToken(token, userDetails)) {
if (!jwtUtil.isValidToken(token, userDetails)) {
throw new CustomException(ErrorCode.WRONG_JWT_TOKEN);
} else {
UsernamePasswordAuthenticationToken authenticated
= new UsernamePasswordAuthenticationToken(userDetails, null, userDetails.getAuthorities());
SecurityContextHolder.getContext().setAuthentication(authenticated);
log.trace("validateToken() 유저 이름 : {}", userService.loadLoggedinUser());
}
}
} catch (ExpiredJwtException e) {
Expand Down

0 comments on commit 22e183d

Please sign in to comment.