OAuthPostAuthListener.java 文件源码

java
阅读 26 收藏 0 点赞 0 评论 0

项目:rest-retro-sample 作者:
@Override
public void onApplicationEvent(AbstractAuthenticationEvent event) {
    Authentication authentication = event.getAuthentication();
    if (event instanceof AuthenticationSuccessEvent) {

      ResourceOwnerPasswordResourceDetails resource = getResourceOwnerPasswordResourceDetails();
      resource.setScope(Arrays.asList("words"));
      resource.setUsername(authentication.getName());
      resource.setPassword(authentication.getCredentials().toString());

      try {
          OAuth2AccessToken accessToken = accessTokenProvider.obtainAccessToken(resource, new DefaultAccessTokenRequest());
          log.debug("Access token request succeeded for user: '{}', new token is '{}'"
                  , resource.getUsername() 
                  , accessToken.getValue());
          if (authentication instanceof AbstractAuthenticationToken && authentication.getDetails() instanceof CustomAuthenticationDetails) {
              ((CustomAuthenticationDetails) ((AbstractAuthenticationToken) authentication).getDetails())
                .setBearer(accessToken.getValue());
              log.debug("Access token was added to authentication as details");
          } else if (log.isDebugEnabled()) {
              log.debug("Access token could not be added to authentication as details");
          }
      } catch (Exception e) {
          log.error("Access token request failed for user: '" + resource.getUsername() + "'", e);
      }
    }
    if (authentication instanceof CredentialsContainer) {
           // Authentication is complete. Remove credentials and other secret data from authentication
           ((CredentialsContainer)authentication).eraseCredentials();
       }

}
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号