-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
383 additions
and
7 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
guides/micronaut-gateway/java/src/main/java/example/micronaut/ApiKeyTokenValidator.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,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<HttpRequest<?>> { | ||
@Override | ||
public Publisher<Authentication> 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(); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
...s/micronaut-gateway/java/src/main/java/example/micronaut/ConfigurationRoutePredicate.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
15 changes: 15 additions & 0 deletions
15
guides/micronaut-gateway/java/src/main/java/example/micronaut/DefaultRouteMatcher.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
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
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
15 changes: 15 additions & 0 deletions
15
guides/micronaut-gateway/java/src/main/java/example/micronaut/RouteMatcher.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
15 changes: 15 additions & 0 deletions
15
guides/micronaut-gateway/java/src/main/java/example/micronaut/RoutePredicate.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
60 changes: 60 additions & 0 deletions
60
guides/micronaut-gateway/java/src/main/java/example/micronaut/RouteSecurityRule.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,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<B> implements SecurityRule<HttpRequest<B>> { | ||
|
||
private final RouteMatcher routeMatcher; | ||
|
||
RouteSecurityRule(RouteMatcher routeMatcher) { | ||
this.routeMatcher = routeMatcher; | ||
} | ||
|
||
@Override | ||
public Publisher<SecurityRuleResult> check(@Nullable HttpRequest<B> request, @Nullable Authentication authentication) { | ||
return Publishers.just(securityRuleResult(request, authentication)); | ||
} | ||
|
||
@NonNull | ||
private SecurityRuleResult securityRuleResult(@Nullable HttpRequest<?> request, @Nullable Authentication authentication) { | ||
Optional<Route> 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; | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
guides/micronaut-gateway/java/src/test/java/example/micronaut/Book.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
Oops, something went wrong.