Grails Facebook插件 - 如何处理无效的用户 [英] Grails Facebook Plugin - How to Handle Invalid User
问题描述
我正在使用美妙的 Grails Facebook插件,事情正常。问题来自于我提供了另一种认证方式:表单认证。如果系统中已经有一个Facebook用户的电子邮件地址的用户,我想优雅的提醒用户这个事实。我不知道如何做,因为我被埋在一个从过滤器调用的服务。理想情况下,我想在登录页面上显示一条错误消息。这是可能的吗?
I am using the wonderful Grails Facebook Plugin and things are working alright. The problem comes from the fact that I provide another way to authenticate: forms authentication. If there is already a user in the system with the email address of the Facebook user, I would like to gracefully alert the user of this fact. I don't know how to do that since I am buried inside of a service which gets called from a Filter. Ideally I would like to show an error message on the login page. Is this possible?
我的 FacebookAuthService
:
FacebookUser create(FacebookAuthToken token) {
log.info("Create domain for facebook user $token.uid")
Facebook facebook = new FacebookTemplate(token.accessToken.accessToken)
FacebookProfile fbProfile = facebook.userOperations().userProfile
String email = fbProfile.getEmail()
String emailMatch = User.findByEmailAddress(email)
if(emailMatch != null)
throw new RuntimeException("username is bad!!")
我想向用户显示此错误消息,而不是一直通过的异常。我该怎么做?谢谢!
I want to display this error message to the user instead of the exception trickling all the way through. How can I do this? Thanks!
推荐答案
您可以将此消息放入Flash对象中:
You can put this message into a flash object:
def grailsWebRequest = WebUtils.retrieveGrailsWebRequest()
def flash = grailsWebRequest.attributes.getFlashScope(request)
flash.error = "username is bad!!!"
return null // Facebook filter will skip authorization at this case
此外, code> RuntimeException 最好使用 AuthenticationException
的实例。在这种情况下,您可以将Spring Security配置为异常后重定向到特殊URL。只需放入 Config.groovy
:
Also, instead of RuntimeException
it's better to use instance of AuthenticationException
. At this case you can configure Spring Security to redirect to special url after exception. Just put into Config.groovy
:
grails.plugins.springsecurity.failureHandler.exceptionMappings = [
'MyException': '/usernameIsBad'
]
这篇关于Grails Facebook插件 - 如何处理无效的用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!