Java Google AppEngine 会话示例

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1134800/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 00:08:36  来源:igfitidea点击:

Google AppEngine Session Example

javagoogle-app-enginesession

提问by Maksim

I just enabled Sessionin my Google AppEngine/Java + GWT application. And how do I use it? How do I get session ID and play will all good stuff from it? Are there any real examples of simple login page where I'm just entering LoginName and Password, then it goes to the server over RPC call, authenticates against database and sends Session ID back to the client.

我刚刚在我的 Google AppEngine/Java + GWT 应用程序中启用了 Session。我该如何使用它?我如何获得会话 ID 并从中播放所有好东西?是否有任何简单登录页面的真实示例,我只是在其中输入登录名和密码,然后通过 RPC 调用进入服务器,对数据库进行身份验证并将会话 ID 发送回客户端。

I have following code already but don't know what to do next:

我已经有了以下代码,但不知道下一步该怎么做:

GWT Login Form:

GWT登录表单:

public class LoginForm {
    private final LoginServiceAsync loginService = GWT.create(LoginService.class);

    VerticalPanel loginVp = new VerticalPanel();
    TextBox loginTxt = new TextBox();
    TextBox passTxt = new TextBox();

    Button loginBtn = new Button("Login");

    public Widget getLoginWidget(){

        loginBtn.addClickHandler(new ClickHandler(){

            public void onClick(ClickEvent arg0) {

                loginService.authenticateUser(loginTxt.getText(), passTxt.getText(), 
                        new AsyncCallback<String>(){

                            public void onFailure(Throwable caught) {
                                InfoPanel.show(InfoPanelType.HUMANIZED_MESSAGE, "No Connetion", "Problem conneting to the server.");
                            }

                            public void onSuccess(String result) {
                                InfoPanel.show(InfoPanelType.HUMANIZED_MESSAGE, "Session ID", "Your session id is: " + result);

                                GWT.log("Setting up session", null);
                                String sessionID = result;
                                final long DURATION = 1000 * 60 * 60 * 24 * 14; //duration remembering login. 2 weeks
                                Date expires = new Date(System.currentTimeMillis() + DURATION);
                                Cookies.setCookie("sid", sessionID, expires, null, "/", false);
                            }
                        }
                );  
            }   
        });

        loginVp.add(loginTxt);
        loginVp.add(passTxt);
        loginVp.add(loginBtn);

        return loginVp;
    }
}

RPC Servlet:

RPC 服务程序:

public class LoginServiceImpl extends RemoteServiceServlet implements LoginService{ 
    //Sends back to the client session id
    public String authenticateUser(String login, String password){
        String sessionId = new String();

        // TODO: figure out how to work with session id in GAE/J
        sessionId = "How to get session id?";

        return sessionId;
    }

    public Boolean checkIfSessionIsValid(String sessionId){

        //TODO: figure out how to check user's credentials  
        return true;
    }
}

Any hints in the right direction would be helpful. Thanks.

任何正确方向的提示都会有所帮助。谢谢。

采纳答案by KevMo

Here is how you can get the session in GAE:

以下是在 GAE 中获取会话的方法:

this.getThreadLocalRequest().getSession();

回答by Thilo

Enabling session support gives you a standard Servlet HttpSession.

启用会话支持可为您提供标准的 Servlet HttpSession。

This will be tracked by means of a cookie (called JSESSONID), which is managed by the servlet container under the covers. You do not need to care about the session id.

这将通过 cookie(称为 JESSONID)进行跟踪,该 cookie 由 servlet 容器在幕后管理。您不需要关心会话 ID。

You can then set attributes (server-side) that will be associated with the session (so that you can retrieve them later).

然后,您可以设置将与会话关联的属性(服务器端)(以便您以后可以检索它们)。

HttpServletRequest request = this.getThreadLocalRequest();

HttpSession session = request.getSession();

// in your authentication method
if(isCorrectPassword)
   session.setAttribute("authenticatedUserName", "name");

// later
 if (session.getAttribute("authenticatedUserName") != null)

This should also work with Ajax requests from GWT. Please refer to any Servlet tutorial for more details.

这也应该适用于来自 GWT 的 Ajax 请求。有关更多详细信息,请参阅任何 Servlet 教程。

The drawback of sessions on GAE (compared to other servlet engines) is that they are serialized in and loaded from the database every time, which could be expensive, especially if you put a lot of data in there.

GAE 上的会话(与其他 servlet 引擎相比)的缺点是它们每次都被序列化并从数据库加载,这可能很昂贵,尤其是当您在其中放入大量数据时。