-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #90 from fourix4/dev
feat : websocket security 구현
- Loading branch information
Showing
3 changed files
with
66 additions
and
0 deletions.
There are no files selected for viewing
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
52 changes: 52 additions & 0 deletions
52
src/main/java/com/example/CatchStudy/global/config/StompHandler.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,52 @@ | ||
package com.example.CatchStudy.global.config; | ||
|
||
import com.example.CatchStudy.domain.dto.response.Response; | ||
import com.example.CatchStudy.global.exception.CatchStudyException; | ||
import com.example.CatchStudy.global.exception.ErrorCode; | ||
import com.example.CatchStudy.global.jwt.JwtUtil; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import io.jsonwebtoken.ExpiredJwtException; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
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.security.core.Authentication; | ||
import org.springframework.security.core.context.SecurityContextHolder; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.util.StringUtils; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class StompHandler implements ChannelInterceptor { | ||
|
||
private final JwtUtil jwtUtil; | ||
|
||
@Override | ||
public Message<?> preSend(Message<?> message, MessageChannel channel) { | ||
StompHeaderAccessor accessor = StompHeaderAccessor.wrap(message); | ||
String accessToken = ""; | ||
// 연결 요청에 대해 실행 | ||
if(accessor.getCommand() == StompCommand.CONNECT) { | ||
accessToken = accessor.getFirstNativeHeader("Authorization"); | ||
} | ||
|
||
if (StringUtils.hasText(accessToken) && accessToken.startsWith("Bearer ")) { | ||
accessToken = accessToken.substring(7); | ||
} | ||
|
||
try { | ||
jwtUtil.validateAccessToken(accessToken); | ||
} catch (ExpiredJwtException e) { | ||
throw new CatchStudyException(ErrorCode.EXPIRED_ACCESS_TOKEN); | ||
} | ||
|
||
Authentication authentication = jwtUtil.getAuthentication(accessToken); | ||
SecurityContextHolder.getContext().setAuthentication(authentication); | ||
accessor.setUser(authentication); | ||
|
||
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