Last active
May 1, 2020 04:33
-
-
Save rajeevshukla/d2d2fce0baa15c5e7d620d68299d0db1 to your computer and use it in GitHub Desktop.
oauth2Login success mapping
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Autowired | |
OAuth2AuthorizedClientService authclientService; | |
@RequestMapping("/oauth2LoginSuccess") | |
public String getOauth2LoginInfo(Model model,@AuthenticationPrincipal OAuth2AuthenticationToken authenticationToken) { | |
// fetching the client details and user details | |
System.out.println(authenticationToken.getAuthorizedClientRegistrationId()); // client name like facebook, google etc. | |
System.out.println(authenticationToken.getName()); // facebook/google userId | |
// 1.Fetching User Info | |
OAuth2User user = authenticationToken.getPrincipal(); // When you login with OAuth it gives you OAuth2User else UserDetails | |
System.out.println("userId: "+user.getName()); // returns the userId of facebook something like 12312312313212 | |
// getAttributes map Contains User details like name, email etc// print the whole map for more details | |
System.out.println("Email:"+ user.getAttributes().get("email")); | |
//2. Just in case if you want to obtain User's auth token value, refresh token, expiry date etc you can use below snippet | |
OAuth2AuthorizedClient client = authclientService | |
.loadAuthorizedClient(authenticationToken.getAuthorizedClientRegistrationId(), | |
authenticationToken.getName()); | |
System.out.println("Token Value"+ client.getAccessToken().getTokenValue()); | |
//3. Now you have full control on users data.You can eitehr see if user is not present in Database then store it and | |
// send welcome email for the first time | |
model.addAttribute("name", user.getAttribute("name")); | |
return "home"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment