比如Shiro框架的 RememberMe Cookie
是不允许Js进行读写的
只能在服务器端通过同一个域的请求获得
import com.constantine.forum.exception.ForumException;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes; import javax.servlet.ServletRequest;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest; /**
* @Description:
* @Author: EM
* @CreateTime: 2019-01-19 11:23
*/
public class CookieUtil { public static final String getCookieValue(String cookieName) throws Exception {
ServletRequestAttributes attrs = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attrs.getRequest();
Cookie[] cookies = request.getCookies();
return findCookie(cookies, cookieName);
} public static final String getCookieValue(ServletRequest request, String cookieName) throws Exception {
Cookie[] cookies = null;
if (request instanceof HttpServletRequest) {
cookies = ((HttpServletRequest) request).getCookies();
}
return findCookie(cookies, cookieName);
} private static String findCookie(Cookie[] cookies, String cookieName) throws Exception {
if (cookies != null) {
for (Cookie cookie : cookies) {
if (cookie.getName().equals(cookieName)) {
return cookie.getValue();
}
}
}
throw new Exception("No cookie found");
} }