Spring配置Principal.getname()返回JWT电子邮件声明

时间:2020-06-11 21:12:57

标签: spring spring-boot spring-security

我目前正在从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);
  }

1 个答案:

答案 0 :(得分:0)

覆盖您的UserDetails类方法getUsername()并返回email属性。或者您可以提供自定义的UserAuthenticationConverter