Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions src/main/java/B1G4/bookmark/config/WebSocketConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,20 @@ public void configureClientInboundChannel(ChannelRegistration registration) {
public Message<?> preSend(Message<?> message, MessageChannel channel) {
StompHeaderAccessor accessor = StompHeaderAccessor.wrap(message);

if (StompCommand.CONNECT.equals(accessor.getCommand())) {
// STOMP 연결 요청에서 Authorization 헤더 추출
if (StompCommand.CONNECT.equals(accessor.getCommand()) ||
StompCommand.SUBSCRIBE.equals(accessor.getCommand()) ||
StompCommand.SEND.equals(accessor.getCommand())) {

String token = accessor.getFirstNativeHeader("Authorization");

if (Objects.nonNull(token) && token.startsWith("Bearer ")) {
token = token.substring(7); // "Bearer " 제거
Authentication authentication = jwtTokenProvider.getAuthentication(token);
accessor.setUser(authentication);
SecurityContextHolder.getContext().setAuthentication(authentication);
try {
Authentication authentication = jwtTokenProvider.getAuthentication(token);
accessor.setUser(authentication); // 사용자 정보 추가
} catch (Exception e) {
System.out.println("WebSocket Authentication failed: " + e.getMessage());
}
}
}
return message;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,12 @@ private Jws<Claims> getClaims(String token) {
}

public Authentication getAuthentication(String token) {
Long memberId = getId(token); // Extract member ID from JWT
// Create a UserDetails-like principal (username, password, authorities)
User principal = new User(memberId.toString(), "", Collections.singletonList(new SimpleGrantedAuthority("ROLE_USER")));

// Create an Authentication object using principal, credentials, and authorities
return new UsernamePasswordAuthenticationToken(principal, token, principal.getAuthorities());
try {
Long memberId = getId(token); // Extract member ID from JWT
User principal = new User(memberId.toString(), "", Collections.singletonList(new SimpleGrantedAuthority("ROLE_USER")));
return new UsernamePasswordAuthenticationToken(principal, token, principal.getAuthorities());
} catch (Exception e) {
throw new AuthException(ErrorStatus.AUTH_INVALID_TOKEN);
}
}
}
Loading