如何使用play 1.2.7 java框架实现登录和注销功能

时间:2013-11-07 10:50:09

标签: java session playframework playframework-1.x

我正在使用play 1.2.7来创建登录和注销功能,当用户登录我将为他创建会话时,通过使用会话对象,我的问题是我完成注销后将重定向到登录页面但是当用户点击浏览器后退按钮页面将刷新,然后再次登录

这是我登录用户时的代码。

public static void login() throws Throwable {
        Http.Cookie remember = request.cookies.get("rememberme");
        if(remember != null) {
            int firstIndex = remember.value.indexOf("-");
            int lastIndex = remember.value.lastIndexOf("-");
            if (lastIndex > firstIndex) {
                String sign = remember.value.substring(0, firstIndex);
                String restOfCookie = remember.value.substring(firstIndex + 1);
                String username = remember.value.substring(firstIndex + 1, lastIndex);
                String time = remember.value.substring(lastIndex + 1);
                Date expirationDate = new Date(Long.parseLong(time)); // surround with try/catch?
                Date now = new Date();
                if (expirationDate == null || expirationDate.before(now)) {
                    logout();
                }
                if(Crypto.sign(restOfCookie).equals(sign)) {
                    session.put("username", username);
                    redirectToOriginalURL();
                }
            }
        }
        flash.keep("url");
        render();
    }

当用户注销时,这是我的注销码:

 public static void logout() throws Throwable {
        Security.invoke("onDisconnect");
        session.clear();
        response.removeCookie("rememberme");
        Security.invoke("onDisconnected");
        flash.success("secure.logout");
        login();
    }

请救我这个问题如何解决这个问题。谢谢。

1 个答案:

答案 0 :(得分:1)

使用安全模块是可行的方法:http://www.playframework.com/documentation/1.2/secure

仅供参考,有些模块可以扩展安全模块并支持基于角色的授权: