SpringBoot HttpServletResponse Header Cookie????????????
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringBoot HttpServletResponse Header Cookie????????????相关的知识,希望对你有一定的参考价值。
?????????lombok equals embed print imp ?????? oid init wired
?????????
?????????Response??????header???cookie?????????????????????????????????????????????
????????????
CookieUtils:
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.PostConstruct;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
* Created by qhong on 2018/10/15 15:46
**/
@Component
public class CookieUtils {
public static final int COOKIE_MAX_AGE = 7 * 24 * 3600;
public static final int COOKIE_HALF_HOUR = 30 * 60;
private static HttpServletResponse response;
@Autowired
private HttpServletResponse response2;
private static HttpServletRequest request;
@Autowired
private HttpServletRequest request2;
@PostConstruct
public void beforeInit() {
request=request2;
response=response2;
}
/**
* ??????Cookie????????????Cookie????????????????????????????????????Null
*
* @param request
* @param name
* @return
*/
public static Cookie getCookie(String name) {
Cookie[] cookies = request.getCookies();
if (cookies==null||cookies.length<1) {
return null;
}
Cookie cookie = null;
for (Cookie c : cookies) {
if (name.equals(c.getName())) {
cookie = c;
break;
}
}
return cookie;
}
/**
* ??????Cookie??????????????????Cookie???
*
* @param request
* @param name
* @return
*/
public static String getCookieValue(String name) {
Cookie cookie = getCookie(name);
if(cookie != null){
return cookie.getValue();
}
return null;
}
/**
* ??????cookie
* @param request
* @param response
* @param name ???????????????????????????
*/
public static void removeCookie(String name) {
if (null == name) {
return;
}
Cookie cookie = getCookie(name);
if(null != cookie){
cookie.setPath("/");
cookie.setValue("");
cookie.setMaxAge(0);
response.addCookie(cookie);
}
}
/**
* ??????????????????Cookie???????????????????????????(????????????)
*
* @param response
* @param name
* @param value
* @param maxValue
*/
public static void setCookie(String name,
String value, int maxValue) {
if (StringUtils.isBlank(name)) {
return;
}
if (null == value) {
value = "";
}
Cookie cookie = new Cookie(name, value);
cookie.setPath("/");
if (maxValue != 0) {
cookie.setMaxAge(maxValue);
} else {
cookie.setMaxAge(COOKIE_HALF_HOUR);
}
response.addCookie(cookie);
// try {
// response.flushBuffer();
// } catch (IOException e) {
// e.printStackTrace();
// }
}
/**
* ??????????????????Cookie?????????30??????????????????
*
* @param response
* @param name
* @param value
*/
public static void setCookie(String name,
String value) {
setCookie(name, value, COOKIE_HALF_HOUR);
}
/**
* ???cookie?????????Map??????
* @param request
* @return
*/
public static Map<String,Cookie> getCookieMap(){
Map<String,Cookie> cookieMap = new HashMap<>();
Cookie[] cookies = request.getCookies();
if(cookies!=null&&cookies.length>1){
for(Cookie cookie : cookies){
cookieMap.put(cookie.getName(), cookie);
}
}
return cookieMap;
}
}
SpringServletUtil:
import java.io.IOException;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
* Created by qhong on 2019/1/11 14:17
**/
@Component
@Slf4j
public class SpringHttpUtil {
// /**
// * ???????????????
// * @return
// */
// private HttpServletRequest getRequest(){
// return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
// }
//
// /**
// * ???????????????
// * @return
// */
// private HttpServletResponse getResponse(){
// return ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getResponse();
// }
@Autowired
private HttpServletResponse response;
@Autowired
private HttpServletRequest request;
/**
* ??????????????????????????????cookie????????????
*/
public String getParams(String name) {
String result = getHeader(name);
//cookie
if (StringUtils.isBlank(result)) {
result = CookieUtils.getCookieValue(name);
}
return result;
}
public String getHeader(String name) {
String result= request.getHeader(name);
if(StringUtils.isBlank(result)){
result=request.getParameter(name);
}
return result;
}
public void setHeader(Map<String, String> map) {
if (map == null || map.isEmpty() || map.size() == 0)
return;
map.entrySet().stream().forEach(x -> {
response.setHeader(x.getKey(), x.getValue());
});
}
public void setCookie(Map<String, String> map) {
if (map == null || map.isEmpty() || map.size() == 0)
return;
map.entrySet().stream().forEach(x -> {
CookieUtils.setCookie(x.getKey(), x.getValue());
});
}
}
?????????
Map<String, String> map = new HashMap<>();
map.put("aaaaa","aaa");
map.put("bbbbb","bbb");
springHttpUtil.setHeader(map);
springHttpUtil.setCookie(map);
???????????????????????? ???????????????cookie??????????????????
???????????????
orgapache omcatembed omcat-embed-core8.5.15 omcat-embed-core-8.5.15.jar
ResponseFacade:
@Override
public void addCookie(Cookie cookie) {
if (isCommitted()) {
return;
}
response.addCookie(cookie);
}
@Override
public void setHeader(String name, String value) {
if (isCommitted()) {
return;
}
response.setHeader(name, value);
}
@Override
public void addHeader(String name, String value) {
if (isCommitted()) {
return;
}
response.addHeader(name, value);
}
?????????????????????????????????????????????isCommitted???false????????????true,????????????????????????????????????
???????????????
response?????????????????????8KB???????????????????????????????????????????????????response???????????????????????????????????????????????????????????????????????????response???isCommited()?????????????????????true?????????response?????????????????????????????????
??????????????????????????????????????????response.flushBuffer()????????????????????????????????????????????????????????????????????????????????????????????????response???isCommited()?????????????????????true?????????response?????????????????????????????????
?????????????????????response.getWirter().flush()?????????????????????response.flushBuffer()????????????????????????????????????????????????
??????response???isCommited()????????????true???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????
???????????????
?????????????????????CookieUtils???????????????????????????????????????response?????????flushBuffer,??????isCommitted???true?????????????????????????????????
response.flushBuffer???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????
?????????
HttpServletResponse???????????????
以上是关于SpringBoot HttpServletResponse Header Cookie????????????的主要内容,如果未能解决你的问题,请参考以下文章