我用ouath 2.0配置了jhipster,后者使用keycloak作为授权服务器。但是如何在我的应用程序中获取当前用户的电子邮件?
答案 0 :(得分:0)
用户通过OAuth2登录时,帐户is synchronized to the local user database。
应用程序中存在SecurityUtils.java
,它具有一种获取当前用户登录名的方法。您可以获取登录名并查询用户/电子邮件。
String login = SecurityUtils.getCurrentUserLogin().orElse("anonymoususer");
Optional<User> optionalUser = userService.getUserWithAuthoritiesByLogin(login);
if (optionalUser.isPresent()) {
String email = optionalUser.get().getEmail();
}
答案 1 :(得分:0)
一种方法是使用 AccountService
在您的组件中,您将执行如下所示的操作。您需要更改这些值以匹配您的项目
// Add these imports
import { AccountService } from 'app/core/auth/account.service';
import { Account } from 'app/core/auth/account.model';
@Component({
selector: 'jhi-pm-your-project-update',
templateUrl: './pm-your-project-update.component.html',
})
export class YourUpdateComponent implements Oninit{
// Add this varable to store the account information
account: Account | null = null;
constructor(
protected dataUtils: DataUtils,
protected eventManager: EventManager,
protected yourService: YourService,
protected activatedRoute: ActivatedRoute,
protected accountService: AccountService
){}
ngOnInit(): void {
this.accountService.identity().subscribe(account => (this.account = account));
}
save(): void {
// Here is how to access the email
yourEntity.lastModifiedBy = this.account?.email;
}
}