I have to authorize requests based on the scopes it is allowed to access. I have a token-based authorization, which returns me the scopes allowed for the request. If one of the scopes matches with the scopes allowed for my API, then I allow it to access the content of the API. So, I created a custom annotation
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Authorize{
String[] scopes() default {""};
}
So, in each API, I just put the annotation above the method and match it with the scopes returned by token authorization.
My Controller1
@PostMapping("/insert")
@Authorize(scopes = {"read", "write"})
public HttpStatus create(){
// insertion code
}
@GetMapping("/students")
@Authorize(scopes = {"foo", "bar"})
public List<Student> get(){
// Get Code
}
My Controller2
@PostMapping("/insert")
@Authorize(scopes = {"read", "write"})
public HttpStatus create(){
// insertion code
}
@GetMapping("/classes")
@Authorize(scopes = {"foo", "bar"})
public List<Class> get(){
// Get Code
}
Code where I am trying to access the scopes and match:
private void validateScope(String[] scopes){
// Here 'scopes' is a string list which token authorization returned.
Method[] methods = GenericController.class.getMethods();
for(Method m: methods){
if(m.isAnnotationPresent(Authorize.class)){
Authorize auth = m.getAnnotation(Authorize.class)
for(String t: auth.scopes())
System.out.println(t);
}
}
// once I parse the corresponding scopes allowed by the API properly, then here I will match it with 'scopes'
}
This just prints out all the scopes applied to the Class. And, also I have to specify a specific Controller. I want to make it generic
How can I achieve this? I want to make the call generic so I can call any controller, and also get the scopes from the specific method, not all of them. I was thinking Google Reflection might help but I did not understand how to use it for my use case.
I have tried manipulating all the answers of Is it possible to read the value of a annotation in java? but none of them work. Any lead will be appreciated. Thanks in advance