Oauth2 get Username from token
You are using Spring Security for authentication.
You can get User Detail from SecurityContext
Authentication authentication = SecurityContextHolder.getContext()
.getAuthentication();
UserDetails userDetail = authentication.getPrincipal();
userDetail.getUsername();
or in Rest Controller
@RequestMapping(value = "/username", method = RequestMethod.GET)
public String currentUserName(Principal principal) {
return principal.getName();
}
or
@RequestMapping(value = "/username", method = RequestMethod.GET)
public String currentUserName(HttpServletRequest request) {
Principal principal = request.getUserPrincipal();
return principal.getName();
}