Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

As a System I want to save login and password in cookies

  • Loading branch information...
commit b9a419161871b47821171790f443cf92869ac631 1 parent c5e91b3
@tdziurko authored
View
6 pom.xml
@@ -77,6 +77,12 @@
<artifactId>guava</artifactId>
<version>11.0</version>
</dependency>
+ <dependency>
+ <groupId>javax.servlet</groupId>
+ <artifactId>servlet-api</artifactId>
+ <version>2.5</version>
+ <scope>provided</scope>
+ </dependency>
</dependencies>
View
6 src/main/java/pl/tomaszdziurko/wicket/WicketApplication.java
@@ -4,6 +4,7 @@
import org.apache.wicket.protocol.http.WebApplication;
import org.apache.wicket.request.Request;
import org.apache.wicket.request.Response;
+import pl.tomaszdziurko.wicket.service.CookieService;
import pl.tomaszdziurko.wicket.service.UserService;
import pl.tomaszdziurko.wicket.view.HomePage;
import pl.tomaszdziurko.wicket.view.LoginPage;
@@ -11,6 +12,7 @@
public class WicketApplication extends WebApplication {
private UserService userService = new UserService();
+ private CookieService cookieService = new CookieService();
@Override
public Class<HomePage> getHomePage() {
@@ -39,5 +41,9 @@ public UserService getUserService() {
return userService;
}
+ public CookieService getCookieService() {
+ return cookieService;
+ }
+
}
View
44 src/main/java/pl/tomaszdziurko/wicket/service/CookieService.java
@@ -0,0 +1,44 @@
+package pl.tomaszdziurko.wicket.service;
+
+import org.apache.wicket.request.Request;
+import org.apache.wicket.request.Response;
+import org.apache.wicket.request.http.WebRequest;
+import org.apache.wicket.request.http.WebResponse;
+
+import javax.servlet.http.Cookie;
+import java.util.List;
+import java.util.concurrent.TimeUnit;
+
+public class CookieService {
+
+ public Cookie loadCookie(Request request, String cookieName) {
+
+ List<Cookie> 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);
+ }
+ }
+}
View
12 src/main/java/pl/tomaszdziurko/wicket/view/LoginPage.java
@@ -10,10 +10,15 @@
import pl.tomaszdziurko.wicket.UserSession;
import pl.tomaszdziurko.wicket.WicketApplication;
import pl.tomaszdziurko.wicket.model.User;
+import pl.tomaszdziurko.wicket.service.CookieService;
import pl.tomaszdziurko.wicket.service.UserService;
public class LoginPage extends BasePage {
+ 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";
+
private String login;
private String password;
private boolean rememberMe;
@@ -40,6 +45,13 @@ public void onSubmit() {
}
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);
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.