Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

As a User I want to be auto-logged when I return to web application

  • Loading branch information...
commit 59cb50d433a79a73b1020b488d5a13a7355da0d3 1 parent b9a4191
@tdziurko authored
View
8 src/main/java/pl/tomaszdziurko/wicket/WicketApplication.java
@@ -5,6 +5,7 @@
import org.apache.wicket.request.Request;
import org.apache.wicket.request.Response;
import pl.tomaszdziurko.wicket.service.CookieService;
+import pl.tomaszdziurko.wicket.service.SessionProvider;
import pl.tomaszdziurko.wicket.service.UserService;
import pl.tomaszdziurko.wicket.view.HomePage;
import pl.tomaszdziurko.wicket.view.LoginPage;
@@ -13,6 +14,7 @@
private UserService userService = new UserService();
private CookieService cookieService = new CookieService();
+ private SessionProvider sessionProvider = new SessionProvider(userService, cookieService);
@Override
public Class<HomePage> getHomePage() {
@@ -32,7 +34,7 @@ public static WicketApplication get() {
@Override
public Session newSession(Request request, Response response) {
- return new UserSession(request);
+ return sessionProvider.createNewSession(request);
}
@@ -45,5 +47,9 @@ public CookieService getCookieService() {
return cookieService;
}
+ public SessionProvider getSessionProvider() {
+ return sessionProvider;
+ }
+
}
View
41 src/main/java/pl/tomaszdziurko/wicket/service/SessionProvider.java
@@ -0,0 +1,41 @@
+package pl.tomaszdziurko.wicket.service;
+
+import org.apache.wicket.protocol.http.WebSession;
+import org.apache.wicket.request.Request;
+import pl.tomaszdziurko.wicket.UserSession;
+import pl.tomaszdziurko.wicket.model.User;
+
+import javax.servlet.http.Cookie;
+
+public class SessionProvider {
+
+ public static final int REMEMBER_ME_DURATION_IN_DAYS = 30;
+ public static final String REMEMBER_ME_LOGIN_COOKIE = "loginCookie";
+ public static final String REMEMBER_ME_PASSWORD_COOKIE = "passwordCookie";
+
+ private UserService userService;
+ private CookieService cookieService;
+
+ 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;
+ }
+}
View
3  src/main/java/pl/tomaszdziurko/wicket/view/HomePage.html
@@ -15,6 +15,9 @@
<a href="#" wicket:id="loginLink">Please login</a>
</div>
<div class="span8 offset2">
+ <div wicket:id="feedback"></div>
+ </div>
+ <div class="span8 offset2">
<br/>
<h3>Welcome <span wicket:id="username"/></h3>
</div>
View
3  src/main/java/pl/tomaszdziurko/wicket/view/HomePage.java
@@ -3,6 +3,7 @@
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.link.Link;
+import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.request.mapper.parameter.PageParameters;
import pl.tomaszdziurko.wicket.UserSession;
@@ -12,6 +13,8 @@
public HomePage(final PageParameters parameters) {
super(parameters);
+ add(new FeedbackPanel("feedback"));
+
Link<Void> logoutLink = new Link<Void>("logout") {
@Override
public void onClick() {
View
8 src/main/java/pl/tomaszdziurko/wicket/view/LoginPage.java
@@ -13,11 +13,11 @@
import pl.tomaszdziurko.wicket.service.CookieService;
import pl.tomaszdziurko.wicket.service.UserService;
-public class LoginPage extends BasePage {
+import static pl.tomaszdziurko.wicket.service.SessionProvider.REMEMBER_ME_DURATION_IN_DAYS;
+import static pl.tomaszdziurko.wicket.service.SessionProvider.REMEMBER_ME_LOGIN_COOKIE;
+import static pl.tomaszdziurko.wicket.service.SessionProvider.REMEMBER_ME_PASSWORD_COOKIE;
- public static final int REMEMBER_ME_DURATION_IN_DAYS = 30;
- private static final String REMEMBER_ME_LOGIN_COOKIE = "loginCookie";
- private static final String REMEMBER_ME_PASSWORD_COOKIE = "passwordCookie";
+public class LoginPage extends BasePage {
private String login;
private String password;
View
4 src/main/webapp/WEB-INF/web.xml
@@ -18,4 +18,8 @@
<filter-name>wicket.rememberme</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
+
+ <session-config>
+ <session-timeout>1</session-timeout>
+ </session-config>
</web-app>
Please sign in to comment.
Something went wrong with that request. Please try again.