我目前正在从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);
}
答案 0 :(得分:0)
覆盖您的UserDetails类方法getUsername()
并返回email
属性。或者您可以提供自定义的UserAuthenticationConverter