diff --git a/guides/micronaut-gateway/java/src/main/java/example/micronaut/ApiKeyTokenValidator.java b/guides/micronaut-gateway/java/src/main/java/example/micronaut/ApiKeyTokenValidator.java new file mode 100644 index 0000000000..52032f1d59 --- /dev/null +++ b/guides/micronaut-gateway/java/src/main/java/example/micronaut/ApiKeyTokenValidator.java @@ -0,0 +1,38 @@ +/* + * Copyright 2017-2024 original authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package example.micronaut; + +import io.micronaut.core.annotation.Nullable; +import io.micronaut.core.async.publisher.Publishers; +import io.micronaut.http.HttpRequest; +import io.micronaut.security.authentication.Authentication; +import io.micronaut.security.token.validator.TokenValidator; +import jakarta.inject.Singleton; +import org.reactivestreams.Publisher; +import java.util.Collections; + +@Singleton +class ApiKeyTokenValidator implements TokenValidator> { + @Override + public Publisher validateToken(String token, @Nullable HttpRequest request) { + if (token != null && token.equals("XXX")) { + return Publishers.just(Authentication.build("sherlock", Collections.singleton("ROLE_DETECTIVE"))); + } else if (token != null && token.equals("YYY")) { + return Publishers.just(Authentication.build("watson", Collections.singleton("ROLE_USER"))); + } + return Publishers.empty(); + } +} diff --git a/guides/micronaut-gateway/java/src/main/java/example/micronaut/ConfigurationRoutePredicate.java b/guides/micronaut-gateway/java/src/main/java/example/micronaut/ConfigurationRoutePredicate.java index 8cca8a590b..f86f065d7a 100644 --- a/guides/micronaut-gateway/java/src/main/java/example/micronaut/ConfigurationRoutePredicate.java +++ b/guides/micronaut-gateway/java/src/main/java/example/micronaut/ConfigurationRoutePredicate.java @@ -1,3 +1,18 @@ +/* + * Copyright 2017-2024 original authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package example.micronaut; import io.micronaut.core.annotation.Nullable; diff --git a/guides/micronaut-gateway/java/src/main/java/example/micronaut/DefaultRouteMatcher.java b/guides/micronaut-gateway/java/src/main/java/example/micronaut/DefaultRouteMatcher.java index f363d49104..9daaf804e1 100644 --- a/guides/micronaut-gateway/java/src/main/java/example/micronaut/DefaultRouteMatcher.java +++ b/guides/micronaut-gateway/java/src/main/java/example/micronaut/DefaultRouteMatcher.java @@ -1,3 +1,18 @@ +/* + * Copyright 2017-2024 original authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package example.micronaut; import io.micronaut.core.annotation.NonNull; diff --git a/guides/micronaut-gateway/java/src/main/java/example/micronaut/GatewayFilter.java b/guides/micronaut-gateway/java/src/main/java/example/micronaut/GatewayFilter.java index 07b0c36783..1a362aad19 100644 --- a/guides/micronaut-gateway/java/src/main/java/example/micronaut/GatewayFilter.java +++ b/guides/micronaut-gateway/java/src/main/java/example/micronaut/GatewayFilter.java @@ -1,5 +1,21 @@ +/* + * Copyright 2017-2024 original authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package example.micronaut; +import io.micronaut.context.annotation.Requires; import io.micronaut.core.annotation.NonNull; import io.micronaut.http.HttpRequest; import io.micronaut.http.MutableHttpRequest; @@ -9,10 +25,13 @@ import io.micronaut.http.client.ProxyHttpClient; import io.micronaut.http.filter.HttpServerFilter; import io.micronaut.http.filter.ServerFilterChain; +import io.micronaut.http.filter.ServerFilterPhase; import io.micronaut.http.uri.UriBuilder; +import io.micronaut.security.filters.SecurityFilter; import org.reactivestreams.Publisher; import java.net.URI; +@Requires(missingProperty = "framework") @Filter(ServerFilter.MATCH_ALL_PATTERN) class GatewayFilter implements HttpServerFilter { private final RouteMatcher routeMatcher; @@ -38,4 +57,9 @@ private MutableHttpRequest mutate(@NonNull Route route, @NonNull HttpRequest< mutableHttpRequest.uri(uri); return mutableHttpRequest; } + + @Override + public int getOrder() { + return ServerFilterPhase.SECURITY.order() + 100; + } } diff --git a/guides/micronaut-gateway/java/src/main/java/example/micronaut/Route.java b/guides/micronaut-gateway/java/src/main/java/example/micronaut/Route.java index 0f8808ca81..491cb8af0e 100644 --- a/guides/micronaut-gateway/java/src/main/java/example/micronaut/Route.java +++ b/guides/micronaut-gateway/java/src/main/java/example/micronaut/Route.java @@ -1,3 +1,18 @@ +/* + * Copyright 2017-2024 original authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package example.micronaut; import io.micronaut.core.annotation.NonNull; @@ -11,6 +26,9 @@ public interface Route extends Named, Ordered { @NonNull String getUri(); + @NonNull + List getRolesAllowed(); + @NonNull List getPredicates(); } diff --git a/guides/micronaut-gateway/java/src/main/java/example/micronaut/RouteConfiguration.java b/guides/micronaut-gateway/java/src/main/java/example/micronaut/RouteConfiguration.java index 7c852c85be..7cd0216230 100644 --- a/guides/micronaut-gateway/java/src/main/java/example/micronaut/RouteConfiguration.java +++ b/guides/micronaut-gateway/java/src/main/java/example/micronaut/RouteConfiguration.java @@ -1,9 +1,26 @@ +/* + * Copyright 2017-2024 original authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package example.micronaut; import io.micronaut.context.annotation.EachProperty; import io.micronaut.context.annotation.Parameter; +import io.micronaut.core.annotation.NonNull; import io.micronaut.core.convert.format.MapFormat; +import java.util.ArrayList; import java.util.List; import java.util.Map; @@ -17,6 +34,8 @@ public class RouteConfiguration implements Route { private Map predicates; + private List rolesAllowed = new ArrayList<>(); + public RouteConfiguration(@Parameter String name) { this.name = name; } @@ -40,6 +59,16 @@ public String getUri() { return uri; } + @Override + @NonNull + public List getRolesAllowed() { + return rolesAllowed; + } + + public void setRolesAllowed(@NonNull List rolesAllowed) { + this.rolesAllowed = rolesAllowed; + } + public void setUri(String uri) { this.uri = uri; } diff --git a/guides/micronaut-gateway/java/src/main/java/example/micronaut/RouteMatcher.java b/guides/micronaut-gateway/java/src/main/java/example/micronaut/RouteMatcher.java index 93e2eb961d..b64d5666d5 100644 --- a/guides/micronaut-gateway/java/src/main/java/example/micronaut/RouteMatcher.java +++ b/guides/micronaut-gateway/java/src/main/java/example/micronaut/RouteMatcher.java @@ -1,3 +1,18 @@ +/* + * Copyright 2017-2024 original authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package example.micronaut; import io.micronaut.core.annotation.NonNull; diff --git a/guides/micronaut-gateway/java/src/main/java/example/micronaut/RoutePredicate.java b/guides/micronaut-gateway/java/src/main/java/example/micronaut/RoutePredicate.java index 9335349482..24b9d8e561 100644 --- a/guides/micronaut-gateway/java/src/main/java/example/micronaut/RoutePredicate.java +++ b/guides/micronaut-gateway/java/src/main/java/example/micronaut/RoutePredicate.java @@ -1,3 +1,18 @@ +/* + * Copyright 2017-2024 original authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package example.micronaut; import io.micronaut.http.HttpRequest; diff --git a/guides/micronaut-gateway/java/src/main/java/example/micronaut/RouteSecurityRule.java b/guides/micronaut-gateway/java/src/main/java/example/micronaut/RouteSecurityRule.java new file mode 100644 index 0000000000..ff301514a0 --- /dev/null +++ b/guides/micronaut-gateway/java/src/main/java/example/micronaut/RouteSecurityRule.java @@ -0,0 +1,60 @@ +/* + * Copyright 2017-2024 original authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package example.micronaut; + +import io.micronaut.core.annotation.NonNull; +import io.micronaut.core.annotation.Nullable; +import io.micronaut.core.async.publisher.Publishers; +import io.micronaut.http.HttpRequest; +import io.micronaut.security.authentication.Authentication; +import io.micronaut.security.rules.SecurityRule; +import io.micronaut.security.rules.SecurityRuleResult; +import jakarta.inject.Singleton; +import org.reactivestreams.Publisher; + +import java.util.Optional; + +@Singleton +class RouteSecurityRule implements SecurityRule> { + + private final RouteMatcher routeMatcher; + + RouteSecurityRule(RouteMatcher routeMatcher) { + this.routeMatcher = routeMatcher; + } + + @Override + public Publisher check(@Nullable HttpRequest request, @Nullable Authentication authentication) { + return Publishers.just(securityRuleResult(request, authentication)); + } + + @NonNull + private SecurityRuleResult securityRuleResult(@Nullable HttpRequest request, @Nullable Authentication authentication) { + Optional routeOptional = routeMatcher.matches(request); + if (routeOptional.isPresent()) { + Route route = routeOptional.get(); + if (authentication == null && route.getRolesAllowed().contains(SecurityRule.IS_ANONYMOUS)) { + return SecurityRuleResult.ALLOWED; + } + if (authentication != null && authentication.getRoles() + .stream() + .anyMatch(role -> route.getRolesAllowed().contains(role))) { + return SecurityRuleResult.ALLOWED; + } + } + return SecurityRuleResult.UNKNOWN; + } +} diff --git a/guides/micronaut-gateway/java/src/test/java/example/micronaut/Book.java b/guides/micronaut-gateway/java/src/test/java/example/micronaut/Book.java index 5e4a57d812..1913eb0cc7 100644 --- a/guides/micronaut-gateway/java/src/test/java/example/micronaut/Book.java +++ b/guides/micronaut-gateway/java/src/test/java/example/micronaut/Book.java @@ -1,3 +1,18 @@ +/* + * Copyright 2017-2024 original authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package example.micronaut; import io.micronaut.serde.annotation.Serdeable; diff --git a/guides/micronaut-gateway/java/src/test/java/example/micronaut/GatewayTest.java b/guides/micronaut-gateway/java/src/test/java/example/micronaut/GatewayTest.java index 225a05f979..6fdcab315b 100644 --- a/guides/micronaut-gateway/java/src/test/java/example/micronaut/GatewayTest.java +++ b/guides/micronaut-gateway/java/src/test/java/example/micronaut/GatewayTest.java @@ -1,13 +1,32 @@ +/* + * Copyright 2017-2024 original authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package example.micronaut; import io.micronaut.context.ApplicationContext; import io.micronaut.core.type.Argument; +import io.micronaut.core.util.StringUtils; import io.micronaut.http.HttpRequest; +import io.micronaut.http.HttpResponse; +import io.micronaut.http.HttpStatus; import io.micronaut.http.client.BlockingHttpClient; import io.micronaut.http.client.HttpClient; import io.micronaut.http.client.exceptions.HttpClientException; import io.micronaut.http.client.exceptions.HttpClientResponseException; import io.micronaut.runtime.server.EmbeddedServer; +import io.micronaut.security.filters.SecurityFilter; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.function.Executable; @@ -19,39 +38,52 @@ public class GatewayTest { @Test void gateway() { - EmbeddedServer grailsServer = ApplicationContext.run(EmbeddedServer.class, Collections.singletonMap("framework", "grails")); + EmbeddedServer grailsServer = ApplicationContext.run(EmbeddedServer.class, Map.of("micronaut.security.filter.enabled", StringUtils.FALSE, "framework", "grails")); HttpClient grailsHttpClient = grailsServer.getApplicationContext().createBean(HttpClient.class, grailsServer.getURL()); BlockingHttpClient grailsClient = grailsHttpClient.toBlocking(); - EmbeddedServer micronautServer = ApplicationContext.run(EmbeddedServer.class, Collections.singletonMap("framework", "micronaut")); + EmbeddedServer micronautServer = ApplicationContext.run(EmbeddedServer.class, Map.of("micronaut.security.filter.enabled", StringUtils.FALSE,"framework", "micronaut")); HttpClient micronautHttpClient = micronautServer.getApplicationContext().createBean(HttpClient.class, micronautServer.getURL()); BlockingHttpClient micronautClient = micronautHttpClient.toBlocking(); Map configuration = Map.of( "micronaut.gateway.routes.micronaut.uri", micronautServer.getURL().toString(), + "micronaut.gateway.routes.micronaut.roles-allowed[0]", "ROLE_DETECTIVE", "micronaut.gateway.routes.micronaut.predicates[0].path", "/micronaut/books", "micronaut.gateway.routes.grails.uri", grailsServer.getURL().toString(), - "micronaut.gateway.routes.grails.predicates[0].path", "/grails/books" + "micronaut.gateway.routes.grails.predicates[0].path", "/grails/books", + "micronaut.gateway.routes.grails.roles-allowed[0]", "ROLE_DETECTIVE", + "micronaut.gateway.routes.grails.roles-allowed[1]", "ROLE_USER" ); EmbeddedServer server = ApplicationContext.run(EmbeddedServer.class, configuration); + assertTrue(server.getApplicationContext().containsBean(SecurityFilter.class)); HttpClient httpClient = server.getApplicationContext().createBean(HttpClient.class, server.getURL()); BlockingHttpClient client = httpClient.toBlocking(); - HttpRequest micronautRequest = HttpRequest.GET("/micronaut/books"); - HttpRequest grailsRequest = HttpRequest.GET("/grails/books"); + String sherlockApiKey = "XXX"; + HttpRequest micronautRequest = HttpRequest.GET("/micronaut/books").bearerAuth(sherlockApiKey); + HttpRequest grailsRequest = HttpRequest.GET("/grails/books").bearerAuth(sherlockApiKey); List books = assertDoesNotThrow(() -> grailsClient.retrieve(grailsRequest, Argument.listOf(Book.class))); assertEquals(6, books.size()); books = assertDoesNotThrow(() -> micronautClient.retrieve(micronautRequest, Argument.listOf(Book.class))); assertEquals(2, books.size()); - books = assertDoesNotThrow(() -> client.retrieve(micronautRequest, Argument.listOf(Book.class))); assertEquals(2, books.size()); books = assertDoesNotThrow(() -> client.retrieve(grailsRequest, Argument.listOf(Book.class))); assertEquals(6, books.size()); + String watsonApiKey = "YYY"; + HttpRequest watsonMicronautRequest = HttpRequest.GET("/micronaut/books").bearerAuth(watsonApiKey); + HttpClientResponseException thrown = assertThrows(HttpClientResponseException.class, () -> client.retrieve(watsonMicronautRequest, Argument.listOf(Book.class))); + assertEquals(HttpStatus.FORBIDDEN, thrown.getResponse().getStatus()); + + HttpRequest anonymousMicronautRequest = HttpRequest.GET("/micronaut/books"); + thrown = assertThrows(HttpClientResponseException.class, () -> client.retrieve(anonymousMicronautRequest, Argument.listOf(Book.class))); + assertEquals(HttpStatus.UNAUTHORIZED, thrown.getResponse().getStatus()); + grailsServer.close(); micronautServer.close(); diff --git a/guides/micronaut-gateway/java/src/test/java/example/micronaut/GrailsBooksController.java b/guides/micronaut-gateway/java/src/test/java/example/micronaut/GrailsBooksController.java index 0c3307ddd8..c13d977f03 100644 --- a/guides/micronaut-gateway/java/src/test/java/example/micronaut/GrailsBooksController.java +++ b/guides/micronaut-gateway/java/src/test/java/example/micronaut/GrailsBooksController.java @@ -1,3 +1,18 @@ +/* + * Copyright 2017-2024 original authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package example.micronaut; import io.micronaut.context.annotation.Requires; diff --git a/guides/micronaut-gateway/java/src/test/java/example/micronaut/GrailsBooksControllerTest.java b/guides/micronaut-gateway/java/src/test/java/example/micronaut/GrailsBooksControllerTest.java index 1181914c7e..49d0253594 100644 --- a/guides/micronaut-gateway/java/src/test/java/example/micronaut/GrailsBooksControllerTest.java +++ b/guides/micronaut-gateway/java/src/test/java/example/micronaut/GrailsBooksControllerTest.java @@ -1,7 +1,23 @@ +/* + * Copyright 2017-2024 original authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package example.micronaut; import io.micronaut.context.annotation.Property; import io.micronaut.core.type.Argument; +import io.micronaut.core.util.StringUtils; import io.micronaut.http.HttpRequest; import io.micronaut.http.client.BlockingHttpClient; import io.micronaut.http.client.HttpClient; @@ -14,6 +30,7 @@ import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; import static org.junit.jupiter.api.Assertions.assertEquals; +@Property(name = "micronaut.security.filter.enabled", value = StringUtils.FALSE) @Property(name = "framework", value = "grails") @MicronautTest class GrailsBooksControllerTest { diff --git a/guides/micronaut-gateway/java/src/test/java/example/micronaut/MicronautBooksController.java b/guides/micronaut-gateway/java/src/test/java/example/micronaut/MicronautBooksController.java index 24995393fb..a0a71ff5fc 100644 --- a/guides/micronaut-gateway/java/src/test/java/example/micronaut/MicronautBooksController.java +++ b/guides/micronaut-gateway/java/src/test/java/example/micronaut/MicronautBooksController.java @@ -1,3 +1,18 @@ +/* + * Copyright 2017-2024 original authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package example.micronaut; import io.micronaut.context.annotation.Requires; diff --git a/guides/micronaut-gateway/java/src/test/java/example/micronaut/MicronautBooksControllerTest.java b/guides/micronaut-gateway/java/src/test/java/example/micronaut/MicronautBooksControllerTest.java index d59b9dec1a..4d70782e6a 100644 --- a/guides/micronaut-gateway/java/src/test/java/example/micronaut/MicronautBooksControllerTest.java +++ b/guides/micronaut-gateway/java/src/test/java/example/micronaut/MicronautBooksControllerTest.java @@ -1,7 +1,23 @@ +/* + * Copyright 2017-2024 original authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package example.micronaut; import io.micronaut.context.annotation.Property; import io.micronaut.core.type.Argument; +import io.micronaut.core.util.StringUtils; import io.micronaut.http.HttpRequest; import io.micronaut.http.client.BlockingHttpClient; import io.micronaut.http.client.HttpClient; @@ -14,6 +30,7 @@ import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; import static org.junit.jupiter.api.Assertions.assertEquals; +@Property(name = "micronaut.security.filter.enabled", value = StringUtils.FALSE) @Property(name = "framework", value = "micronaut") @MicronautTest class MicronautBooksControllerTest { diff --git a/guides/micronaut-gateway/java/src/test/java/example/micronaut/RouteConfigurationTest.java b/guides/micronaut-gateway/java/src/test/java/example/micronaut/RouteConfigurationTest.java index fdefe26540..62e4558279 100644 --- a/guides/micronaut-gateway/java/src/test/java/example/micronaut/RouteConfigurationTest.java +++ b/guides/micronaut-gateway/java/src/test/java/example/micronaut/RouteConfigurationTest.java @@ -1,3 +1,18 @@ +/* + * Copyright 2017-2024 original authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package example.micronaut; import io.micronaut.context.ApplicationContext; diff --git a/guides/micronaut-gateway/java/src/test/java/example/micronaut/RouteMatcherTest.java b/guides/micronaut-gateway/java/src/test/java/example/micronaut/RouteMatcherTest.java index 35e411e447..16f3fe0194 100644 --- a/guides/micronaut-gateway/java/src/test/java/example/micronaut/RouteMatcherTest.java +++ b/guides/micronaut-gateway/java/src/test/java/example/micronaut/RouteMatcherTest.java @@ -1,3 +1,18 @@ +/* + * Copyright 2017-2024 original authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package example.micronaut; import io.micronaut.context.annotation.Property; diff --git a/guides/micronaut-gateway/metadata.json b/guides/micronaut-gateway/metadata.json index 5f12df3b65..f4e6d971a1 100644 --- a/guides/micronaut-gateway/metadata.json +++ b/guides/micronaut-gateway/metadata.json @@ -9,7 +9,7 @@ "apps": [ { "name": "default", - "features": ["http-client"] + "features": ["http-client", "security"] } ] } diff --git a/guides/micronaut-gateway/micronaut-gateway.adoc b/guides/micronaut-gateway/micronaut-gateway.adoc index d31678c8da..4c23a35ed7 100644 --- a/guides/micronaut-gateway/micronaut-gateway.adoc +++ b/guides/micronaut-gateway/micronaut-gateway.adoc @@ -38,6 +38,12 @@ and an implementation: source:DefaultRouteMatcher[] +== Security + +source:ApiKeyTokenValidator[] + +source:RouteSecurityRule[] + == Gateway Filter Create a Gateway Filter which uses a https://docs.micronaut.io/latest/guide/#proxyClient[ProxyHttpClient]: