-
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.
Merge pull request #26 from SafeNet-2024/feature/add-security
[feat] ์น์์ผ ์ฐ๊ฒฐ ๋ฐ ๋ฉ์์ง ์ ์ก์ JWT ํ ํฐ ๊ฒ์ฆ ๊ณผ์ ์ถ๊ฐ
- Loading branch information
Showing
6 changed files
with
128 additions
and
34 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
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
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
46 changes: 46 additions & 0 deletions
46
src/main/java/com/SafeNet/Backend/global/config/AuthChannelInterceptor.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,46 @@ | ||
package com.SafeNet.Backend.global.config; | ||
|
||
import com.SafeNet.Backend.global.auth.JwtTokenProvider; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
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.authentication.UsernamePasswordAuthenticationToken; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.Collections; | ||
|
||
// WebSocket ๋ฉ์์ง์ ํค๋์์ ACCESS_TOKEN์ ์ถ์ถํ๊ณ ๊ฒ์ฆ | ||
// ์ ํจํ ํ ํฐ์ด ์๋ ๊ฒฝ์ฐ ์ฌ์ฉ์ ์ธ์ฆ ์ ๋ณด๋ฅผ ์ค์ ํ๊ณ , ์ ํจํ์ง ์์ ๊ฒฝ์ฐ ์ฐ๊ฒฐ์ ์ฐจ๋จ | ||
@Component | ||
public class AuthChannelInterceptor implements ChannelInterceptor { | ||
|
||
private final JwtTokenProvider jwtTokenProvider; | ||
|
||
@Autowired | ||
public AuthChannelInterceptor(JwtTokenProvider jwtTokenProvider) { | ||
this.jwtTokenProvider = jwtTokenProvider; | ||
} | ||
|
||
@Override | ||
public Message<?> preSend(Message<?> message, MessageChannel channel) { | ||
StompHeaderAccessor accessor = StompHeaderAccessor.wrap(message); | ||
if (StompCommand.CONNECT.equals(accessor.getCommand())) { // CONNECT ํ๋ ์์ ์๋ฒ์ ๋ํ ์ธ์ฆ ๋ฐ ๊ธฐํ ์ค์ ๊ณผ ๊ด๋ จ๋ ์ ๋ณด๋ฅผ ์ ์กํ๊ธฐ ์ํด ์ฌ์ฉ | ||
String token = accessor.getFirstNativeHeader("ACCESS_TOKEN"); | ||
if (token != null && token.startsWith("Bearer ")) { | ||
token = token.substring(7); | ||
if (jwtTokenProvider.validateToken(token)) { | ||
String username = jwtTokenProvider.getAuthentication(token).getName(); | ||
accessor.setUser(new UsernamePasswordAuthenticationToken(username, null, Collections.emptyList())); | ||
} else { | ||
throw new IllegalArgumentException("Invalid or expired token"); | ||
} | ||
} else { | ||
throw new IllegalArgumentException("Missing or invalid ACCESS_TOKEN header"); | ||
} | ||
} | ||
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