【问题标题】:How to handle security constraints using GWT 2.1's RequestFactory?如何使用 GWT 2.1 的 RequestFactory 处理安全约束?
【发布时间】:2023-04-01 11:46:01
【问题描述】:

我目前正在开发要部署在 Google App Engine 上的 GWT 2.1 应用程序。我想用新的RequestFactory实现服务器通信。

现在我的问题是如何在这种情况下处理细粒度的安全问题?某些服务器操作(在 RequestContext 存根中声明的那些)应仅限于某些用户(可能取决于远程调用的参数)。如果呼叫未经授权,我希望客户端显示一个登录页面(例如,以便可以以其他用户身份登录)。

从费用示例中,我知道如何实现自动重定向到登录页面,但在此示例中,安全模型非常简单:当且仅当用户登录时,客户端才被允许访问 servlet .

我应该在我的服务器端服务中引发自定义 UnAuthorizedException 吗?我应该在哪里拦截这个异常? (我可以在像 Expenses 示例的 GaeAuthFilter 这样的 servlet 过滤器中执行此操作吗?)

【问题讨论】:

    标签: security google-app-engine authentication gwt servlets


    【解决方案1】:

    我也在寻找解决此问题的方法,并提出了以下建议。它不能完全处理用户界面方面的事情(即重定向到登录页面),但它会保护您的数据层免受未经授权的访问。

    public class MyRequestFactoryServlet extends RequestFactoryServlet
    {
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException
        {
            if (! userIsLoggedIn(req))
            {
                throw new ServletException("not logged in");
            }
            else
            {
                super.doPost(req, res);
            }
        }
    
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
        {
            if (! userIsLoggedIn(req))
            {
                throw new ServletException("not logged in");
            }
            else
            {
                super.doGet(req, res);
            }
        }
    
        protected boolean userIsLoggedIn(HttpServletRequest req)
        {
            // insert your custom code here for checking session for valid login token
            User user = (User) req.getSession().getAttribute("LOGGED_IN_USER");
            return user != null && user.isEnabled();
        }
    

    然后在 web.xml 中使用 MyRequestFactoryServlet 而不是 RequestFactoryServlet。

    为了处理登录的 UI 方面,我使用 GWT RPC 让我的应用程序的登录页面检查有效登录;如果用户未登录,系统会提示他们输入用户名/密码。上面的代码保护后端免受用户试图通过直接跳转到其他 URL 或手动将数据发布到 servlet 来绕过登录页面。

    【讨论】:

    • 感谢您的回复。我想知道在为 Android 使用相同的 RF 时如何实现?我在 android 下使用相同的 requestfactories 和对象,但我不确定如何设置会话属性。你处理过这个吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-08
    • 1970-01-01
    • 1970-01-01
    • 2011-06-24
    • 1970-01-01
    • 1970-01-01
    • 2011-07-13
    相关资源
    最近更新 更多