簡體   English   中英

Spring OAuth2 名稱始終為 null

[英]Spring OAuth2 name always null

OAuth2 和 Spring 框架的新功能。 到目前為止已經遵循了兩個介紹指南(例如這個)和OAuth2User object(org.springframework.security.oauth2.core.user.OAuth2User)總是有一個 null 的“名稱”屬性值,如下所示根據教程,顯然不應該是 null。 最后克隆回購協議只是為了確保我有相同的代碼並且值仍然是 null。

@SpringBootApplication
@RestController
public class SocialApplication extends WebSecurityConfigurerAdapter {

    @GetMapping("/user")
    public Map<String, Object> user(@AuthenticationPrincipal OAuth2User principal) {
        
//      for(String key : principal.getAttributes().keySet()) {
//          System.out.println(String.format("key= %s value=%s", key, principal.getAttribute(key)));
//      }
        return Collections.singletonMap("name", principal.getAttribute("name"));
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        // @formatter:off
        http
            .authorizeRequests(a -> a
                .antMatchers("/", "/error", "/webjars/**").permitAll()
                .anyRequest().authenticated()
            )
            .exceptionHandling(e -> e
                .authenticationEntryPoint(new HttpStatusEntryPoint(HttpStatus.UNAUTHORIZED))
            )
            .oauth2Login();
        // @formatter:on
    }

    public static void main(String[] args) {
        SpringApplication.run(SocialApplication.class, args);
    }

}

    }

    public static void main(String[] args) {
        SpringApplication.run(SocialApplication.class, args);
    }
}

取消注釋上面的 for 循環將從principle.getAttributes()獲取的大多數鍵變為 null(包括nameemail等)。 我不清楚我在這里做錯了什么 - 我在我的屬性 fie 中正確配置了 clientId 和 clientSecret(通過創建 github OAuth 應用程序獲得)。 任何幫助將非常感激

我有同樣的問題,我通過訪問我的 github 個人資料並更新我的名字解決了這個問題(在我的情況下我沒有提供它)!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM