【问题标题】:Extract data from Principal in Java Spring Boot从 Java Spring Boot 中的 Principal 中提取数据
【发布时间】:2020-05-12 17:00:11
【问题描述】:

我尝试从 Principal 中提取数据。

principal

怎么做?我需要来自“属性”集合的登录值。 principal 没有这样的方法。

【问题讨论】:

标签: java spring-boot principal


【解决方案1】:

这样的东西可以工作。获得这些属性总是有点讨厌。

    @RequestMapping("/user")
    public void user(Authentication authentication) {
        LinkedHashMap<String, Object> properties = (LinkedHashMap<String, Object>) authentication.getDetails();
        String loginProperty = properties.get("login");
        //Do what you want to do with your property
    }

【讨论】:

  • 为什么我不能在我的 @GetMapping("/authenticated") 端点中使用 OAuth2Authentication?我得到异常:Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.IllegalStateException: Current user principal is not of type [org.springframework.security.oauth2.provider.OAuth2Authentication]: org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken@28f0d859: Principal: Name: [...], Granted Authorities: [[...]], User Attributes: [{...}]
  • 嗨,卡罗尔,我调整了答案。你能改用“身份验证”吗?
  • 你的解决方案给了我 (LinkedHashMap) authentication.getUserAuthentication().getDetails(); Unchecked cast: 'java.lang.Object' to 'java.util.LinkedHashMap&lt;java.lang.String,java.lang.Object&gt;' 并返回 properties.get("login"); Incompatible types. Required: org.springframework.security.core.Authentication Found: java.lang.Object
  • 首先只是一个警告。第二个是我的坏。它不应该返回任何东西,它只是一个关于如何获取价值的例子。再次调整。
  • 现在我得到:Cannot resolve method 'getUserAuthentication()'
猜你喜欢
  • 1970-01-01
  • 2019-05-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-11
  • 2018-02-20
  • 2016-07-27
  • 2016-12-21
相关资源
最近更新 更多