Я пытаюсь получить UserDetails
объект, как показано ниже.Но у меня есть некоторые трудности и невозможно получить UserDetails
объект, поэтому в authentication.getAttributes()
есть только JSONObject
.Есть ли какой-нибудь альтернативный способ в микроавтобусе получить UserDetails
объект?
Пользовательский UserDetails
объект:
public class MyUserPrincipal implements UserDetails {
private Account account;
public MyUserPrincipal(Account account) {
this.account = account;
}
public Account getAccount() {
return getAccount();
}
}
Rest api:
//micronaut
@Post(value = "/echo")
@Status(HttpStatus.OK)
public Long echo(@Nullable Authentication authentication) {
Long accountId = (Long)((JSONObject)authentication.getAttributes().get("account")).get("id");
return accountId;
}
Например, вSpring Security легко с аннотацией @AuthenticationPrincipal
в параметре.
Rest api:
@GET
public ResponseEntity<?> echo(@AuthenticationPrincipal MyUserPrincipal user) {
return new ResponseEntity<>(user.getAccount().getAccountId(), HttpStatus.OK);
}