比如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");
    }

}

 

posted on 2019-01-18 16:11  不爱吃蘑菇的大蘑菇  阅读(1227)  评论(0编辑  收藏  举报