0

在应用程序中管理用户会话的最佳方式是什么Google App Engine?理想情况下,我希望我的应用程序保持无状态,并且不在内存中保存任何与用户相关的数据,但是我也害怕在每个请求上发送网络的用户凭据(更不用说在每个请求上对用户进行身份验证都需要调用哪个要Datastore花钱)。

我查看了谷歌的OAuth 2.0解决方案,但据我了解,如果我的应用程序想要连接到任何谷歌 API 并且需要客户端的许可才能访问他的谷歌帐户,它会有所帮助。

有没有办法管理用户会话?最常见的场景是知道是哪个用户发起了这个请求,而无需将其userId作为请求参数发送。

请注意,我们不使用第三方提供商。用户正常注册到我们的页面并拥有一个自定义帐户。我不是在寻找有助于将身份验证与第三方服务集成的工具。否则我会使用谷歌OAuth 2.0或类似的 API

4

1 回答 1

0

您始终可以实现身份验证器接口。

        public class MyAuthenticator implements Authenticator {
    @Override
    public User authenticate(HttpServletRequest request) {
        HttpSession session = request.getSession(false);
        //
        return null;// if not authenticated, otherwise return User object.
    }
}

// Endpoints class.
@Api(name = "example", authenticators = { MyAuthenticator.class })
public class MyEndpoints {
    public Profile getProfile(User user) {

        if (user == null) {
            throw new UnauthorizedException("Authorization required");
        }
        return new Profile(user.getEmail(), "displayName");
    }

    // store this class somewhere in models
    public class Profile {
        private String email;
        private String displayName;

        public Profile(String email, String displayName) {
            this.email = email;
            this.displayName = displayName;
        }

        public String getEmail() {
            return email;
        }




        public String getdisplayName() {
            return displayName;
        }
    }
}

使用 HttpServletRequest 对象来实现基于经典会话的登录或使用您自己的自定义标头。嗯,这取决于你的情况。未通过身份验证时返回 null,通过身份验证时返回 User 对象。还要在双方(客户端和服务器)上实施某种加密,以阻止拥有会话密钥的人访问您的 api。

于 2017-05-27T08:54:56.423 回答