具有Intuit重定向的Spring Boot OAuth2

时间:2018-10-23 02:40:43

标签: spring-boot oauth-2.0 intuit

我正在使用Spring Boot执行OAuth2登录到Intuit。但是,Intuit不会使用访问令牌来响应redirect_uri,而是会发送一个代码,然后需要将其交换为令牌。我可以手动执行该请求,并尝试直接在会话上设置令牌,但这不会导致对用户进行身份验证。

这是我的redirect_uri,然后执行Intuit请求以填充其余令牌数据。

@RequestMapping("/redirect")
public String oAuth2Redirect(HttpSession session,
                             @RequestParam(value = "state", required = false) String state,
                             @RequestParam(value = "code", required = false) String code,
                             @RequestParam(value = "realmId", required = false) String realmId,
                             @RequestParam(value = "error", required = false) String error) throws com.intuit.oauth2.exception.OAuthException {

    ...

    // Exchange Code for Tokens
    OAuth2PlatformClient platformClient = new OAuth2PlatformClient(oAuth2Config);
    BearerTokenResponse bearerTokenResponse = platformClient.retrieveBearerTokens(code, oauth2RedirectUri);

    DefaultOAuth2ClientContext context = (DefaultOAuth2ClientContext) session.getAttribute("scopedTarget.oauth2ClientContext");
    DefaultOAuth2AccessToken defaultOAuth2AccessToken = new DefaultOAuth2AccessToken(bearerTokenResponse.getAccessToken());
    defaultOAuth2AccessToken.setExpiration(new Date(System.currentTimeMillis() + bearerTokenResponse.getExpiresIn()));
    defaultOAuth2AccessToken.setRefreshToken(new DefaultExpiringOAuth2RefreshToken(bearerTokenResponse.getRefreshToken(),
            new Date(System.currentTimeMillis() + bearerTokenResponse.getXRefreshTokenExpiresIn())));
    context.setAccessToken(defaultOAuth2AccessToken);

    ...

    return "success";
}

是否可以对端点的用户内部进行身份验证?有没有更好的方法来完成此额外的OAuth步骤?

编辑:

我试图用授权码简单地更新上下文,以便它可以通过Spring Security获得令牌。但是此上下文在我的过滤器中似乎不起作用。

这是在重定向中:

    DefaultOAuth2ClientContext context = (DefaultOAuth2ClientContext) session.getAttribute("scopedTarget.oauth2ClientContext");
    context.getAccessTokenRequest().setAuthorizationCode(code);

这是我的过滤器(可能使用了错误的上下文):

@Qualifier("oauth2ClientContext")
@Autowired
private OAuth2ClientContext oauth2ClientContext;

@Bean("oauth2Filter")
public Filter oauth2Filter(@Qualifier("quickbooksResource") ResourceServerProperties quickbooksResource,
                           @Qualifier("quickbooksDetails") AuthorizationCodeResourceDetails quickbooksDetails) {
    OAuth2ClientAuthenticationProcessingFilter quickbooksFilter = new OAuth2ClientAuthenticationProcessingFilter("/login");

    OAuth2RestTemplate quickbooksTemplate = new OAuth2RestTemplate(quickbooksDetails, oauth2ClientContext);
    quickbooksFilter.setRestTemplate(quickbooksTemplate);

    UserInfoTokenServices tokenServices = new UserInfoTokenServices(quickbooksResource.getUserInfoUri(), quickbooksDetails.getClientId());
    tokenServices.setRestTemplate(quickbooksTemplate);
    quickbooksFilter.setTokenServices(tokenServices);

    return quickbooksFilter;
}

0 个答案:

没有答案