Skip to content

Commit e9c30d0

Browse files
author
Adnane Miliari
committed
🩹 resolve circular dependency in gateway API key authorization
1 parent 9072765 commit e9c30d0

File tree

7 files changed

+72
-91
lines changed

7 files changed

+72
-91
lines changed

feign-clients/src/main/java/dev/nano/clients/apiKeyManager/apiKey/ApiKeyManagerClient.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
@FeignClient(name = "apiKey-manager", url = "${clients.apiKey-manager.url}")
99
public interface ApiKeyManagerClient {
1010

11-
@GetMapping("/api/v1/apiKey-manager/api-keys/{apiKey}/applications/{applicationName}/authorization")
11+
@GetMapping("{apiKey}/applications/{applicationName}/authorization")
1212
ApiKeyManagerResponse isKeyAuthorizedForApplication(
1313
@PathVariable("apiKey") String apiKey,
1414
@PathVariable("applicationName") String applicationName);

gateway/src/main/java/dev/nano/gateway/GatewayApplication.java

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
@EnableDiscoveryClient
1212
@EnableFeignClients(
1313
basePackages = "dev.nano.clients"
14+
1415
)
1516
@PropertySources({
1617
@PropertySource("classpath:clients-${spring.profiles.active}.properties")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package dev.nano.gateway;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.Configuration;
5+
import org.springframework.http.codec.ServerCodecConfigurer;
6+
7+
@Configuration
8+
public class GatewayConfig {
9+
10+
@Bean
11+
public ServerCodecConfigurer serverCodecConfigurer() {
12+
return ServerCodecConfigurer.create();
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package dev.nano.gateway.security;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.beans.factory.annotation.Qualifier;
5+
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
6+
import org.springframework.cloud.gateway.filter.GlobalFilter;
7+
import org.springframework.cloud.gateway.route.Route;
8+
import org.springframework.cloud.gateway.support.ServerWebExchangeUtils;
9+
import org.springframework.context.annotation.Lazy;
10+
import org.springframework.core.Ordered;
11+
import org.springframework.http.HttpStatus;
12+
import org.springframework.stereotype.Component;
13+
import org.springframework.web.server.ResponseStatusException;
14+
import org.springframework.web.server.ServerWebExchange;
15+
import reactor.core.publisher.Mono;
16+
17+
import java.util.List;
18+
19+
@Component
20+
public class ApiAuthorizationFilter implements GlobalFilter, Ordered {
21+
22+
final ApiKeyAuthorizationChecker apiKeyAuthorizationChecker;
23+
24+
@Autowired
25+
public ApiAuthorizationFilter(
26+
@Qualifier("main-checker") @Lazy ApiKeyAuthorizationChecker apiKeyAuthorizationChecker
27+
) {
28+
this.apiKeyAuthorizationChecker = apiKeyAuthorizationChecker;
29+
}
30+
31+
@Override
32+
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
33+
34+
Route route = exchange.getAttribute(ServerWebExchangeUtils.GATEWAY_ROUTE_ATTR);
35+
String applicationName = route.getId();
36+
List<String> apiKey = exchange.getRequest().getHeaders().get("ApiKey");
37+
38+
if (applicationName == null || apiKey.isEmpty()) {
39+
throw new ResponseStatusException(
40+
HttpStatus.UNAUTHORIZED,
41+
"Application name is not defined, you are not authorized to access this resource"
42+
);
43+
}
44+
45+
return chain.filter(exchange);
46+
}
47+
48+
@Override
49+
public int getOrder() {
50+
return Ordered.LOWEST_PRECEDENCE; // lowest priority filter
51+
}
52+
}

gateway/src/main/java/dev/nano/gateway/security/ApiKeyAuthenticationFilter.java

-89
This file was deleted.

gateway/src/main/java/dev/nano/gateway/security/ApiKeyManagerAuthorizationChecker.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
* 4. Handle service failures with circuit breaker
2424
*/
2525

26-
@Service("main-checker-manager")
26+
@Service("main-checker")
2727
@AllArgsConstructor
2828
@Slf4j
2929
public class ApiKeyManagerAuthorizationChecker implements ApiKeyAuthorizationChecker {

gateway/src/main/resources/application.yml

+3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ server:
66
spring:
77
application:
88
name: gateway
9+
main:
10+
web-application-type: reactive
11+
allow-bean-definition-overriding: true
912
cloud:
1013
gateway:
1114
routes:

0 commit comments

Comments
 (0)