我试图得到UserDetails像下面这样的对象。但是,我有一些困难,不可能得到对象,UserDetails所以只有JSONObject. authentication.getAttributes()在 micronaut 中有没有其他方法可以获取UserDetails对象?
自定义UserDetails对象:
public class MyUserPrincipal implements UserDetails {
private Account account;
public MyUserPrincipal(Account account) {
this.account = account;
}
public Account getAccount() {
return getAccount();
}
}
休息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中,参数中的注释很容易。
休息API:
@GET
public ResponseEntity<?> echo(@AuthenticationPrincipal MyUserPrincipal user) {
return new ResponseEntity<>(user.getAccount().getAccountId(), HttpStatus.OK);
}
牧羊人nacy
守着一只汪
相关分类