0

我在我的 grails 应用程序中使用 Spring Security Facebook 插件。它在身份验证后从 Facebook 页面返回时显示以下错误消息。facebook 用户详细信息未保存在数据库中。请帮助解决此错误并成功验证 Facebook 用户。提前致谢。

FacebookUser.groovy 域类

class FacebookUser {

    long uid
  String accessToken
  Date accessTokenExpires

    static belongsTo = [user: User]

    static constraints = {
        uid unique: true
    }
}

spring-security-facebook 应用程序的 Config.groovy 设置

grails.plugins.springsecurity.facebook.domain.classname='com.awnsys.gcx.FacebookUser'
grails.plugins.springsecurity.facebook.domain.appUserConnectionPropertyName = 'user'
grails.plugins.springsecurity.facebook.appId='APPID'
grails.plugins.springsecurity.facebook.secret='secretID'

grails.plugins.springsecurity.facebook.autoCreate.enabled=true
grails.plugins.springsecurity.facebook.autoCreate.roles=['ROLE_USER', 'ROLE_FACEBOOK',]

以下是用户在 facebook 中接受应用程序后从 facebook 重定向时在浏览器中显示的错误消息

Error 500: Internal Server Error
URI : /myapp/j_spring_security_facebook_check
Class : grails.validation.ValidationException
Message : Validation Error(s) occurred during save(): - Field error in object 'com.mydomain.myapp.User' on field 'username': rejected value [facebook_100005487962357]; codes [com.mydomain.myapp.User.username.email.error.com.mydomain.myapp.User.username,com.mydomain.myapp.User.username.email.error.username,com.mydomain.myapp.User.username.email.error.java.lang.String,com.mydomain.myapp.User.username.email.error,user.username.email.error.com.mydomain.myapp.User.username,user.username.email.error.username,user.username.email.error.java.lang.String,user.username.email.error,com.mydomain.myapp.User.username.email.invalid.com.mydomain.myapp.User.username,com.mydomain.myapp.User.username.email.invalid.username,com.mydomain.myapp.User.username.email.invalid.java.lang.String,com.mydomain.myapp.User.username.email.invalid,user.username.email.invalid.com.mydomain.myapp.User.username,user.username.email.invalid.username,user.username.email.invalid.java.lang.String,user.username.email.invalid,email.invalid.com.mydomain.myapp.User.username,email.invalid.username,email.invalid.java.lang.String,email.invalid]; arguments [username,class com.mydomain.myapp.User,facebook_100005487962357]; default message [Property [{0}] of class [{1}] with value [{2}] is not a valid e-mail address]

DefaultFacebookAuthDao.groovy 的第 149 行左右

146:                    appUser.setProperty(securityConf.userLookup.passwordExpiredPropertyName, false)
147:                }
148:                AppUserDomainClazz.withTransaction {
149:                    appUser.save(flush: true, failOnError: true)
150:                }
151:            }
152:            user[appUserConnectionPropertyName] = appUser

DefaultFacebookAuthDao.groovy 的第 148 行左右

145:                    appUser.setProperty(securityConf.userLookup.accountLockedPropertyName, false)
146:                    appUser.setProperty(securityConf.userLookup.passwordExpiredPropertyName, false)
147:                }
148:                AppUserDomainClazz.withTransaction {
149:                    appUser.save(flush: true, failOnError: true)
150:                }
151:            }

FacebookAuthProvider.groovy 的第 67 行左右

64:                    log.error("Can't create user w/o access_token")
65:                    throw new CredentialsExpiredException("Can't receive access_token from Facebook")
66:                }
67:                user = facebookAuthDao.create(token)
68:                justCreated = true
69:            } else {
70:                log.error "User $token.uid doesn't exist, and creation of a new user is disabled."

FacebookAuthProvider.groovy 的第 67 行左右

64:                    log.error("Can't create user w/o access_token")
65:                    throw new CredentialsExpiredException("Can't receive access_token from Facebook")
66:                }
67:                user = facebookAuthDao.create(token)
68:                justCreated = true69:            } else {
70:                log.error "User $token.uid doesn't exist, and creation of a new user is disabled."

痕迹

Line | Method
->>  149 | doCall                in DefaultFacebookAuthDao.groovy
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|    687 | withTransaction       in org.grails.datastore.gorm.GormStaticApi
|    148 | create . . . . . . .  in DefaultFacebookAuthDao.groovy
|     67 | authenticate          in FacebookAuthProvider.groovy
|     40 | attemptAuthentication in FacebookAuthRedirectFilter.groovy
|   1145 | runWorker             in java.util.concurrent.ThreadPoolExecutor
|    615 | run . . . . . . . . . in java.util.concurrent.ThreadPoolExecutor$Worker
^    724 | run                   in java.lang.Thread
4

2 回答 2

0

对于那些正在实现 FacebookAuthService 并遇到 Facebook 和 FacebookProfile 类被标记为错误的问题的人,请确保将这两个插件添加到 BuildConfig.groovy 文件中

compile ":spring-social-core:0.1.31"
compile ":spring-social-facebook:0.1.32"

注意:Spring Social Facebook 与 Spring Security Facebook 不是同一个插件。所以如果你有上面的代码,你仍然需要编译 Spring Social 插件。

compile ':spring-security-core:2.0-RC2'
compile ':spring-security-facebook:0.15.2-CORE2'
于 2014-02-02T13:58:01.660 回答
0

默认情况下,插件会生成用户名facebook_%UID%facebook_100005487962357来自您的堆栈跟踪)。似乎您的User域需要.username字段电子邮件(并且有这样的限制),因此无法保存。

在这种情况下,我建议您通过实现FacebookAuthService.createAppUser方法来实现自己的填充用户对象的逻辑。请参阅文档:http ://splix.github.io/grails-spring-security-facebook/guide/5%20Customization.html#5.1%20Using%20FacebookAuthService

而且,Facebook 身份验证插件不知道用户电子邮件(默认情况下 Facebook 登录不提供),您必须从 Facebook API 获取它,请参见示例:https ://github.com/splix/grails-facebook-authentication -example/blob/master/grails-app/services/FacebookAuthService.groovy#L51

于 2013-09-30T16:47:21.743 回答