Bom dia.
Eu uso VRaptor e estou com um problema relacionado com Cookie.
A feature é a seguinte, o user tem uma hash fixa no banco e eu pego esse hash e jogo num cookie chamado user. Funciona normal, o problema é que, quando um outro usuário loga (depois de um tempo), o meu usuário recebe o cookie com a hash do outro user. Resumindo, o server esta entregando o hash de OUTRO user.
Eu criei um interceptor e um cookie dispatcher para facilitar a minha vida com a annotation @Cookie.
Eis o meu Interceptor:
@Intercepts(after = NoCacheInterceptor.class)
public class CookieSignInInterceptor implements Interceptor {
private UserCookie userCookie;
public CookieSignInInterceptor(UserCookie userCookie) {
this.userCookie = userCookie;
}
@Override
public void intercept(InterceptorStack stack, ResourceMethod method, Object resourceInstance)
throws InterceptionException {
userCookie.signIn();
stack.next(method, resourceInstance);
}
@Override
public boolean accepts(ResourceMethod method) {
return true;
}
}
E o meu dispatcher:
@Component
public class CookieDispatcher {
private HttpServletResponse response;
public CookieDispatcher(HttpServletResponse response) {
this.response = response;
}
public class CookieBuilder {
private Cookie cookie;
private CookieBuilder(String name, String value) {
cookie = new Cookie(name, value);
}
public CookieBuilder setComment(String purpose) {
cookie.setComment(purpose);
return this;
}
public CookieBuilder setDomain(String pattern) {
cookie.setDomain(pattern);
return this;
}
public CookieBuilder setMaxAge(int expiry, TimeUnit unit) {
Long seconds = unit.toSeconds(expiry);
cookie.setMaxAge(seconds.intValue());
return this;
}
public CookieBuilder setPath(String uri) {
cookie.setPath(uri);
return this;
}
public CookieBuilder setSecure(boolean flag) {
cookie.setSecure(flag);
return this;
}
public CookieBuilder setValue(String newValue) {
cookie.setValue(newValue);
return this;
}
public CookieBuilder setVersion(int v) {
cookie.setVersion(v);
return this;
}
public void dispatch() {
response.addCookie(cookie);
}
}
public CookieBuilder newCookie(String name, String value) {
return new CookieBuilder(name, value).setPath("/");
}
}
UserCookie:
@Component
@RequestScoped
public class UserCookie {
public static final String COOKIE_NAME = "user";
private HttpServletRequest request;
private UserWeb userWeb;
private SignInDAO dao;
private User user;
public UserCookie(HttpServletRequest request, UserWeb userWeb, SignInDAO dao) {
this.request = request;
this.userWeb = userWeb;
this.dao = dao;
user = get();
}
public void signIn() {
if (isCookieValid())
userWeb.signIn(user);
}
private boolean isCookieValid() {
return userWeb.notActive() && user != null;
}
private User get() {
Cookie[] cookies = request.getCookies();
if (notEmpty(cookies))
for (Cookie cookie : cookies)
if (COOKIE_NAME.equals(cookie.getName()))
return getUser(cookie);
return null;
}
private User getUser(Cookie cookie) {
if (notEmpty(cookie.getValue()))
return dao.getUser(cookie.getValue());
else
return null;
}
}
Conseguem imaginar como que o server esta estregando o hash errado para o usuario errado ?