1 package de.juplo.yourshouter;
4 import java.util.Collections;
5 import javax.servlet.http.HttpServletRequest;
6 import javax.servlet.http.HttpServletResponse;
7 import org.slf4j.Logger;
8 import org.slf4j.LoggerFactory;
10 import org.springframework.social.connect.UsersConnectionRepository;
11 import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
15 * Intercepts all requests to handle the user-cookie.
19 public final class UserCookieInterceptor extends HandlerInterceptorAdapter
21 private final static Logger LOG =
22 LoggerFactory.getLogger(UserCookieInterceptor.class);
25 private final UsersConnectionRepository repository;
29 * Creates an instance of this class, that uses the given instance of
30 * {@link UsersConnectionRepository}.
33 * The instance of {@link UsersConnectionRepository} to use.
35 public UserCookieInterceptor(UsersConnectionRepository repository)
37 this.repository = repository;
42 * Before a request is handled, the current user is loaded from the cookie,
43 * if the cookie is present and the user is known. If the user is not known,
44 * the cookie is removed.
47 * The {@link HttpServletRequest} that is intercepted.
49 * The {@link HttpServletResponse} that is intercepted.
51 * The handler, that handles the intercepted request.
53 * Always <code>true</code>, to indicate, that the intercepted request
54 * should be handled normally.
57 public boolean preHandle(
58 HttpServletRequest request,
59 HttpServletResponse response,
63 String user = UserCookieGenerator.INSTANCE.readCookieValue(request);
67 .findUserIdsConnectedTo("facebook", Collections.singleton(user))
71 LOG.info("loading user {} from cookie", user);
72 SecurityContext.setCurrentUser(user);
77 LOG.warn("user {} is not known!", user);
78 UserCookieGenerator.INSTANCE.removeCookie(response);
85 * After a request, the user is removed from the security-context.
88 * The {@link HttpServletRequest} that is intercepted.
90 * The {@link HttpServletResponse} that is intercepted.
92 * The handler, that handles the intercepted request.
94 * If an exception was thrown during the handling of this request, it is
95 * handed in through this parameter.
98 public void afterCompletion(
99 HttpServletRequest request,
100 HttpServletResponse response,
105 SecurityContext.remove();