Spring Security Facebook身份验证不起作用 - 内部服务器错误

时间:2013-09-30 16:28:24

标签: facebook grails spring-security

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

FacebookUser.groovy Domain Class

class FacebookUser {

    long uid
  String accessToken
  Date accessTokenExpires

    static belongsTo = [user: User]

    static constraints = {
        uid unique: true
    }
}

spring-security-facebook app的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

2 个答案:

答案 0 :(得分:0)

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

在这种情况下,我建议您通过实现FacebookAuthService.createAppUser方法来实现填充User对象的自己的逻辑。请参阅文档: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

答案 1 :(得分: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'