如何使用GWT 2.1的RequestFactory处理安全性约束?

Mar*_*arc 3 security authentication gwt google-app-engine servlets

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

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

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

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

Geo*_*old 5

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

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();
    }
Run Code Online (Sandbox Code Playgroud)

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

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