简体   繁体   English

如何在spring oauth2安全性中登录successHandler后立即获取访问令牌?

[英]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通过使用clientIdusername方法来获取令牌tokenStore.findTokensByClientIdAndUserName("your Client Id", userDetails.getUsername()); That's it.就是这样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring Security + Google OAuth2登录:访问令牌是否为空? - Spring Security + Google OAuth2 Login: access token is null? 如何使用 Spring Security 5 在 Spring Boot 应用程序(不是 Web 应用程序)中获取 oauth2 访问令牌 - How to get oauth2 access token in a spring boot application (not a web application) using spring security 5 Spring 安全 oauth2 - 在 oauth/token 调用后添加过滤器 - Spring security oauth2 - add filter after oauth/token call Spring security oauth2 - 无法访问 /oauth/token 路由 - Spring security oauth2 - Can't access /oauth/token route Spring Security Oauth2:访问令牌无效/过期时的回退 - Spring Security Oauth2: Fallback when access token is invalid/expired 如何在spring security oauth2中分离访问令牌和刷新令牌端点 - How to separate access token and refresh token endpoint in spring security oauth2 Spring boot security oauth2 从 cookie 中获取 access_token - Spring boot security oauth2 get access_token from cookie 如何在spring boot 2 oauth2中获得令牌? - How get a token in spring boot 2 oauth2? Spring刷新访问令牌后刷新令牌以更改令牌 - Spring OAuth2 refresh token to change after refreshing access token 如何验证oAuth2 access_token是否被Spring安全性发布给它的同一客户端使用? - How to verify that oAuth2 access_token is used by same client to whom it was issued in Spring security?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM