io.lsn.spring.auth.transport.cookie.CookieAuthHelper Maven / Gradle / Ivy
package io.lsn.spring.auth.transport.cookie;
import io.lsn.spring.auth.entity.User;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
public class CookieAuthHelper {
/**
* build cookie object
*
* @param request
* @param user
* @param toRemove
* @return
*/
public static Cookie buildCookie(HttpServletRequest request, User user, boolean toRemove) {
Cookie cookie = new Cookie("X-Auth-Token", toRemove ? null : user.getApiToken().getToken());
cookie.setHttpOnly(true);
cookie.setSecure(request.isSecure());
cookie.setPath("/");
cookie.setMaxAge(toRemove ? 0 : -1);
return cookie;
}
}