我使用 spring-security-oauth 实现了自定义身份验证服务器以及多个用于单点登录的客户端应用程序,我可以选择 facebook 和 google 登录。
现在在我的客户端应用程序上,当我有这个 Controller 时:
@RequestMapping({ "/user", "/me" })
public Principal user(Principal principal) {
return principal;
}
对于 google,它工作正常并返回名称,但对于 facebook,它返回一个唯一的 ID。我尝试了很多方法,但无法从这个主要对象中获取名称。有人可以帮忙吗?
这是我的简短 Facebook 配置:
accessTokenUri: https://graph.facebook.com/oauth/access_token
userAuthorizationUri: https://www.facebook.com/dialog/oauth
tokenName: oauth_token
authenticationScheme: query
clientAuthenticationScheme: form
userInfoUri: https://graph.facebook.com/me?fields=id,name,email
最佳答案
好的,我已经解决了:
@RequestMapping(value = { "/user", "/me"}, method = RequestMethod.GET)
public Map<String, String> user(Principal principal) {
Map<String, Object> details = (Map<String, Object>) ((OAuth2Authentication) principal).getUserAuthentication().getDetails();
Map<String, String> map = new LinkedHashMap<>();
map.put("name", (String) details.get("name"));
map.put("email", (String) details.get("email"));
return map;
}
关于java - principal.getName() 返回 facebook 的 ID 而不是名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43968110/