Grails Facebook使用spring安全登录 - 如何知道登录用户名

问题描述:

在Grails应用程序中,使用spring security facebook插件,当用户登录时,应用程序如何获取实际的Facebook用户名,以便主页可以显示登录的用户名?

In a Grails application, with spring security facebook plugin, when user logs in, how can the application get actual facebook user name so that the home page can display the logged in user name?

该插件提供Facebook API access_token ,您必须从Facebook请求此类用户详细信息。

The plugin provides Facebook API access_token, and you have to request such user details from Facebook.

示例:

添加依附关系到Spring Social Facebook:

Add dependency to Spring Social Facebook:

dependency {
  compile 'org.springframework.social:spring-social-core:1.0.1.RELEASE'
  compile 'org.springframework.social:spring-social-facebook:1.0.1.RELEASE'
}

,然后从Facebook上加载用户创建,通过在FacebookAuthService中添加以下内容:

and then load it from Facebook on user creation, by adding following into FacebookAuthService:

void onCreate(FacebookUser user, FacebookAuthToken token) {
  Facebook facebook = new FacebookTemplate(token.accessToken.accessToken)
  FacebookProfile fbProfile = facebook.userOperations().userProfile

  //fill the name
  //fieldname ('fullname' at this example) is up to you
  user.fullname = fbProfile.name
}

另见

  • docs for Spring Social - http://static.springsource.org/spring-social-facebook/docs/1.0.x/reference/html/apis.html#facebook-getProfile
  • about FacebookUserService - http://splix.github.com/grails-spring-security-facebook/guide/5%20Customization.html#5.1%20Using%20FacebookAuthService