-
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 #2 from Mojacknong/main
[feat] 스프링 클라우드 게이트웨이 필터
- Loading branch information
Showing
4 changed files
with
123 additions
and
1 deletion.
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
30 changes: 30 additions & 0 deletions
30
src/main/java/com/example/farmusgateway/config/FilterConfig.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,30 @@ | ||
package com.example.farmusgateway.config; | ||
|
||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.cloud.gateway.route.RouteLocator; | ||
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@Configuration | ||
public class FilterConfig { | ||
|
||
@Value("${baseurl.user}") | ||
private String userUrl; | ||
|
||
@Bean | ||
public RouteLocator gatewayRoutes(RouteLocatorBuilder builder) { | ||
// application.yml 에 적용한 라우팅 대신에 필터를 적용해서 처리 | ||
return builder.routes() | ||
.route(r -> r.path("/api/user/auth/logout") | ||
.filters(f -> f.addRequestHeader("user", "first-request-header")) | ||
.uri(userUrl)) | ||
|
||
.route(r -> r.path("/api/user/auth/reissue-token") | ||
.filters(f -> f.addRequestHeader("user", "second-request-header") | ||
.addRequestHeader("Authorization","sd") | ||
) | ||
.uri(userUrl)) | ||
.build(); | ||
} | ||
} |
84 changes: 84 additions & 0 deletions
84
src/main/java/com/example/farmusgateway/filter/AuthorizationHeaderFilter.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,84 @@ | ||
package com.example.farmusgateway.filter; | ||
|
||
|
||
import io.jsonwebtoken.Jwts; | ||
import lombok.Data; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.cloud.gateway.filter.GatewayFilter; | ||
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory; | ||
import org.springframework.core.env.Environment; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.server.reactive.ServerHttpRequest; | ||
import org.springframework.http.server.reactive.ServerHttpResponse; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.server.ServerWebExchange; | ||
import reactor.core.publisher.Mono; | ||
|
||
@Component | ||
@Slf4j | ||
public class AuthorizationHeaderFilter extends AbstractGatewayFilterFactory<AuthorizationHeaderFilter.Config> { | ||
|
||
@Value("${jwt.secret}") | ||
private String secret; | ||
|
||
public AuthorizationHeaderFilter(Environment env) { | ||
super(Config.class); | ||
|
||
} | ||
|
||
// login -> token -> users (with token) -> header(include token) | ||
@Override | ||
public GatewayFilter apply(Config config) { | ||
return (exchange, chain) -> { | ||
ServerHttpRequest request = exchange.getRequest(); | ||
if(!request.getHeaders().containsKey(HttpHeaders.AUTHORIZATION)) { | ||
return onError(exchange, "no authorization header", HttpStatus.UNAUTHORIZED); | ||
} | ||
|
||
String authorizationHeader = request.getHeaders().get(HttpHeaders.AUTHORIZATION).get(0); | ||
String jwt = authorizationHeader.replace("Bearer", ""); | ||
if(!isJwtValid(jwt)) { | ||
return onError(exchange, "JWT token is not valid", HttpStatus.UNAUTHORIZED); | ||
} | ||
|
||
// Custom Post Filter | ||
return chain.filter(exchange); | ||
}; | ||
} | ||
|
||
private boolean isJwtValid(String jwt) { | ||
boolean returnValue = true; | ||
|
||
String subject = null; | ||
try { | ||
subject = Jwts.parser().setSigningKey(secret) | ||
.parseClaimsJws(jwt).getBody() | ||
.getSubject(); | ||
} catch(Exception ex) { | ||
returnValue = false; | ||
} | ||
|
||
if(subject == null || subject.isEmpty()) { | ||
returnValue = false; | ||
} | ||
|
||
return returnValue; | ||
} | ||
|
||
// Mono, Flux -> Spring WebFlux | ||
private Mono<Void> onError(ServerWebExchange exchange, String error, HttpStatus httpStatus) { | ||
ServerHttpResponse response = exchange.getResponse(); | ||
response.setStatusCode(httpStatus); | ||
|
||
log.error(error); | ||
|
||
return response.setComplete(); | ||
} | ||
|
||
@Data | ||
public static class Config { | ||
|
||
} | ||
} |