Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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

  • Loading branch information...
commit 59cb50d433a79a73b1020b488d5a13a7355da0d3 1 parent b9a4191
Tomasz Dziurko authored September 14, 2012
8  src/main/java/pl/tomaszdziurko/wicket/WicketApplication.java
@@ -5,6 +5,7 @@
5 5
 import org.apache.wicket.request.Request;
6 6
 import org.apache.wicket.request.Response;
7 7
 import pl.tomaszdziurko.wicket.service.CookieService;
  8
+import pl.tomaszdziurko.wicket.service.SessionProvider;
8 9
 import pl.tomaszdziurko.wicket.service.UserService;
9 10
 import pl.tomaszdziurko.wicket.view.HomePage;
10 11
 import pl.tomaszdziurko.wicket.view.LoginPage;
@@ -13,6 +14,7 @@
13 14
 
14 15
     private UserService userService = new UserService();
15 16
     private CookieService cookieService = new CookieService();
  17
+    private SessionProvider sessionProvider = new SessionProvider(userService, cookieService);
16 18
 
17 19
     @Override
18 20
     public Class<HomePage> getHomePage() {
@@ -32,7 +34,7 @@ public static WicketApplication get() {
32 34
 
33 35
     @Override
34 36
     public Session newSession(Request request, Response response) {
35  
-        return new UserSession(request);
  37
+        return sessionProvider.createNewSession(request);
36 38
     }
37 39
 
38 40
 
@@ -45,5 +47,9 @@ public CookieService getCookieService() {
45 47
         return cookieService;
46 48
     }
47 49
 
  50
+    public SessionProvider getSessionProvider() {
  51
+        return sessionProvider;
  52
+    }
  53
+
48 54
 
49 55
 }
41  src/main/java/pl/tomaszdziurko/wicket/service/SessionProvider.java
... ...
@@ -0,0 +1,41 @@
  1
+package pl.tomaszdziurko.wicket.service;
  2
+
  3
+import org.apache.wicket.protocol.http.WebSession;
  4
+import org.apache.wicket.request.Request;
  5
+import pl.tomaszdziurko.wicket.UserSession;
  6
+import pl.tomaszdziurko.wicket.model.User;
  7
+
  8
+import javax.servlet.http.Cookie;
  9
+
  10
+public class SessionProvider {
  11
+
  12
+    public static final int REMEMBER_ME_DURATION_IN_DAYS = 30;
  13
+    public static final String REMEMBER_ME_LOGIN_COOKIE = "loginCookie";
  14
+    public static final String REMEMBER_ME_PASSWORD_COOKIE = "passwordCookie";
  15
+
  16
+    private UserService userService;
  17
+    private CookieService cookieService;
  18
+
  19
+    public SessionProvider(UserService userService, CookieService cookieService) {
  20
+        this.userService = userService;
  21
+        this.cookieService = cookieService;
  22
+    }
  23
+
  24
+    public WebSession createNewSession(Request request) {
  25
+        UserSession session = new UserSession(request);
  26
+
  27
+        Cookie loginCookie = cookieService.loadCookie(request, REMEMBER_ME_LOGIN_COOKIE);
  28
+        Cookie passwordCookie = cookieService.loadCookie(request, REMEMBER_ME_PASSWORD_COOKIE);
  29
+
  30
+        if(loginCookie != null && passwordCookie != null) {
  31
+            User user = userService.findByLoginAndPassword(loginCookie.getValue(), passwordCookie.getValue());
  32
+
  33
+            if(user != null) {
  34
+                session.setUser(user);
  35
+                session.info("You were automatically logged in.");
  36
+            }
  37
+        }
  38
+
  39
+        return session;
  40
+    }
  41
+}
3  src/main/java/pl/tomaszdziurko/wicket/view/HomePage.html
@@ -15,6 +15,9 @@
15 15
             <a href="#" wicket:id="loginLink">Please login</a>
16 16
         </div>
17 17
         <div class="span8 offset2">
  18
+            <div wicket:id="feedback"></div>
  19
+        </div>
  20
+        <div class="span8 offset2">
18 21
             <br/>
19 22
             <h3>Welcome <span wicket:id="username"/></h3>
20 23
         </div>
3  src/main/java/pl/tomaszdziurko/wicket/view/HomePage.java
@@ -3,6 +3,7 @@
3 3
 import org.apache.wicket.markup.html.basic.Label;
4 4
 import org.apache.wicket.markup.html.link.BookmarkablePageLink;
5 5
 import org.apache.wicket.markup.html.link.Link;
  6
+import org.apache.wicket.markup.html.panel.FeedbackPanel;
6 7
 import org.apache.wicket.request.mapper.parameter.PageParameters;
7 8
 import pl.tomaszdziurko.wicket.UserSession;
8 9
 
@@ -12,6 +13,8 @@
12 13
     public HomePage(final PageParameters parameters) {
13 14
         super(parameters);
14 15
 
  16
+        add(new FeedbackPanel("feedback"));
  17
+
15 18
         Link<Void> logoutLink = new Link<Void>("logout") {
16 19
             @Override
17 20
             public void onClick() {
8  src/main/java/pl/tomaszdziurko/wicket/view/LoginPage.java
@@ -13,11 +13,11 @@
13 13
 import pl.tomaszdziurko.wicket.service.CookieService;
14 14
 import pl.tomaszdziurko.wicket.service.UserService;
15 15
 
16  
-public class LoginPage extends BasePage {
  16
+import static pl.tomaszdziurko.wicket.service.SessionProvider.REMEMBER_ME_DURATION_IN_DAYS;
  17
+import static pl.tomaszdziurko.wicket.service.SessionProvider.REMEMBER_ME_LOGIN_COOKIE;
  18
+import static pl.tomaszdziurko.wicket.service.SessionProvider.REMEMBER_ME_PASSWORD_COOKIE;
17 19
 
18  
-    public static final int REMEMBER_ME_DURATION_IN_DAYS = 30;
19  
-    private static final String REMEMBER_ME_LOGIN_COOKIE = "loginCookie";
20  
-    private static final String REMEMBER_ME_PASSWORD_COOKIE = "passwordCookie";
  20
+public class LoginPage extends BasePage {
21 21
 
22 22
     private String login;
23 23
     private String password;
4  src/main/webapp/WEB-INF/web.xml
@@ -18,4 +18,8 @@
18 18
 		<filter-name>wicket.rememberme</filter-name>
19 19
 		<url-pattern>/*</url-pattern>
20 20
 	</filter-mapping>
  21
+
  22
+    <session-config>
  23
+        <session-timeout>1</session-timeout>
  24
+    </session-config>
21 25
 </web-app>

0 notes on commit 59cb50d

Please sign in to comment.
Something went wrong with that request. Please try again.