做直播网站找哪个,天津北京网站建设公司哪家好,无线设置网站,购物网站开发和运行环境在Web应用程序中#xff0c;具有“记住我”功能非常普遍#xff0c;该功能使用户每次访问我们的网站时都能自动登录。 可以使用Spring Security来实现这种功能#xff0c;但我认为将基于请求的身份验证框架与基于组件的Web框架结合使用并不是最好的主意。 这两个世界不能很好… 在Web应用程序中具有“记住我”功能非常普遍该功能使用户每次访问我们的网站时都能自动登录。 可以使用Spring Security来实现这种功能但我认为将基于请求的身份验证框架与基于组件的Web框架结合使用并不是最好的主意。 这两个世界不能很好地融合在一起所以我更喜欢使用我自己的烘焙解决方案我将在下面介绍。 基础项目 我们从一个简单的Web应用程序开始该应用程序使用最新的仍很热门的Apache Wicket 6编写。 您可以从GitHub下载完整的源代码并使用mvn clean compile jettyrun启动应用程序。 基本应用程序包含两个页面 主页显示已登录和未登录用户的欢迎消息或者注销或登录链接。 登录页面允许用户基于简单的用户内存集合进行登录。 一些有效的登录名/密码对John / johnLisa / lisaTom / tom。 记住我的功能 实现“记住我”功能的标准方法如下 询问用户是否希望他将来被记住并自动登录。 如果是这样请在他的计算机上使用登录名和密码保存cookie。 对于每个访问我们网站的新用户请检查是否存在步骤2中的cookie如果存在则为自动登录用户。 当他手动注销时请删除cookie以便可以清除用于自动登录的数据。 第二点需要一些解释。 在此示例应用程序中我们将保存登录信息而不是哈希值即 cookie中未加密的密码。 在实际情况下这是不可接受的。 取而代之的是您应该考虑存储散列和加盐的密码这样即使有人拦截了用户Cookie密码仍然是秘密的需要更多的工作来对其进行解码。 更新 Micha Matoka发布了两个非常有趣的链接这些链接如何在实际系统中完成。 这些方法甚至不使用密码或密码哈希。 有关更多详细信息请查看此帖子下方的他的评论。 第1步作为用户我想决定是否要使用“记住我”功能 链接以提交此步骤 为了允许用户通知应用程序他想使用“记住我”功能我们只需在登录页面添加一个复选框即可。 因此我们需要稍微修改LoginPage Java和html文件突出显示新内容 form wicket:idform classform-horizontalfieldsetlegendPlease login/legend/fieldsetdiv classcontrol-groupdiv wicket:idfeedback/div/divdiv classcontrol-grouplabel classcontrol-label forloginLogin/labeldiv classcontrolsinput typetext idlogin wicket:idlogin //div/divdiv classcontrol-grouplabel classcontrol-label forpasswordPassword/labeldiv classcontrolsinput typepassword idpassword wicket:idpassword //div/divdiv classcontrol-groupdiv classcontrolslabel classcheckboxinput typecheckbox wicket:idrememberMe Remember me on this computer/label/div/divdiv classform-actionsinput typesubmit wicket:idsubmit valueLogin titleLogin classbtn btn-primary//div/formprivate String login;private String password;private boolean rememberMe;public LoginPage() {FormVoid loginForm new FormVoid(form);add(loginForm);loginForm.add(new FeedbackPanel(feedback));loginForm.add(new RequiredTextFieldString(login, new PropertyModelString(this, login)));loginForm.add(new PasswordTextField(password, new PropertyModelString(this, password)));loginForm.add(new CheckBox(rememberMe, new PropertyModelBoolean(this, rememberMe)));Button submit new Button(submit) {// (...)};loginForm.add(submit);} 现在我们准备好下一步。 步骤2作为系统我想将登录名和密码保存在Cookie中 链接以提交此步骤 首先我们需要一个CookieService它将封装负责处理cookie的所有逻辑在需要时保存列出和清除cookie。 代码非常简单我们使用WebResponse和WebRequest类来修改用户浏览器中的cookie。 public class CookieService {public Cookie loadCookie(Request request, String cookieName) {ListCookie cookies ((WebRequest) request).getCookies();if (cookies null) {return null;}for (Cookie cookie : cookies) {if(cookie.getName().equals(cookieName)) {return cookie;}}return null;}public void saveCookie(Response response, String cookieName, String cookieValue, int expiryTimeInDays) {Cookie cookie new Cookie(cookieName, cookieValue);cookie.setMaxAge((int) TimeUnit.DAYS.toSeconds(expiryTimeInDays));((WebResponse)response).addCookie(cookie);}public void removeCookieIfPresent(Request request, Response response, String cookieName) {Cookie cookie loadCookie(request, cookieName);if(cookie ! null) {((WebResponse)response).clearCookie(cookie);}}
} 然后当用户在LoginPage上选中“记住我”时我们必须在其浏览器中保存cookie Button submit new Button(submit) {Overridepublic void onSubmit() {UserService userService WicketApplication.get().getUserService();User user userService.findByLoginAndPassword(login, password);if(user null) {error(Invalid login and/or password. Please try again.);}else {UserSession.get().setUser(user);if(rememberMe) {CookieService cookieService WicketApplication.get().getCookieService();cookieService.saveCookie(getResponse(), REMEMBER_ME_LOGIN_COOKIE, user.getLogin(), REMEMBER_ME_DURATION_IN_DAYS);cookieService.saveCookie(getResponse(), REMEMBER_ME_PASSWORD_COOKIE, user.getPassword(), REMEMBER_ME_DURATION_IN_DAYS);}setResponsePage(HomePage.class);}}}; 第3步作为用户我想在返回Web应用程序时自动登录 链接以提交此步骤 为了检查用户进入我们的应用程序是否是“使用户自动登录”我们必须丰富负责创建新用户会话的逻辑。 当前它是在WicketApplication类中完成的该类在被请求时创建新的WebSession实例。 因此每次创建新会话时我们都必须检查cookie是否存在以及它们是否为有效的用户名/密码对请自动登录该用户。 因此让我们开始将与会话相关的逻辑提取到名为SessionProvider的单独的类中。 它将需要UserService和CookieService来检查现有用户和cookie因此我们将它们作为构造函数中的引用传递。 public class WicketApplication extends WebApplication {private UserService userService new UserService();private CookieService cookieService new CookieService();private SessionProvider sessionProvider new SessionProvider(userService, cookieService);Overridepublic Session newSession(Request request, Response response) {return sessionProvider.createNewSession(request);}
} SessionProvider的作用是创建新的UserSession检查是否存在正确的cookie如果存在则设置登录用户。 此外我们添加了反馈消息以通知用户他已被自动记录。 因此让我们看一下代码 public class SessionProvider {public SessionProvider(UserService userService, CookieService cookieService) {this.userService userService;this.cookieService cookieService;}public WebSession createNewSession(Request request) {UserSession session new UserSession(request);Cookie loginCookie cookieService.loadCookie(request, REMEMBER_ME_LOGIN_COOKIE);Cookie passwordCookie cookieService.loadCookie(request, REMEMBER_ME_PASSWORD_COOKIE);if(loginCookie ! null passwordCookie ! null) {User user userService.findByLoginAndPassword(loginCookie.getValue(), passwordCookie.getValue());if(user ! null) {session.setUser(user);session.info(You were automatically logged in.);}}return session;}
} 为了在HomePage.java上显示反馈消息我们必须在该处添加FeedbackPanel但是为了简洁起见我将在本文中省略它。 您可以阅读commit来检查如何做。 因此经过三步我们应该使“记住我”成为可能。 要快速检查它请通过添加以下内容来修改web.xml文件中的会话超时 session-configsession-timeout1/session-timeout/session-config 然后启动应用程序mvn clean compile jettyrun 进入登录页面登录关闭浏览器并在1分钟后会话终止时在http// localhost8080上再次打开它。 您应该会看到以下内容 这样就可以了。 但是我们还需要做一件事允许用户删除Cookie并关闭自动登录。 第4步作为用户我希望能够注销并清除我的Cookie 链接以提交此步骤 在最后一步我们必须允许用户清除其数据并禁用其帐户的“记住我”。 这将通过在用户明确单击“注销”链接时清除两个cookie来实现。 LinkVoid logoutLink new LinkVoid(logout) {Overridepublic void onClick() {CookieService cookieService WicketApplication.get().getCookieService();cookieService.removeCookieIfPresent(getRequest(), getResponse(), SessionProvider.REMEMBER_ME_LOGIN_COOKIE);cookieService.removeCookieIfPresent(getRequest(), getResponse(), SessionProvider.REMEMBER_ME_PASSWORD_COOKIE);UserSession.get().setUser(null);UserSession.get().invalidate();}};logoutLink.setVisible(UserSession.get().userLoggedIn());add(logoutLink); 摘要 就是这样。 在此端口中我们已经在使用Apache Wicket编写的Web应用程序中实现了简单的“记住我”功能而无需使用任何外部身份验证库。 祝您编程愉快别忘了分享 参考来自Code Hard Go Pro博客的JCG合作伙伴 Tomasz Dziurko的Apache Wicket中的“记住我”功能 。 翻译自: https://www.javacodegeeks.com/2012/09/apache-wicket-remember-me-functionality.html