Currently, I am working on a project where a requirement is accessToken need to be collected at spring success handler just after login. I read different blogs but couldn't found any possible solution. Here I am giving code snippet that I tried,
This is my success handler where I am trying to get accessToken. But when I am trying to cast authentication.getDetails() to OAuth2AuthenticationDetails I am getting exception.
@Component
public class AuthSuccessListener implements ApplicationListener<AuthenticationSuccessEvent> {
@Override
public void onApplicationEvent(AuthenticationSuccessEvent event) {
UserDetails userDetails = (UserDetails) event.getAuthentication().getPrincipal();
System.out.println(userDetails);
System.out.println("pass is " + userDetails.getPassword());
System.out.println("username is " + userDetails.getUsername());
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
//Authentication authentication = event.getAuthentication();
OAuth2AuthenticationDetails details = (OAuth2AuthenticationDetails) authentication.getDetails();
String accessToken = details.getTokenValue();
System.out.println(accessToken);
}
}
I don't know whether this approach is valid or not. If there any other way to get accessToken just after login in successHandler or somewhere else, then please help to solve this issue.