I'm developing some social system, and I want make some annotation which automatically inject loggedUser to method parameter.
I've tried to use a AOP but official documentation says:
Return types are restricted to primitives, String, Class, enums, annotations, and arrays of the preceding types. Methods can have default values.
public void getLoggedUserDetails( @CurrentUser User user){
//some logic
}
It it possible to do that with Aspects and annotaion or I should look for another solution?
there is a @AuthenticationPrincipal
and it works for controller method parameters. see here
@Controller
class UserController {
@GetMapping("/self")
public ResponseEntity<User> demo(@AuthenticationPrincipal User user){
return new ResponseEntity<>(user, HttpStatus.OK);
}
You can also create a custom annotation:
@Target({ElementType.PARAMETER, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@AuthenticationPrincipal
public @interface CurrentUser {}
see details here