Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/#769 refarch refactoring #1207

Merged
merged 5 commits into from
Jan 18, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ management:
enabled-by-default: false
web:
exposure:
include: 'health, info, prometheus'
include: 'health, info, prometheus, livenessstate, readinessstate'
path-mapping:
prometheus: metrics
endpoint:
Expand All @@ -37,6 +37,7 @@ server:
include-stacktrace: never
whitelabel:
enabled: false
shutdown: graceful

spring:
application:
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ management:
enabled-by-default: false
web:
exposure:
include: 'health, info, prometheus'
include: 'health, info, prometheus, livenessstate, readinessstate'
path-mapping:
prometheus: metrics
endpoint:
Expand Down Expand Up @@ -61,6 +61,7 @@ server:
include-stacktrace: never
whitelabel:
enabled: false
shutdown: graceful
spring:
application:
name: ${info.application.name}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ public SecurityWebFilterChain clientAccessFilterChain(ServerHttpSecurity http) {
public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
http
.logout(logoutSpec -> {
//.logoutSuccessHandler(GatewayUtils.createLogoutSuccessHandler(LOGOUT_SUCCESS_URL))
logoutSpec.logoutSuccessHandler(new HttpStatusReturningServerLogoutSuccessHandler())
logoutSpec.logoutSuccessHandler(new HttpStatusReturningServerLogoutSuccessHandler())
.logoutUrl(LOGOUT_URL)
.requiresLogout(ServerWebExchangeMatchers.pathMatchers(HttpMethod.POST, LOGOUT_URL));
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,24 @@
*/
package de.muenchen.oss.digiwf.gateway.filter;

import de.muenchen.oss.digiwf.gateway.util.GatewayUtils;
import com.hazelcast.org.apache.commons.codec.binary.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.reactivestreams.Publisher;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.DataBufferFactory;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.MediaType;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.http.server.reactive.ServerHttpResponseDecorator;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;


Expand All @@ -29,20 +38,57 @@ public class GlobalAuthenticationErrorFilter implements GlobalFilter, Ordered {

private static final String GENERIC_AUTHENTICATION_ERROR = "{ \"status\":401, \"error\":\"Authentication Error\" }";

public static final int ORDER_GLOBAL_FILTER = -3;

@Override
public int getOrder() {
return GatewayUtils.ORDER_GLOBAL_FILTER;
return ORDER_GLOBAL_FILTER;
}

@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
public Mono<Void> filter(final ServerWebExchange exchange, final GatewayFilterChain chain) {
log.debug("Check for authentication errors");
return GatewayUtils.responseBodyManipulatorForServerWebExchange(
exchange,
chain,
HttpStatus.UNAUTHORIZED,
GENERIC_AUTHENTICATION_ERROR
);
}
final HttpStatus httpStatus = HttpStatus.UNAUTHORIZED;
final String newResponseBody = GENERIC_AUTHENTICATION_ERROR;
final String EMPTY_JSON_OBJECT = "{}";

final ServerHttpResponse response = exchange.getResponse();

final ServerHttpResponseDecorator decoratedResponse = new ServerHttpResponseDecorator(response) {

/**
* This overridden method adds the response body given in the parameter of
* the surrounding method when the http status given in the parameter of
* the surrounding method is met otherwise the already appended body will be used.
*
* @param body The body received by the upstream response.
* @return Either the body received by the upstream response or
* the body given by the parameter.
*/
@Override
public Mono<Void> writeWith(Publisher<? extends DataBuffer> body) {
final HttpStatusCode responseHttpStatus = getDelegate().getStatusCode();
if (body instanceof Flux && responseHttpStatus.equals(httpStatus)) {
final DataBufferFactory dataBufferFactory = response.bufferFactory();
final DataBuffer newDataBuffer = dataBufferFactory.wrap(
StringUtils.getBytesUtf8(ObjectUtils.defaultIfNull(newResponseBody, EMPTY_JSON_OBJECT)));

log.debug("Response from upstream {} get new response body: {}", httpStatus, newResponseBody);
getDelegate().getHeaders().setContentLength(newDataBuffer.readableByteCount());
getDelegate().getHeaders().setContentType(MediaType.APPLICATION_JSON);
Flux<? extends DataBuffer> flux = (Flux<? extends DataBuffer>) body;

return super.writeWith(flux.buffer().map(
// replace old body represented by dataBuffer by the new one
dataBuffer -> newDataBuffer));
}
return super.writeWith(body);
}

};

final ServerWebExchange swe = exchange.mutate().response(decoratedResponse).build();
return chain.filter(swe);
};

}
Loading
Loading