使用 Spring Boot OAuth2 针对 Google 进行身份验证时,如何将 google 用户电子邮件添加到白名单用户

2024-01-10

我想将连接到我的 OAuth2 客户端的用户列入白名单,但我不知道如何获取用户名(特别是 Google 电子邮件地址)。

我基于 Spring 教程创建了一个 Spring Boot OAuth2 应用程序

https://spring.io/guides/tutorials/spring-boot-oauth2/#_social_login_manual https://spring.io/guides/tutorials/spring-boot-oauth2/#_social_login_manual

我正在通过 Google 进行身份验证(成功)。我想确定用户的电子邮件地址,以便我可以将身份验证用户列入白名单。

这个网站,

http://www.baeldung.com/spring-security-openid-connect#filter http://www.baeldung.com/spring-security-openid-connect#filter

建议我可以解压从 Google 返回的“id_token”,如下所示:

/**
 * logic to unpack a ConnectID id_token like what we get from Google -
 * see "Spring Security and OpenID Connect" - heading '4. Custom OpenID Connect Filter':
 *         http://www.baeldung.com/spring-security-openid-connect#filter
 * 
 * @param oa2token
 * @return
 */
private static UsernamePasswordAuthenticationToken getOpenIDDataForToken(OAuth2AccessToken oa2token)
{
        try {
            String idToken = oa2token.getAdditionalInformation().get("id_token").toString();
            Jwt tokenDecoded = JwtHelper.decode(idToken);
            Map<String, String> authInfo = new ObjectMapper().readValue(tokenDecoded.getClaims(), Map.class);

            OpenIdConnectUserDetails user = new OpenIdConnectUserDetails(authInfo, oa2token);
            return new UsernamePasswordAuthenticationToken(user, null, user.getAuthorities());
        } catch (InvalidTokenException e) {
            throw new BadCredentialsException("Could not obtain user details from token", e);
        }
}

但我无法编译这段代码 -我不知道如何上课 JtwHelper!

我四处搜寻,以下可能是正确的Maven依赖性:

        <!-- https://mvnrepository.com/artifact/org.springframework.security/spring-security-jwt -->
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-jwt</artifactId>
        </dependency>

但是将其添加到我的 pom.xml 中并没有帮助 - 我没有在我的 .m2 存储库中得到真正的 Jar 文件 - 我得到一个文本文件!最重要的是,Eclipse 无法解析类型JwtHelper.

Help?我不确定我哪里出错了。


看起来这个页面上有一个答案my回答(感谢@user2802927):

如何从 OAuth2 授权服务器/用户端点获取自定义用户信息 https://stackoverflow.com/questions/35056169/how-to-get-custom-user-info-from-oauth2-authorization-server-user-endpoint#answer-42015548

这是代码:

    Principal principal = servlet_request.getUserPrincipal();
    try {
         if (principal != null) {
                OAuth2Authentication oAuth2Authentication = (OAuth2Authentication) principal;
                Authentication authentication = oAuth2Authentication.getUserAuthentication();
                Map<String, String> details = new LinkedHashMap<>();
                details = (Map<String, String>) authentication.getDetails();
                Map<String, String> map = new LinkedHashMap<>();
                map.put("email", details.get("email"));
                logger.debug("details map is: {}", map);
            }
    } catch (Exception e) {
        logger.error("dumping principal " + principal + "failed, exception: ", e );
    }

输出显示我找到了成功——用户的电子邮件地址!!!

2017-05-23 11:48:26.751 DEBUG 7687 --- [nio-8443-exec-1] ication$$EnhancerBySpringCGLIB$$91415b85 :
details map is: {[email protected] /cdn-cgi/l/email-protection}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 Spring Boot OAuth2 针对 Google 进行身份验证时,如何将 google 用户电子邮件添加到白名单用户 的相关文章

随机推荐