Skip to content

Commit

Permalink
🔐add configurable API key validation with enable/disable toggle in ga…
Browse files Browse the repository at this point in the history
…teway service
  • Loading branch information
Adnane Miliari committed Nov 30, 2024
1 parent 6059d11 commit 283e77b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.cloud.gateway.route.Route;
Expand All @@ -19,6 +20,9 @@
@Component
public class ApiAuthorizationFilter implements GlobalFilter, Ordered {

@Value("${spring.security.api-key.enabled:true}")
private boolean apiKeyEnabled;

final ApiKeyAuthorizationChecker apiKeyAuthorizationChecker;

@Autowired
Expand All @@ -30,6 +34,9 @@ public ApiAuthorizationFilter(

@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
if (!apiKeyEnabled) {
return chain.filter(exchange);
}

Route route = exchange.getAttribute(ServerWebExchangeUtils.GATEWAY_ROUTE_ATTR);
String applicationName = route.getId();
Expand All @@ -47,6 +54,6 @@ public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {

@Override
public int getOrder() {
return Ordered.LOWEST_PRECEDENCE; // lowest priority filter
return Ordered.LOWEST_PRECEDENCE;
}
}
3 changes: 3 additions & 0 deletions gateway/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ spring:
main:
web-application-type: reactive
allow-bean-definition-overriding: true
security:
api-key:
enabled: false
cloud:
gateway:
routes:
Expand Down

0 comments on commit 283e77b

Please sign in to comment.