spring - Spring 配置 Principal.getname() 以返回 JWT 电子邮件声明
问题描述
我目前正在从以下地址收到用户的电子邮件SecurityContextHolder
:
Jwt jwt = (Jwt) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
return jwt.getClaims().get("email").toString();
有没有办法配置Principal.getName()
它直接返回电子邮件以避免控制器中 Jwt 的特定知识?例如:
@PutMapping("/reserve")
public Object reserveGame(Principal principal) {
String email = principal.getName(); // need this to return email
return dao.reserveGame(email);
}
解决方案
覆盖您的 UserDetails 类方法getUsername()
并返回email
属性。或者您可以提供自定义UserAuthenticationConverter
推荐阅读
- angular - Angular 和 IIS Windows 身份验证通信
- excel - EXCEL 中的 IF 或 OR 匹配函数
- python - 如何在snakemake中引用规则的输出
- ios - 在objective-c中自定义react-native webview
- sql - 在 1:N 关系中只选择一个值
- java - .net 和 java 中的 grpc 库版本
- powershell - 如何使用 deviceid 的子字符串获取完整的实例 ID?
- sql - 如何使用 SQL 存储过程替换作为输入参数的字符串中的字符
- javascript - 如何在chartjs中为同一组数据添加多个标签?
- cloud-foundry - 如何从 CloudFoundry 向 AWS-sdk 提供凭证