'Spring OAuth2 name always null
New to OAuth2 and Spring framework. Have followed two introduction guides to it so far (for example this one) and the OAuth2User object (org.springframework.security.oauth2.core.user.OAuth2User) always has a null value for the "name" attribute which, as following along with the tutorials, should not be null apparently. Ended up cloning the repo just to make sure I had identical code and value was still 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);
}
}
Uncommenting the for loop above revleals most keys fetched from principle.getAttributes() to be null (including name, email etc). Not clear to me what I'm doing wrong here - I have clientId and clientSecret (obtained by creating github OAuth app) configured correctly in my properties fie. Any help would be much appreciated
Solution 1:[1]
I had the same issue and I solved it by access my github profile and update my name (In my case I didn't provide it)!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Enbidi |
