本文整理匯總了Java中javax.servlet.http.HttpServletResponse.addCookie方法的典型用法代碼示例。如果您正苦於以下問題:Java HttpServletResponse.addCookie方法的具體用法?Java HttpServletResponse.addCookie怎麽用?Java HttpServletResponse.addCookie使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類javax.servlet.http.HttpServletResponse
的用法示例。
在下文中一共展示了HttpServletResponse.addCookie方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: doFilterInternal
import javax.servlet.http.HttpServletResponse; //導入方法依賴的package包/類
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
// Spring put the CSRF token in session attribute "_csrf"
CsrfToken csrfToken = (CsrfToken) request.getAttribute("_csrf");
// Send the cookie only if the token has changed
String actualToken = request.getHeader("X-CSRF-TOKEN");
if (actualToken == null || !actualToken.equals(csrfToken.getToken())) {
// Session cookie that will be used by AngularJS
String pCookieName = "CSRF-TOKEN";
Cookie cookie = new Cookie(pCookieName, csrfToken.getToken());
cookie.setMaxAge(-1);
cookie.setHttpOnly(false);
cookie.setPath("/");
response.addCookie(cookie);
}
filterChain.doFilter(request, response);
}
示例2: logout
import javax.servlet.http.HttpServletResponse; //導入方法依賴的package包/類
/**
* 注銷
*
* @param session
* @param response
*/
@RequestMapping("/logout")
public void logout(HttpSession session, HttpServletResponse response, HttpServletRequest request) {
session.removeAttribute(WebConst.LOGIN_SESSION_KEY);
Cookie cookie = new Cookie(WebConst.USER_IN_COOKIE, "");
cookie.setValue(null);
cookie.setMaxAge(0);// 立即銷毀cookie
cookie.setPath("/");
response.addCookie(cookie);
try {
response.sendRedirect("/admin/login");
} catch (IOException e) {
e.printStackTrace();
LOGGER.error("注銷失敗", e);
}
}
示例3: addCookie
import javax.servlet.http.HttpServletResponse; //導入方法依賴的package包/類
/**
* Adds the cookie, taking into account {@link RememberMeCredential#REQUEST_PARAMETER_REMEMBER_ME}
* in the request.
*
* @param request the request
* @param response the response
* @param cookieValue the cookie value
*/
public void addCookie(final HttpServletRequest request, final HttpServletResponse response, final String cookieValue) {
final String theCookieValue = this.casCookieValueManager.buildCookieValue(cookieValue, request);
if (!StringUtils.hasText(request.getParameter(RememberMeCredential.REQUEST_PARAMETER_REMEMBER_ME))) {
super.addCookie(response, theCookieValue);
} else {
final Cookie cookie = createCookie(theCookieValue);
cookie.setMaxAge(this.rememberMeMaxAge);
if (isCookieSecure()) {
cookie.setSecure(true);
}
if (isCookieHttpOnly()) {
final Method setHttpOnlyMethod = ReflectionUtils.findMethod(Cookie.class, "setHttpOnly", boolean.class);
if(setHttpOnlyMethod != null) {
cookie.setHttpOnly(true);
} else {
logger.debug("Cookie cannot be marked as HttpOnly; container is not using servlet 3.0.");
}
}
response.addCookie(cookie);
}
}
開發者ID:hsj-xiaokang,項目名稱:springboot-shiro-cas-mybatis,代碼行數:31,代碼來源:CookieRetrievingCookieGenerator.java
示例4: setCookie
import javax.servlet.http.HttpServletResponse; //導入方法依賴的package包/類
/**
* 設置 Cookie
*
* @param name 名稱
* @param value 值
* @param maxAge 生存時間(單位秒)
* @param path 路徑
*/
public static void setCookie(HttpServletResponse response, String name, String value, String path, int maxAge) {
Cookie cookie = new Cookie(name, null);
cookie.setPath(path);
cookie.setMaxAge(maxAge);
try {
cookie.setValue(URLEncoder.encode(value, "utf-8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
response.addCookie(cookie);
}
示例5: set
import javax.servlet.http.HttpServletResponse; //導入方法依賴的package包/類
/**
* 保存
*
* @param response
* @param key
* @param value
* @param ifRemember
* true = age設置-1,不緩存;否則 age設置倆小時;
*/
public static void set(HttpServletResponse response, String key,
String value, boolean ifRemember) {
int age = Constant.COOKIE_MAX_AGE;
if (ifRemember) {
age = Constant.COOKIE_MAX_AGE;
} else {
age = -1;
}
Cookie cookie = new Cookie(key, value);
cookie.setMaxAge(age); // Cookie過期時間,單位/秒
cookie.setPath(Constant.COOKIE_PATH); // Cookie適用的路徑
response.addCookie(cookie);
}
示例6: checkforCookie
import javax.servlet.http.HttpServletResponse; //導入方法依賴的package包/類
/**
* Checks if specific cookies exist and save their value as message.
*
* @param request
* @param response
*/
protected void checkforCookie(HttpServletRequest request, HttpServletResponse response) {
if (request.getCookies() != null) {
for (Cookie cook : request.getCookies()) {
if (cook.getName().equals(MESSAGECOOKIE)) {
request.setAttribute("message", cook.getValue().replaceAll("_", " "));
cook.setMaxAge(0);
response.addCookie(cook);
} else if (cook.getName().equals(PRODUCTCOOKIE)) {
request.setAttribute("numberProducts", cook.getValue());
} else if (cook.getName().equals(ERRORMESSAGECOOKIE)) {
request.setAttribute("errormessage", cook.getValue().replaceAll("_", " "));
cook.setMaxAge(0);
response.addCookie(cook);
}
}
}
}
示例7: addCookie
import javax.servlet.http.HttpServletResponse; //導入方法依賴的package包/類
/**
* 添加cookie
*
* @param response
* @param name
* Cookie的名稱,不能為null
* @param value
* Cookie的值,默認值空字符串
* @param maxAge
* @param path
* 默認值'/'
*/
public static void addCookie(HttpServletResponse response, String name,
String value, Integer maxAge, String path) {
if (value == null) {
value = "";
}
if (path == null) {
path = "/";
}
Cookie cookie = new Cookie(name, value);
cookie.setPath(path);
if (maxAge != null) {
cookie.setMaxAge(maxAge);
}
response.addCookie(cookie);
}
示例8: saveToken
import javax.servlet.http.HttpServletResponse; //導入方法依賴的package包/類
@Override
public void saveToken(CsrfTokenBean token, HttpServletRequest request,
HttpServletResponse response) {
String tokenValue = token == null ? "" : token.getToken();
Cookie cookie = new Cookie(this.cookieName, tokenValue);
cookie.setSecure(request.isSecure());
if (this.cookiePath != null && !this.cookiePath.isEmpty()) {
cookie.setPath(this.cookiePath);
} else {
cookie.setPath(this.getRequestContext(request));
}
if (token == null) {
cookie.setMaxAge(0);
}
else {
cookie.setMaxAge(-1);
}
if (cookieHttpOnly && setHttpOnlyMethod != null) {
ReflectionUtils.invokeMethod(setHttpOnlyMethod, cookie, Boolean.TRUE);
}
response.addCookie(cookie);
}
示例9: clearCookie
import javax.servlet.http.HttpServletResponse; //導入方法依賴的package包/類
/**
* 清空Cookie操作 clearCookie
*
* @param request
* @param response
* @return boolean
* @author JIANG FEI Jun 19, 2014 10:12:17 AM
*/
public static boolean clearCookie(HttpServletRequest request,
HttpServletResponse response, String name, String domain) {
boolean bool = false;
Cookie[] cookies = request.getCookies();
if(null == cookies || cookies.length == 0) return bool;
try {
for (int i = 0; i < cookies.length; i++) {
Cookie cookie = new Cookie(name, null);
cookie.setMaxAge(0);
cookie.setPath("/");// 根據你創建cookie的路徑進行填寫
cookie.setDomain(domain);
response.addCookie(cookie);
bool = true;
}
} catch (Exception ex) {
logger.error("清空Cookies發生異常!", ex);
}
return bool;
}
示例10: addCookie
import javax.servlet.http.HttpServletResponse; //導入方法依賴的package包/類
protected void addCookie(HttpServletResponse response, String key, String value, int expiry) {
if (StringUtils.isBlank(key) || null == response) {
return;
}
Cookie cookie = new Cookie(key, value);
cookie.setPath("/");
cookie.setMaxAge(expiry);
response.addCookie(cookie);
}
示例11: index
import javax.servlet.http.HttpServletResponse; //導入方法依賴的package包/類
@RequestMapping("/")
public String index( Model model, HttpServletResponse response) {
Cookie cookie = new Cookie("jwtk", "011001110202423424214234");
cookie.setHttpOnly(true);
response.addCookie(cookie);
List<JwtkMessage> msg = jwtkMessageRepository.findAllByOrderByDateDesc();
model.addAttribute("msg", msg);
return INDEX_PAGE;
}
示例12: setCookie
import javax.servlet.http.HttpServletResponse; //導入方法依賴的package包/類
/**
* 設置 Cookie
* @param name 名稱
* @param value 值
* @param maxAge 生存時間(單位秒)
* @param uri 路徑
*/
public static void setCookie(HttpServletResponse response, String name, String value, String path, int maxAge) {
Cookie cookie = new Cookie(name, null);
cookie.setPath(path);
cookie.setMaxAge(maxAge);
try {
cookie.setValue(URLEncoder.encode(value, "utf-8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
response.addCookie(cookie);
}
示例13: userLogin
import javax.servlet.http.HttpServletResponse; //導入方法依賴的package包/類
private void userLogin(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Model model, String username, String headImgUrl) {
httpServletResponse.setCharacterEncoding("utf-8");
Cookie usercookie = new Cookie("uid", username);
Cookie userheadimg = new Cookie("headimg", headImgUrl);
usercookie.setPath("/");
userheadimg.setPath("/");
httpServletResponse.addCookie(usercookie);
httpServletResponse.addCookie(userheadimg);
HttpSession httpSession = httpServletRequest.getSession();
httpSession.setAttribute("uid", username);
httpSession.setAttribute("headimg", headImgUrl);
model.addAttribute("uid", username);
model.addAttribute("headimg", headImgUrl);
}
示例14: addCookie
import javax.servlet.http.HttpServletResponse; //導入方法依賴的package包/類
public void addCookie(final HttpServletRequest request, final HttpServletResponse response, final String cookieValue) {
if (!StringUtils.hasText(request.getParameter(RememberMeCredential.REQUEST_PARAMETER_REMEMBER_ME))) {
super.addCookie(response, cookieValue);
} else {
final Cookie cookie = createCookie(cookieValue);
cookie.setMaxAge(this.rememberMeMaxAge);
if (isCookieSecure()) {
cookie.setSecure(true);
}
response.addCookie(cookie);
}
}
示例15: addSessionCookie
import javax.servlet.http.HttpServletResponse; //導入方法依賴的package包/類
private static void addSessionCookie(String token, HttpServletResponse response) {
Cookie session = new Cookie("sid", token);
if (domain != null) {
session.setDomain(domain);
}
session.setPath("/");
session.setHttpOnly(true);
response.addCookie(session);
}