File tree Expand file tree Collapse file tree
java/pl/tomaszdziurko/wicket Expand file tree Collapse file tree Original file line number Diff line number Diff line change 55import org .apache .wicket .request .Request ;
66import org .apache .wicket .request .Response ;
77import pl .tomaszdziurko .wicket .service .CookieService ;
8+ import pl .tomaszdziurko .wicket .service .SessionProvider ;
89import pl .tomaszdziurko .wicket .service .UserService ;
910import pl .tomaszdziurko .wicket .view .HomePage ;
1011import pl .tomaszdziurko .wicket .view .LoginPage ;
@@ -13,6 +14,7 @@ public class WicketApplication extends WebApplication {
1314
1415 private UserService userService = new UserService ();
1516 private CookieService cookieService = new CookieService ();
17+ private SessionProvider sessionProvider = new SessionProvider (userService , cookieService );
1618
1719 @ Override
1820 public Class <HomePage > getHomePage () {
@@ -32,7 +34,7 @@ public static WicketApplication get() {
3234
3335 @ Override
3436 public Session newSession (Request request , Response response ) {
35- return new UserSession (request );
37+ return sessionProvider . createNewSession (request );
3638 }
3739
3840
@@ -45,5 +47,9 @@ public CookieService getCookieService() {
4547 return cookieService ;
4648 }
4749
50+ public SessionProvider getSessionProvider () {
51+ return sessionProvider ;
52+ }
53+
4854
4955}
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change @@ -14,6 +14,9 @@ <h1>Home Page</h1>
1414 < a href ="# " wicket:id ="logout "> Logout</ a >
1515 < a href ="# " wicket:id ="loginLink "> Please login</ a >
1616 </ div >
17+ < div class ="span8 offset2 ">
18+ < div wicket:id ="feedback "> </ div >
19+ </ div >
1720 < div class ="span8 offset2 ">
1821 < br />
1922 < h3 > Welcome < span wicket:id ="username "/> </ h3 >
Original file line number Diff line number Diff line change 33import org .apache .wicket .markup .html .basic .Label ;
44import org .apache .wicket .markup .html .link .BookmarkablePageLink ;
55import org .apache .wicket .markup .html .link .Link ;
6+ import org .apache .wicket .markup .html .panel .FeedbackPanel ;
67import org .apache .wicket .request .mapper .parameter .PageParameters ;
78import pl .tomaszdziurko .wicket .UserSession ;
89
@@ -12,6 +13,8 @@ public class HomePage extends BasePage {
1213 public HomePage (final PageParameters parameters ) {
1314 super (parameters );
1415
16+ add (new FeedbackPanel ("feedback" ));
17+
1518 Link <Void > logoutLink = new Link <Void >("logout" ) {
1619 @ Override
1720 public void onClick () {
Original file line number Diff line number Diff line change 1313import pl .tomaszdziurko .wicket .service .CookieService ;
1414import pl .tomaszdziurko .wicket .service .UserService ;
1515
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 ;
1719
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 {
2121
2222 private String login ;
2323 private String password ;
Original file line number Diff line number Diff line change 1818 <filter-name >wicket.rememberme</filter-name >
1919 <url-pattern >/*</url-pattern >
2020 </filter-mapping >
21+
22+ <session-config >
23+ <session-timeout >1</session-timeout >
24+ </session-config >
2125</web-app >
You can’t perform that action at this time.
0 commit comments