首页 > 解决方案 > Azure AD OAuth2AuthorizationResponse 的授权代码

问题描述

我已经按照此处指定的方式启动并运行了 Springboot Active Directory 示例:

https://github.com/Microsoft/azure-spring-boot/tree/master/azure-spring-boot-samples/azure-active-directory-spring-boot-backend-sample

我可以使用我创建并授予“Windows Azure Active Directory”权限的客户端 ID 使用我的 Azure AD 凭据登录。

下一步是我想获取登录用户的头像,所以我需要从OAuth2AuthorizationResponse

我不清楚如何访问这些数据。它在返回的 OAuth2User 对象中不可用

我尝试在 /login/oauth2/code/azure 上设置一个 HandlerInterceptor,这样我就可以拦截响应,但这永远不会被击中(?)

我还尝试添加自定义过滤器:

http.addFilterAfter(
              new CustomFilter(), BasicAuthenticationFilter.class)

但这永远不会被/login/oauth2/code/azureURI击中

标签: azurespring-boot

解决方案


我能够通过覆盖SavedRequestAwareAuthenticationSuccessHandler并将其添加到我的安全配置来拦截身份验证响应:

http....
 .and().oauth2Login().successHandler(authCodeCachingSuccessHandler)

我缓存代码并传递给超类来处理:

 @Override
 public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws ServletException, IOException {

    Map<String, String[]> parameterMap = request.getParameterMap();
    String[] s = parameterMap.get("code");
    tokenCache.setAuthCode( (DefaultOidcUser) authentication.getPrincipal(), s[0] );

    super.onAuthenticationSuccess(request, response, authentication);
 }

我怀疑这是“最佳实践”,但它确实有效,而且我还没有找到更优雅的方法


推荐阅读