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
1 change: 1 addition & 0 deletions src/main/java/B1G4/bookmark/config/SecurityConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public class SecurityConfig {
"/refresh",
"/actuator/prometheus",
"/ws/**",
"/topic/**"
};

@Bean
Expand Down
26 changes: 24 additions & 2 deletions src/main/java/B1G4/bookmark/config/WebSocketConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,29 @@ public void registerStompEndpoints(StompEndpointRegistry registry) {

@Override
public void configureClientInboundChannel(ChannelRegistration registration) {
registration.interceptors(stompHandler); // StompHandler 추가
registration.interceptors(new ChannelInterceptor() {
@Override
public Message<?> preSend(Message<?> message, MessageChannel channel) {
StompHeaderAccessor accessor = StompHeaderAccessor.wrap(message);

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 " 제거
try {
Authentication authentication = jwtTokenProvider.getAuthentication(token);
accessor.setUser(authentication); // 사용자 정보 추가
} catch (Exception e) {
System.out.println("WebSocket Authentication failed: " + e.getMessage());
}
}
}
return message;
}
});
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,13 @@ private Jws<Claims> getClaims(String token) {
}

public Authentication getAuthentication(String token) {
Claims claims = Jwts.parserBuilder()
.setSigningKey(secretKey)
.build()
.parseClaimsJws(token)
.getBody();

String userId = claims.get("id", String.class);
User user = new User(userId, "", Collections.singletonList(new SimpleGrantedAuthority("ROLE_USER")));
return new UsernamePasswordAuthenticationToken(user, token, user.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);
}
}

public void validateToken(String token) {
Expand Down
Loading