-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: #202 fix logout, add gateway bearer filter, optimize imports
- Loading branch information
Showing
55 changed files
with
507 additions
and
502 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
52 changes: 52 additions & 0 deletions
52
...way/src/main/java/pl/sknikod/kodemygateway/util/AddAuthorizationGatewayFilterFactory.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,52 @@ | ||
package pl.sknikod.kodemygateway.util; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.apache.logging.log4j.util.Strings; | ||
import org.springframework.cloud.gateway.filter.GatewayFilter; | ||
import org.springframework.cloud.gateway.filter.GatewayFilterChain; | ||
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory; | ||
import org.springframework.http.HttpCookie; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.server.reactive.ServerHttpRequest; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.server.ServerWebExchange; | ||
import reactor.core.publisher.Mono; | ||
|
||
import java.util.Optional; | ||
|
||
@Component | ||
@Slf4j | ||
public class AddAuthorizationGatewayFilterFactory | ||
extends AbstractGatewayFilterFactory<Object> { | ||
|
||
public AddAuthorizationGatewayFilterFactory() { | ||
super(Object.class); | ||
} | ||
|
||
@Override | ||
public GatewayFilter apply(Object config) { | ||
return new AddAuthorizationFilter(); | ||
} | ||
|
||
@RequiredArgsConstructor | ||
private static final class AddAuthorizationFilter implements GatewayFilter { | ||
private static final String ACCESS_TOKEN_COOKIE = "AUTH_CONTEXT"; | ||
|
||
@Override | ||
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) { | ||
return Optional.ofNullable(exchange.getRequest().getCookies().getFirst(ACCESS_TOKEN_COOKIE)) | ||
.map(HttpCookie::getValue) | ||
.filter(Strings::isNotEmpty) | ||
.map(token -> { | ||
log.info("Adding {} header", HttpHeaders.AUTHORIZATION); | ||
ServerHttpRequest modifiedRequest = exchange.getRequest().mutate() | ||
.headers(httpHeaders -> httpHeaders.setBearerAuth(token)) | ||
.header(HttpHeaders.COOKIE, (String) null) | ||
.build(); | ||
return chain.filter(exchange.mutate().request(modifiedRequest).build()); | ||
}) | ||
.orElseGet(() -> chain.filter(exchange)); | ||
} | ||
} | ||
} |
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
78 changes: 78 additions & 0 deletions
78
...y-api-gateway/src/main/java/pl/sknikod/kodemygateway/util/LogoutGatewayFilterFactory.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,78 @@ | ||
package pl.sknikod.kodemygateway.util; | ||
|
||
import lombok.*; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.cloud.gateway.filter.GatewayFilter; | ||
import org.springframework.cloud.gateway.filter.GatewayFilterChain; | ||
import org.springframework.cloud.gateway.filter.NettyWriteResponseFilter; | ||
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory; | ||
import org.springframework.core.Ordered; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseCookie; | ||
import org.springframework.http.server.reactive.ServerHttpResponse; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.server.ServerWebExchange; | ||
import reactor.core.publisher.Mono; | ||
|
||
import java.util.List; | ||
|
||
import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.CLIENT_RESPONSE_CONN_ATTR; | ||
|
||
@Component | ||
@Slf4j | ||
public class LogoutGatewayFilterFactory extends AbstractGatewayFilterFactory<Object> { | ||
|
||
public LogoutGatewayFilterFactory() { | ||
super(Object.class); | ||
} | ||
|
||
@Override | ||
public GatewayFilter apply(Object config) { | ||
return new LogoutGatewayFilter(); | ||
} | ||
|
||
@Getter | ||
@Setter | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public static class Config { | ||
private String location; | ||
} | ||
|
||
@RequiredArgsConstructor | ||
private static final class LogoutGatewayFilter implements GatewayFilter, Ordered { | ||
private static final String ACCESS_TOKEN_COOKIE = "AUTH_CONTEXT"; | ||
private static final String REFRESH_TOKEN_COOKIE = "AUTH_PERSIST"; | ||
|
||
@Override | ||
public int getOrder() { | ||
return NettyWriteResponseFilter.WRITE_RESPONSE_FILTER_ORDER; | ||
} | ||
|
||
@Override | ||
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) { | ||
return chain.filter(exchange) | ||
.then(Mono.<Void>defer(() -> { | ||
if (exchange.getAttribute(CLIENT_RESPONSE_CONN_ATTR) != null) { | ||
modifyHeaders(exchange.getResponse()); | ||
} | ||
return Mono.empty(); | ||
})); | ||
} | ||
|
||
private void modifyHeaders(ServerHttpResponse response) { | ||
if (response.getStatusCode() != HttpStatus.OK) { | ||
log.warn("Response status code is {}. Execute modifyHeaders anyway", response.getStatusCode()); | ||
} | ||
response.getHeaders().addAll( | ||
HttpHeaders.SET_COOKIE, | ||
List.of(createExpiredCookie(ACCESS_TOKEN_COOKIE).toString(), createExpiredCookie(REFRESH_TOKEN_COOKIE).toString()) | ||
); | ||
} | ||
|
||
private ResponseCookie createExpiredCookie(String name) { | ||
return ResponseCookie.from(name).maxAge(0).build(); | ||
} | ||
} | ||
} |
Oops, something went wrong.