-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: support meta-annotations (#191)
* Look for annotations more robustly by using Spring `AnnotationsUtils`
- Loading branch information
1 parent
96788a2
commit bc8c32f
Showing
5 changed files
with
188 additions
and
4 deletions.
There are no files selected for viewing
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
43 changes: 43 additions & 0 deletions
43
...security/token/support/spring/validation/interceptor/SpringJwtTokenAnnotationHandler.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,43 @@ | ||
package no.nav.security.token.support.spring.validation.interceptor; | ||
|
||
import java.lang.annotation.Annotation; | ||
import java.lang.reflect.Method; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.Optional; | ||
|
||
import org.springframework.core.annotation.AnnotationUtils; | ||
|
||
import no.nav.security.token.support.core.context.TokenValidationContextHolder; | ||
import no.nav.security.token.support.core.validation.JwtTokenAnnotationHandler; | ||
|
||
public class SpringJwtTokenAnnotationHandler extends JwtTokenAnnotationHandler { | ||
|
||
|
||
public SpringJwtTokenAnnotationHandler(TokenValidationContextHolder tokenValidationContextHolder) { | ||
super(tokenValidationContextHolder); | ||
} | ||
|
||
@Override | ||
protected Annotation getAnnotation(Method method, List<Class<? extends Annotation>> types) { | ||
return Optional.ofNullable(scanAnnotation(method, types)) | ||
.orElseGet(() -> scanAnnotation(method.getDeclaringClass(), types)); | ||
} | ||
|
||
private static Annotation scanAnnotation(Method m, List<Class<? extends Annotation>> types) { | ||
return types.stream() | ||
.map(t -> AnnotationUtils.findAnnotation(m, t)) | ||
.filter(Objects::nonNull) | ||
.findFirst() | ||
.orElse(null); | ||
} | ||
|
||
private static Annotation scanAnnotation(Class<?> clazz, List<Class<? extends Annotation>> types) { | ||
return types.stream() | ||
.map(t -> AnnotationUtils.findAnnotation(clazz, t)) | ||
.filter(Objects::nonNull) | ||
.findFirst() | ||
.orElse(null); | ||
} | ||
|
||
} |
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
32 changes: 32 additions & 0 deletions
32
...est/java/no/nav/security/token/support/spring/validation/interceptor/MetaAnnotations.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,32 @@ | ||
package no.nav.security.token.support.spring.validation.interceptor; | ||
|
||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.Target; | ||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.RetentionPolicy; | ||
import no.nav.security.token.support.core.api.Protected; | ||
import no.nav.security.token.support.core.api.ProtectedWithClaims; | ||
import no.nav.security.token.support.core.api.Unprotected; | ||
|
||
@Protected | ||
@Target({ ElementType.TYPE, ElementType.METHOD }) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
|
||
@interface ProtectedMeta { | ||
|
||
} | ||
|
||
@ProtectedWithClaims(issuer = "issuer1", claimMap = { "acr=Level4" }) | ||
@Target({ ElementType.TYPE, ElementType.METHOD }) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
|
||
@interface ProtectedWithClaimsMeta { | ||
|
||
} | ||
@Unprotected | ||
@Target({ ElementType.TYPE, ElementType.METHOD }) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
|
||
@interface UnprotectedMeta { | ||
|
||
} |