【问题标题】:Spring Boot OAuth2: How to retrieve user token info detailsSpring Boot OAuth2:如何检索用户令牌信息详细信息
【发布时间】:2017-10-11 14:23:36
【问题描述】:

我正在关注https://spring.io/guides/tutorials/spring-boot-oauth2/,到目前为止一切正常。实际上,我什至还设法将 Google Oauth 连接为第二个身份验证提供程序。现在我正在尝试从后端的令牌信息端点读取信息,以将其与本地数据库同步并调整输出。但我不知何故努力检索信息。

本教程有时会创建此端点:

@RequestMapping("/user")
public Principal user(Principal principal) {
    return principal;
}

调用时,它会正确显示(成功登录后)

{
    "authorities": [...],
    "details": {...},
    "authenticated": true,
    "userAuthentication": {
        "authorities": [...],
        "details": {
            "email": "foo@bar.com",
            "name": "Foo Bar",
            "id": "12345674890000"
        },
        "authenticated": true,
        "principal": "12345674890000",
        "credentials": "N/A",
        "name": "12345674890000"
    },
    "oauth2Request": {},
    "clientOnly": false,
    "credentials": "",
    "principal": "12345674890000",
    "name": "12345674890000"
}

因此,Spring Boot 安全性以某种方式获取了正确的信息。现在我正在尝试构建一个Interceptor(基本功能正在运行,每个请求都会调用拦截器)来处理这些信息。我正在努力获得例如现在的电子邮件地址。我找到this 答案,但它指的是旧版本的spring boot,只是链接到一个新接口,但我想知道我是否需要它(对于我认为的简单用例来说,它看起来也很复杂) .在我的 spring 应用程序中从这个 /user 端点获取信息而不实际调用我自己的端点来检索它的最佳方法是什么?

我唯一能找到的是通过SecurityContextHolder 原则 ID 和其他对我没有帮助的东西。我实际上并没有检索到例如电子邮件地址。

【问题讨论】:

    标签: java spring spring-boot spring-security oauth


    【解决方案1】:

    尝试使用 SecurityContext:

    SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    

    它将包含当前登录的真实用户对象

    【讨论】:

    • @Wolli 您是否尝试过上述方法?当你这样做时你会得到什么(在控制器中): Principal principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal(); Map details = ((OAuth2Authentication) principal).getUserAuthentication().getDetails(); logger.info("详细信息 = " + 详细信息); // id、email、姓名、链接等
    • sry 对于迟到的答案,但这两个建议都不起作用,因为我从 getPrincipal() 方法返回的只是一个字符串对象......有什么线索吗? www 中的很多建议说应该有一个真实的用户对象,但总是只有一个字符串。我认为我的设置可能还缺少其他东西...
    • 如果我尝试将此字符串原则转换为 UserDetails 类(如大多数建议的那样),我会得到一个 CastException ...
    【解决方案2】:

    这可能会有所帮助

    我假设您知道用户和角色映射。我的 User 类扩展了 UserDetails,所以我需要重写下面提到的一些方法

    Collection<? extends GrantedAuthority> getAuthorities();
    String getPassword();
    String getUsername();
    boolean isAccountNonExpired();
    boolean isAccountNonLocked();
    boolean isCredentialsNonExpired();
    boolean isEnabled();
    

    然后调用 SecurityContextHolder.getContext().getAuthentication() 有助于返回用户对象如下

    User user = null;
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    if (authentication != null
       && authentication.getPrincipal() != null
       && authentication.getPrincipal() instanceof User) {
            user = (User) authentication.getPrincipal();
        }
    

    现在在用户对象中,如果映射完美,您将从用户表中获取所有信息。如用户名、电子邮件、名字、姓氏等。

    【讨论】:

      猜你喜欢
      • 2016-06-02
      • 2016-12-31
      • 1970-01-01
      • 2017-08-04
      • 2018-03-17
      • 2016-08-01
      • 2019-12-01
      • 2016-05-05
      • 1970-01-01
      相关资源
      最近更新 更多