[英]How to get access token just after login in successHandler in spring oauth2 security?
Currently, I am working on a project where a requirement is accessToken need to be collected at spring success handler just after login.目前,我正在开发一个项目,其中需要在登录后立即在 spring 成功处理程序中收集accessToken 。 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.这是我尝试获取 accessToken 的成功处理程序。 But when I am trying to cast authentication.getDetails() to OAuth2AuthenticationDetails I am getting exception.
但是当我尝试将authentication.getDetails() 转换为OAuth2AuthenticationDetails 时,我遇到了异常。
@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.
如果在successHandler或其他地方登录后还有其他方法可以获取accessToken,请帮助解决此问题。
At least I found a solution to get token at succeshandler just after login.至少我找到了一个解决方案,可以在登录后立即在 succeshandler 处获取令牌。 As I haven't got relevant solution in short, that's why I am posting this answer.
由于我没有简而言之相关的解决方案,这就是我发布此答案的原因。 I solved the issue like this.
我这样解决了这个问题。
@Component
public class AuthSuccessListener implements ApplicationListener<AuthenticationSuccessEvent> {
private final TokenStore tokenStore;
public AuthSuccessListener(TokenStore tokenStore) {
this.tokenStore = tokenStore;
}
@Override
public void onApplicationEvent(AuthenticationSuccessEvent event) {
UserDetails userDetails = (UserDetails) event.getAuthentication().getPrincipal();
String accessToken = null;
List<OAuth2AccessToken> tokens = (List<OAuth2AccessToken>) tokenStore.findTokensByClientIdAndUserName("your Client Id", userDetails.getUsername());
if (tokens.size() > 0) {
accessToken = tokens.get(0).getValue();
}
}
}
Here you can see I used TokenStore to reteive token by using clientId and username with method tokenStore.findTokensByClientIdAndUserName("your Client Id", userDetails.getUsername());
在这里你可以看到我使用TokenStore通过使用clientId和username方法来获取令牌
tokenStore.findTokensByClientIdAndUserName("your Client Id", userDetails.getUsername());
That's it.就是这样。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.