-
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.
#3 [APIGateway] [Feat] UserService에 라우팅
- URL 변환 - Header에 JWT Token 전달 - Header에 UserID 전달
- Loading branch information
Showing
4 changed files
with
101 additions
and
2 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
64 changes: 64 additions & 0 deletions
64
apigateway/src/main/java/com/cloudclub/apigateway/filter/JwtAuthenticationFilter.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,64 @@ | ||
package com.cloudclub.apigateway.filter; | ||
|
||
import io.jsonwebtoken.Claims; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.cloud.gateway.filter.GatewayFilter; | ||
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.server.reactive.ServerHttpRequest; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class JwtAuthenticationFilter extends AbstractGatewayFilterFactory<JwtAuthenticationFilter.Config> { | ||
|
||
@Autowired | ||
private JwtUtil jwtUtil; | ||
|
||
public JwtAuthenticationFilter() { | ||
super(Config.class); | ||
} | ||
|
||
public static class Config { | ||
// 필터 설정이 필요한 경우 여기에 추가 | ||
} | ||
|
||
@Override | ||
public GatewayFilter apply(Config config) { | ||
return ((exchange, chain) -> { | ||
ServerHttpRequest request = exchange.getRequest(); | ||
|
||
// /login, /register 등 인증이 필요없는 경로는 패스 | ||
if (request.getURI().getPath().contains("/login") || | ||
request.getURI().getPath().contains("/register")) { | ||
return chain.filter(exchange); | ||
} | ||
|
||
// Authorization 헤더 확인 | ||
if (!request.getHeaders().containsKey(HttpHeaders.AUTHORIZATION)) { | ||
throw new RuntimeException("Missing authorization header"); | ||
} | ||
|
||
String authHeader = request.getHeaders().get(HttpHeaders.AUTHORIZATION).get(0); | ||
if (authHeader == null || !authHeader.startsWith("Bearer ")) { | ||
throw new RuntimeException("Invalid authorization header"); | ||
} | ||
|
||
// JWT 토큰 추출 및 검증 | ||
String token = authHeader.substring(7); | ||
try { | ||
Claims claims = jwtUtil.validateToken(token); | ||
|
||
// 검증된 사용자 정보를 헤더에 추가 | ||
ServerHttpRequest modifiedRequest = exchange.getRequest().mutate() | ||
.header("Authorization", authHeader) | ||
.header("X-User-Id", claims.getSubject()) | ||
.header("X-User-Role", claims.get("role", String.class)) | ||
.build(); | ||
|
||
return chain.filter(exchange.mutate().request(modifiedRequest).build()); | ||
} catch (Exception e) { | ||
throw new RuntimeException("Invalid token"); | ||
} | ||
}); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
apigateway/src/main/java/com/cloudclub/apigateway/filter/JwtUtil.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,20 @@ | ||
package com.cloudclub.apigateway.filter; | ||
|
||
import io.jsonwebtoken.Claims; | ||
import io.jsonwebtoken.Jwts; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class JwtUtil { | ||
|
||
@Value("${jwt.secret}") | ||
private String secret; | ||
|
||
public Claims validateToken(String token) { | ||
return Jwts.parser() | ||
.setSigningKey(secret) | ||
.parseClaimsJws(token) | ||
.getBody(); | ||
} | ||
} |
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