【发布时间】:2015-02-09 12:06:56
【问题描述】:
TL;DR:从 EDIT 1 读取。
我正在尝试寻找如何实现身份验证的解决方案,尤其是用于移动应用程序使用的我的 rest api 的 OAuth。
我发现了这个流行的插件(Spring Security Rest):
http://alvarosanchez.github.io/grails-spring-security-rest/docs/guide/single.html#oauth1
但问题是它是为 javascript 前端应用程序设计的,所以它需要一个 callback url 到我的前端来传递生成的令牌(如图所示)。
对于移动应用程序,我该怎么做?我可以为此实现另一个插件或设计吗? 我觉得奇怪的是,在 Grails 或 Spring 中并没有很多关于无状态 OAuth 的插件或教程,但是有大量的移动应用程序使用它。
这是一个用例示例:
- 用户打开移动应用程序。
- 用户在移动应用上使用 facebook 登录。
- 用户向 api/orders 发出请求,这将只返回他的订单。
谁应该处理其中的 OAuth 部分?插件上显示的图表中的流程不适用于移动应用程序,因此移动应用程序是否应该向 facebook 验证,获取令牌存储然后将其传递给网络应用程序?
我想了解在这种情况下正确的设计/流程是什么,以及是否有插件或使用 grails 实现它的方法。
编辑:这是正确的流程吗?
如果是,是否有使用 Grails 实现此功能的标准方法?
编辑 2: 现在我了解了流程,我需要将登录部分与 Spring 安全休息插件(已经处理其他所有内容)集成。 这是我想与插件集成的代码,但我不知道在插件中的何处或修改什么:
// api/auth/fb
def auth(){
//Extract fb_access_token
String fbAccessToken = request.JSON.?fb_access_token
//Call https://graph.facebook.com/me?access_token=fbAccessToken
def userInfo = facebookService. ... (fbAcessToken) //Method to use in the plugin
//Verify if the userInfo contains an error/doesn't contain a fbId
if(userInfo.getFbId() == null ){
respond unauthorized() // 401, Invalid access token
}
//Verify if this token is for our app
else if(userInfo.getAppSecret() != System.env.getParameter("appSecret")){
respond unauthorized() //401, token not for this app
}
//Register or login
else{
User user = User.findByFbId(userInfo.getFbId())
if(user == null){
facebookService.registerUser(userInfo) //Custom method implemented in the service
}
else{
FbToken fbToken = new FbToken(userInfo.getToken(), userInfo.getExpiration())
user.setFbAccessToken(fbToken)
}
//Login the user with spring security and let it handle the rest (token creation => storage => http answer)
}
}
【问题讨论】:
-
你最后有没有运气让它工作?
标签: api rest grails mobile oauth