美文网首页
JAVA获取cookie

JAVA获取cookie

作者: _花 | 来源:发表于2022-05-19 17:12 被阅读0次

    1.utils类

    /*
     * 该类可以从浏览器请求中提取出cookies并进行对cookis的相关操作
     * 
     */
     
    public class CookiesUtil extends BaseController {
        
        /**
         * 根据名字获取cookie
         * 
         * @param request
         * @param name
         *            cookie名字
         * @return
         */
        public static Cookie getCookieByName(HttpServletRequest request, String name) {
            Map<String, Cookie> cookieMap = ReadCookieMap(request);
            if (cookieMap.containsKey(name)) {
                Cookie cookie = (Cookie) cookieMap.get(name);
                return cookie;
            } else {
                return null;
            }
        }
     
        /**
         * 将cookie封装到Map里面
         * 
         * @param request
         * @return
         */
        private static Map<String, Cookie> ReadCookieMap(HttpServletRequest request) {
            Map<String, Cookie> cookieMap = new HashMap<String, Cookie>();
            Cookie[] cookies = request.getCookies();
            if (null != cookies) {
                for (Cookie cookie : cookies) {
                    cookieMap.put(cookie.getName(), cookie);
                }
            }
            return cookieMap;
        }
     
        /**
         * 保存Cookies
         * 
         * @param response
         *            servlet请求
         * @param value
         *            保存值
         * @author jxf
         */
        public static HttpServletResponse setCookie(HttpServletResponse response, String name, String value,int time) {
            // new一个Cookie对象,键值对为参数
            Cookie cookie = new Cookie(name, value);
            // tomcat下多应用共享
            cookie.setPath("/");
            // 如果cookie的值中含有中文时,需要对cookie进行编码,不然会产生乱码
            try {
                URLEncoder.encode(value, "utf-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            cookie.setMaxAge(time);
            // 将Cookie添加到Response中,使之生效
            response.addCookie(cookie); // addCookie后,如果已经存在相同名字的cookie,则最新的覆盖旧的cookie
            return response;
        }
        /**
         *删掉 cookie
         * @param request
         * @return
         */
        public static HttpServletResponse delCookie(HttpServletRequest request, HttpServletResponse response){
            try {
                Cookie[] cookies = request.getCookies();
                for (Cookie cookie : cookies) {
                  // 这块时间不可以设置成负数,设置成负数,浏览器默认会存成session,关掉浏览器才会失效
                    setCookie(response, cookie.getName(), cookie.getValue(),0);
                    System.out.println(cookie.getName());
                }
            } catch (Exception e) {
                System.out.println( "从cookie获取到相应的值失败----"+e.getMessage());
            }
            return response;
        }
    }
    

    2.具体使用

     /**
         *从cookies 中获取对应的值
         * @param request
         * @return
         */
        public static Map<String,String> getToken(HttpServletRequest request){
            Map<String,String> map=new HashMap<>(  );
            String token = "";
            String accessToken = "";
            String expiresIn = "";
            try {
                Cookie[] cookies = request.getCookies();
                for (Cookie cookie : cookies) {
                    switch(cookie.getName()){
                        case "accessToken":
                            accessToken = cookie.getValue();
                            break;
                        case "expiresIn":
                            expiresIn = cookie.getValue();
                            break;
                        case "token":
                            token = cookie.getValue();
                            break;
                        default:
                            break;
                    }
                }
                map.put( "token",token );
                map.put( "accessToken",accessToken );
                map.put( "expiresIn",expiresIn );
            } catch (Exception e) {
                System.out.println( "从cookie获取到相应的值失败----"+e.getMessage());
            }
            return map;
        }
    

    转自:记录 Java 从浏览器中获取cookies

    相关文章

      网友评论

          本文标题:JAVA获取cookie

          本文链接:https://www.haomeiwen.com/subject/qaqaurtx.html