Web applications 这是a'的合法实现吗;记住我';我的web应用程序的功能?
我正试图在我的web应用程序中添加一个“记住我”功能,让用户在重新启动浏览器之间保持登录状态。我想我得到了大部分。我在后台使用谷歌应用引擎,它让我可以使用java servlet。下面是一些要演示的伪代码:Web applications 这是a'的合法实现吗;记住我';我的web应用程序的功能?,web-applications,session,servlets,Web Applications,Session,Servlets,我正试图在我的web应用程序中添加一个“记住我”功能,让用户在重新启动浏览器之间保持登录状态。我想我得到了大部分。我在后台使用谷歌应用引擎,它让我可以使用java servlet。下面是一些要演示的伪代码: public class MyServlet { public void handleRequest() { if (getThreadLocalRequest().getSession().getAttribute("user") != null) {
public class MyServlet {
public void handleRequest() {
if (getThreadLocalRequest().getSession().getAttribute("user") != null) {
// User already has session running for them.
}
else {
// No session, but check if they chose 'remember me' during
// their initial login, if so we can have them 'auto log in'
// now.
Cookie[] cookies = getThreadLocalRequest().getCookies();
if (cookies.find("rememberMePlz").exists()) {
// The value of this cookie is the cookie id, which is a
// unique string that is in no way based upon the user's
// name/email/id, and is hard to randomly generate.
String cookieid = cookies.find("rememberMePlz").value();
// Get the user object associated with this cookie id from
// the data store, would probably be a two-step process like:
//
// select * from cookies where cookieid = 'cookieid';
// select * from users where userid = 'userid fetched from above select';
User user = DataStore.getUserByCookieId(cookieid);
if (user != null) {
// Start session for them.
getThreadLocalRequest().getSession()
.setAttribute("user", user);
}
else {
// Either couldn't find a matching cookie with the
// supplied id, or maybe we expired the cookie on
// our side or blocked it.
}
}
}
}
}
// On first login, if user wanted us to remember them, we'd generate
// an instance of this object for them in the data store. We send the
// cookieid value down to the client and they persist it on their side
// in the "rememberMePlz" cookie.
public class CookieLong {
private String mCookieId;
private String mUserId;
private long mExpirationDate;
}
好吧,这一切都是有道理的。唯一可怕的是,如果有人发现了饼干的价值,会发生什么?恶意个人可以在其浏览器中设置该cookie并访问我的网站,并以与之相关联的用户身份登录
同样,我想这就是为什么cookie ID必须很难随机生成的原因,因为恶意用户不必窃取某人的cookie-他们可以随机分配cookie值,并以与该cookie关联的任何用户(如果有)的身份登录,对吗
可怕的是,我觉得我至少应该在客户端cookie中包含用户名,这样当它向服务器显示时,除非数据存储中的用户名+cookieid匹配,否则我不会自动登录
任何评论都会很好,我对这一点还不熟悉,并试图找出最佳实践。我并不是在写一个包含任何敏感个人信息的网站,但我还是想尽量减少滥用的可能性
谢谢
唯一可怕的是,如果有人发现了饼干的价值,会发生什么?恶意个人可以在其浏览器中设置该cookie并访问我的网站,并以与之相关联的用户身份登录
没错。这也适用于所有“标准”会话cookie,例如JSP/Servlet webapps中的jsessionid
,以及PHP webapps中的PHPSESSID
,等等。然而,它们的寿命如此之短,以至于不会造成伤害。对于长寿命cookie,只需确保cookie值是唯一的(使用DB的PK/UK)、长的(最多255个字符)、难以猜测的(没有模式、没有MD5哈希),并且可以包含所有可打印的ASCII字符(0x20-0x7E范围)
您还经常看到一个选项“只在这个IP地址上记住我”,这样它基本上被锁定到一个唯一的IP地址。你可以通过。听起来不错,到底什么是“DB的PK/UK”?这是一个实用程序库还是生成高质量随机cookie ID的东西?PK=主键(隐式唯一),UK=唯一键。当您尝试插入副本时,数据库将出错。利用它:)