简体   繁体   English

如果未登录,则为服务器端重定向

[英]Server-side redirect if not logged in

I use Cloud Endpoints and do client-side oauth to access my backend. 我使用Cloud Endpoints并使用客户端oauth访问我的后端。 This works fine, except that it means I can only check whether the person hitting up my app URL is a user or not after the whole page and attached scripts have loaded and the oauth request has completed. 这样可以正常工作,除了它意味着我只能在整个页面和附加的脚本加载并且oauth请求完成后才能检查打到我的应用程序URL的人是否是用户。

I'd like to enable server-side authentication so that I can use the same URL for my landing page and my app, and redirect the person to the app if she is a user or to the landing page if she isn't. 我想启用服务器端身份验证,以便为登录页面和我的应用程序使用相同的URL,如果用户是用户,则将用户重定向到应用程序,如果不是,则将用户重定向到登录页面。

The problem is that the User Tutorial uses the UserService for authentication whereas Cloud Endpoints uses the OAuth Service . 问题是User Tutorial使用UserService进行身份验证,而Cloud Endpoints使用OAuth服务 I can't figure out how to use OAuth in my redirect filter because all the links from the OAuth section linked to above are for OAuth 1 and are deprecated . 我无法弄清楚如何在我的重定向过滤器中使用OAuth,因为上面链接的OAuth部分的所有链接都是针对OAuth 1的,并且已弃用

How can I check in my redirect filter whether the person visiting is a user or not and simultaneously get a token that I can use to subsequently call my endpoints if she is a user? 如何检查我的重定向过滤器是否访问的用户是否是用户并同时获得一个令牌,如果她是用户,我可以使用该令牌随后呼叫我的端点?

I'm using the Google Api JS client to make calls to cloud endpoints, so the solution would optimally integrate with that, using gapi.auth.setToken(token) with the token retrieved and bypass the whole gapi.auth.authorize(params, callback) dance. 我正在使用Google Api JS客户端来调用云端点,因此解决方案将与其进行最佳集成,使用gapi.auth.setToken(token)检索令牌并绕过整个gapi.auth.authorize(params, callback)舞蹈。

You can provide custom servlet implementaion for login flow where You will fetch oauth-token by providing new Flow and then validate that access token to grant authorization of protected resources. 您可以为登录流提供自定义servlet实现,您可以通过提供新流来获取oauth-token,然后验证该访问令牌以授予受保护资源的授权。

Google provides good reference document here and You can use this sample code repo for reference. Google 在此提供了很好的参考文档,您可以使用示例代码库进行参考。

You can use the user API for endpoints, no need to use OAuth if doesn't suit your needs. 您可以将用户API用于端点,如果不符合您的需求,则无需使用OAuth。 You only need to add an parameter of the type com.google.appengine.api.users.User to your endpoint. 您只需要将com.google.appengine.api.users.User类型的参数添加到您的终端。 As stated here . 如前所述这里

Eg: 例如:

 @ApiMethod(name = "scores.insert")
    public Score insert(Score score, User user) throws OAuthRequestException, IOException {
      //Check if user is null
      //Do your thing

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM