Java Code Examples for java.net.HttpCookie#setPath()

The following examples show how to use java.net.HttpCookie#setPath() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CookieManager.java    From Kalle with Apache License 2.0 6 votes vote down vote up
/**
 * Cookie for the specified URI to save, where path and port will be verified.
 *
 * @param uri        uri.
 * @param cookieList all you want to save the Cookie, does not meet the rules will not be saved.
 */
public void add(URI uri, List<String> cookieList) {
    for (String cookieValue : cookieList) {
        List<HttpCookie> cookies = HttpCookie.parse(cookieValue);
        for (HttpCookie cookie : cookies) {
            if (cookie.getPath() == null) {
                String path = normalizePath(uri.getPath());
                cookie.setPath(path);
            } else if (!pathMatches(uri, cookie)) {
                continue;
            }

            if (cookie.getDomain() == null) cookie.setDomain(uri.getHost());

            String portList = cookie.getPortlist();
            int port = getPort(uri);
            if (TextUtils.isEmpty(portList) || containsPort(portList, port)) {
                cookieJar.add(uri, cookie);
            }
        }
    }
}
 
Example 2
Source File: CookieUtils.java    From leetcode-editor with Apache License 2.0 6 votes vote down vote up
public static List<HttpCookie> toHttpCookie(String json) {

        List<HttpCookie> cookieList = new ArrayList<>();

        JSONArray jsonArray = JSONArray.parseArray(json);
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            HttpCookie clientCookie = new HttpCookie(jsonObject.getString("name"), jsonObject.getString("value"));
            clientCookie.setDomain(jsonObject.getString("domain"));
            clientCookie.setPath(jsonObject.getString("path"));
            clientCookie.setMaxAge(7 * 24 * 60);
            cookieList.add(clientCookie);
        }

        return cookieList;
    }
 
Example 3
Source File: CookieEntity.java    From NoHttp with Apache License 2.0 6 votes vote down vote up
/**
 * Into {@link HttpCookie}.
 *
 * @return {@link HttpCookie}.
 */
public HttpCookie toHttpCookie() {
    HttpCookie cookie = new HttpCookie(name, value);
    cookie.setComment(comment);
    cookie.setCommentURL(commentURL);
    cookie.setDiscard(discard);
    cookie.setDomain(domain);
    if (expiry == -1L)
        cookie.setMaxAge(-1L);
    else
        cookie.setMaxAge((expiry - System.currentTimeMillis()) / 1000L);
    cookie.setPath(path);
    cookie.setPortlist(portList);
    cookie.setSecure(secure);
    cookie.setVersion(version);
    return cookie;
}
 
Example 4
Source File: HttpResultCoder.java    From redkale with Apache License 2.0 6 votes vote down vote up
public static List<HttpCookie> getCookieList(ByteBuffer buffer) {
    int len = buffer.getChar();
    if (len == 0) return null;
    final List<HttpCookie> list = new ArrayList<>(len);
    for (int i = 0; i < len; i++) {
        HttpCookie cookie = new HttpCookie(getShortString(buffer), getShortString(buffer));
        cookie.setDomain(getShortString(buffer));
        cookie.setPath(getShortString(buffer));
        cookie.setPortlist(getShortString(buffer));
        cookie.setMaxAge(buffer.getLong());
        cookie.setSecure(buffer.get() == 1);
        cookie.setHttpOnly(buffer.get() == 1);
        list.add(cookie);
    }
    return list;
}
 
Example 5
Source File: SerializableCookie.java    From httplite with Apache License 2.0 6 votes vote down vote up
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
    String name = (String) in.readObject();
    String value = (String) in.readObject();
    clientCookie = new HttpCookie(name, value);

    clientCookie.setComment((String) in.readObject());
    clientCookie.setCommentURL((String) in.readObject());
    clientCookie.setDomain((String) in.readObject());
    clientCookie.setPath((String) in.readObject());

    clientCookie.setMaxAge(in.readLong());
    clientCookie.setVersion(in.readInt());

    clientCookie.setSecure(in.readBoolean());
    clientCookie.setDiscard(in.readBoolean());
}
 
Example 6
Source File: SerializableHttpCookie.java    From DaVinci with Apache License 2.0 6 votes vote down vote up
private void readObject(ObjectInputStream in) throws IOException,
        ClassNotFoundException {
    String name = (String) in.readObject();
    String value = (String) in.readObject();
    cookie = new HttpCookie(name, value);
    cookie.setComment((String) in.readObject());
    cookie.setCommentURL((String) in.readObject());
    cookie.setDomain((String) in.readObject());
    cookie.setMaxAge(in.readLong());
    cookie.setPath((String) in.readObject());
    cookie.setPortlist((String) in.readObject());
    cookie.setVersion(in.readInt());
    cookie.setSecure(in.readBoolean());
    cookie.setDiscard(in.readBoolean());
    setHttpOnly(in.readBoolean());
}
 
Example 7
Source File: TransportableHttpCookie.java    From Nimingban with Apache License 2.0 6 votes vote down vote up
@Nullable
public HttpCookie to() {
    if (name == null || value == null) {
        return null;
    }

    HttpCookie cookie = new HttpCookie(name, value);
    cookie.setComment(comment);
    cookie.setCommentURL(commentURL);
    cookie.setDiscard(discard);
    cookie.setDomain(domain);
    cookie.setMaxAge(maxAge);
    cookie.setPath(path);
    cookie.setPortlist(portList);
    cookie.setSecure(secure);
    cookie.setVersion(version);

    return cookie;
}
 
Example 8
Source File: SerializableHttpCookie.java    From 4pdaClient-plus with Apache License 2.0 6 votes vote down vote up
private void readObject(ObjectInputStream in) throws IOException,
        ClassNotFoundException {
    String name = (String) in.readObject();
    String value = (String) in.readObject();
    cookie = new HttpCookie(name, value);
    cookie.setComment((String) in.readObject());
    cookie.setCommentURL((String) in.readObject());
    cookie.setDomain((String) in.readObject());
    cookie.setMaxAge(in.readLong());
    cookie.setPath((String) in.readObject());
    cookie.setPortlist((String) in.readObject());
    cookie.setVersion(in.readInt());
    cookie.setSecure(in.readBoolean());
    cookie.setDiscard(in.readBoolean());
    setHttpOnly(in.readBoolean());
}
 
Example 9
Source File: UserServlet.java    From redkale-demo with Apache License 2.0 5 votes vote down vote up
@HttpMapping(url = "/user/logout", auth = false)
public void logout(HttpRequest req, HttpResponse resp) throws IOException {
    String sessionid = req.getSessionid(false);
    if (sessionid != null) service.logout(sessionid);
    HttpCookie cookie = new HttpCookie(COOKIE_AUTOLOGIN, "");
    cookie.setPath("/");
    cookie.setMaxAge(1);
    resp.addCookie(cookie);
    resp.finish("{\"success\":true}");
}
 
Example 10
Source File: JsonCookieTest.java    From keywhiz with Apache License 2.0 5 votes vote down vote up
@Test public void cookiesAlwaysVersion1() throws Exception {
  HttpCookie cookieVersionZero = new HttpCookie("session", "session-contents");
  cookieVersionZero.setPath("/");
  cookieVersionZero.setDomain("localhost");
  cookieVersionZero.setVersion(0);

  HttpCookie convertedCookie = JsonCookie.toHttpCookie(
      JsonCookie.fromHttpCookie(cookieVersionZero));
  assertThat(convertedCookie.getVersion()).isEqualTo(1);
}
 
Example 11
Source File: JsonCookie.java    From keywhiz with Apache License 2.0 5 votes vote down vote up
public static HttpCookie toHttpCookie(JsonCookie cookieContents) {
  HttpCookie cookie = new HttpCookie(cookieContents.name(), cookieContents.value());
  cookie.setDomain(cookieContents.domain());
  cookie.setPath(cookieContents.path());
  cookie.setSecure(cookieContents.isSecure());
  cookie.setHttpOnly(cookieContents.isHttpOnly());
  cookie.setVersion(1); // Always set version to 1 or important fields will be dropped
  return cookie;
}
 
Example 12
Source File: SimpleCookieStore.java    From Nimingban with Apache License 2.0 5 votes vote down vote up
public void fixLostCookiePath() {
    for (URL url : map.keySet()) {
        List<HttpCookieWithId> list = map.get(url);
        for (HttpCookieWithId hcwi : list) {
            HttpCookie cookie = hcwi.httpCookie;
            if (TextUtils.isEmpty(cookie.getPath())) {
                cookie.setPath("/");
                HttpCookieDB.updateCookie(hcwi, url);
            }
        }
    }
}
 
Example 13
Source File: UserServlet.java    From redkale-demo with Apache License 2.0 5 votes vote down vote up
/**
 * 修改密码
 *
 * @param req
 * @param resp
 *
 * @throws IOException
 */
@HttpMapping(url = "/user/updatepwd")
public void updatepwd(HttpRequest req, HttpResponse resp) throws IOException {
    UserPwdBean bean = req.getJsonParameter(UserPwdBean.class, "bean");
    UserInfo curr = req.currentUser();
    if (curr != null) bean.setSessionid(req.getSessionid(false));
    RetResult<UserInfo> result = service.updatePwd(bean);
    if (result.isSuccess() && curr == null) { //找回的密码
        curr = result.getResult();
        LoginBean loginbean = new LoginBean();
        loginbean.setAccount(curr.getEmail().isEmpty() ? curr.getMobile() : curr.getEmail());
        loginbean.setPassword(UserService.secondPasswordMD5(bean.getNewpwd()));
        loginbean.setSessionid(req.changeSessionid());
        loginbean.setLoginagent(req.getHeader("User-Agent"));
        loginbean.setLoginip(req.getRemoteAddr());
        result = service.login(loginbean);
    }
    String autologin = req.getCookie(COOKIE_AUTOLOGIN);
    if (result.isSuccess() && autologin != null) {
        autologin = UserService.decryptAES(autologin);
        if (autologin.contains("$0")) { //表示COOKIE_AUTOLOGIN 为密码类型存储
            String newpwd = UserService.secondPasswordMD5(bean.getNewpwd());
            int wen = autologin.indexOf('?');
            int mei = autologin.indexOf('$');
            String key = autologin.substring(0, mei + 2) + newpwd + autologin.substring(wen);
            HttpCookie cookie = new HttpCookie(COOKIE_AUTOLOGIN, UserService.encryptAES(key));
            cookie.setHttpOnly(true);
            cookie.setPath("/");
            String time = autologin.substring(wen + 1);
            int fen = time.indexOf('-');
            int age = Integer.parseInt(time.substring(0, fen)); //秒数
            long point = Long.parseLong(time.substring(fen + 1)); //毫秒数
            cookie.setMaxAge(age - (System.currentTimeMillis() - point) / 1000);
            resp.addCookie(cookie);
        }
    }
    resp.finishJson(result);
}
 
Example 14
Source File: CookieDBJar.java    From Nimingban with Apache License 2.0 5 votes vote down vote up
private HttpCookie cookie2HttpCookie(Cookie cookie) {
    HttpCookie httpCookie = new HttpCookie(cookie.name(), cookie.value());
    if (cookie.expiresAt() < System.currentTimeMillis()) {
        httpCookie.setMaxAge(-100L);
    } else {
        httpCookie.setMaxAge((cookie.expiresAt() - System.currentTimeMillis()) / 1000);
    }
    httpCookie.setDomain(cookie.domain());
    httpCookie.setPath(cookie.path());
    httpCookie.setSecure(cookie.secure());
    return httpCookie;
}
 
Example 15
Source File: SerializableHttpCookie.java    From AndroidStudyDemo with GNU General Public License v2.0 5 votes vote down vote up
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
    String name = (String) in.readObject();
    String value = (String) in.readObject();
    mClientCookie = new HttpCookie(name, value);
    mClientCookie.setComment((String) in.readObject());
    mClientCookie.setCommentURL((String) in.readObject());
    mClientCookie.setDomain((String) in.readObject());
    mClientCookie.setMaxAge(in.readLong());
    mClientCookie.setPath((String) in.readObject());
    mClientCookie.setPortlist((String) in.readObject());
    mClientCookie.setVersion(in.readInt());
    mClientCookie.setSecure(in.readBoolean());
    mClientCookie.setDiscard(in.readBoolean());
}
 
Example 16
Source File: SerializableHttpCookie.java    From ratebeer with GNU General Public License v3.0 5 votes vote down vote up
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
	String name = (String) in.readObject();
	String value = (String) in.readObject();
	cookie = new HttpCookie(name, value);
	cookie.setComment((String) in.readObject());
	cookie.setCommentURL((String) in.readObject());
	cookie.setDomain((String) in.readObject());
	cookie.setMaxAge(in.readLong());
	cookie.setPath((String) in.readObject());
	cookie.setPortlist((String) in.readObject());
	cookie.setVersion(in.readInt());
	cookie.setSecure(in.readBoolean());
	cookie.setDiscard(in.readBoolean());
	setHttpOnly(in.readBoolean());
}
 
Example 17
Source File: SerializableHttpCookie.java    From mattermost-android-classic with Apache License 2.0 5 votes vote down vote up
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
    String name = (String) in.readObject();
    String value = (String) in.readObject();
    clientCookie = new HttpCookie(name, value);
    clientCookie.setComment((String) in.readObject());
    clientCookie.setCommentURL((String) in.readObject());
    clientCookie.setDomain((String) in.readObject());
    clientCookie.setMaxAge(in.readLong());
    clientCookie.setPath((String) in.readObject());
    clientCookie.setPortlist((String) in.readObject());
    clientCookie.setVersion(in.readInt());
    clientCookie.setSecure(in.readBoolean());
    clientCookie.setDiscard(in.readBoolean());
}
 
Example 18
Source File: SerializableHttpCookie.java    From NewsMe with Apache License 2.0 5 votes vote down vote up
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException
{
    String name = (String) in.readObject();
    String value = (String) in.readObject();
    clientCookie = new HttpCookie(name, value);
    clientCookie.setComment((String) in.readObject());
    clientCookie.setCommentURL((String) in.readObject());
    clientCookie.setDomain((String) in.readObject());
    clientCookie.setMaxAge(in.readLong());
    clientCookie.setPath((String) in.readObject());
    clientCookie.setPortlist((String) in.readObject());
    clientCookie.setVersion(in.readInt());
    clientCookie.setSecure(in.readBoolean());
    clientCookie.setDiscard(in.readBoolean());
}
 
Example 19
Source File: UserServlet.java    From redkale-demo with Apache License 2.0 5 votes vote down vote up
@HttpMapping(url = "/user/qqlogin", auth = false)
public void qqlogin(HttpRequest req, HttpResponse resp) throws IOException {
    String access_token = req.getParameter("access_token");
    String openid = req.getParameter("openid");
    if (finest) logger.finest("/user/qqlogin :  " + openid + "," + access_token);
    LoginQQBean bean = new LoginQQBean();
    bean.setAccesstoken(access_token);
    bean.setApptoken(req.getParameter("apptoken", ""));
    bean.setOpenid(openid);
    bean.setLoginaddr(req.getRemoteAddr());
    bean.setLoginagent(req.getHeader("User-Agent"));
    bean.setSessionid(req.changeSessionid());
    RetResult<UserInfo> rr = service.qqlogin(bean);
    if (rr.isSuccess()) {
        UserInfo info = rr.getResult();
        int age = 1000 * 24 * 60 * 60;
        String key = info.getUser36id() + "$2" + info.getQqopenid() + "?" + age + "-" + System.currentTimeMillis();
        HttpCookie cookie = new HttpCookie(COOKIE_AUTOLOGIN, UserService.encryptAES(key));
        cookie.setHttpOnly(true);
        cookie.setPath("/");
        cookie.setMaxAge(age);
        resp.addCookie(cookie);
    }
    if (access_token == null || access_token.isEmpty()) {
        resp.setHeader("Location", req.getParameter("url", "/"));
        resp.finish(302, null);
    } else { //APP 模式
        resp.finishJson(rr);
    }
}
 
Example 20
Source File: UserServlet.java    From redkale-demo with Apache License 2.0 4 votes vote down vote up
/**
 * 微信登陆 https://open.weixin.qq.com/connect/qrconnect?appid=wx微信ID&redirect_uri=xxxxx&response_type=code&scope=snsapi_login&state=wx微信ID_1#wechat_redirect
 * 接收两种形式:
 * WEB端微信登录: /user/wxlogin?code=XXXXXX&state=wx微信ID_1&apptoken=XXX
 * APP端微信登录: /user/wxlogin?openid=XXXX&state=1&access_token=XXX&apptoken=XXX
 * <p>
 * @param req
 * @param resp
 *
 * @throws IOException
 */
@HttpMapping(url = "/user/wxlogin", auth = false)
public void wxlogin(HttpRequest req, HttpResponse resp) throws IOException {
    String code = req.getParameter("code");
    String state = req.getParameter("state");  //state值格式: appid_autoregflag

    String access_token = req.getParameter("access_token");
    String openid = req.getParameter("openid");

    if (finest) logger.finest("/user/wxlogin :  code = " + code + ", access_token = " + access_token + ", openid = " + openid + ", state =" + state);
    int pos = state.indexOf('_');
    String appid = pos > 0 ? state.substring(0, pos) : state;
    if (appid.length() < 2) appid = "";
    boolean autoreg = (pos > 0 || "1".equals(state)) ? (state.charAt(pos + 1) == '1') : true;
    final boolean wxbrowser = req.getHeader("User-Agent", "").contains("MicroMessenger");
    LoginWXBean bean = new LoginWXBean();
    { //WEB方式
        bean.setAppid(appid);
        bean.setCode(code);
    }
    { //APP方式
        bean.setAccesstoken(access_token);
        bean.setOpenid(openid);
    }
    bean.setAutoreg(autoreg);
    bean.setApptoken(req.getParameter("apptoken", ""));
    bean.setLoginaddr(req.getRemoteAddr());
    bean.setLoginagent(req.getHeader("User-Agent"));
    if (autoreg) bean.setSessionid(req.changeSessionid());
    RetResult<UserInfo> rr = service.wxlogin(bean);
    if (autoreg && rr.isSuccess() && (wxbrowser || (access_token != null && !access_token.isEmpty()))) {
        UserInfo info = rr.getResult();
        int age = 1000 * 24 * 60 * 60;
        String key = (bean.emptyApptoken() ? "" : (bean.getApptoken() + "#")) + info.getUser36id() + "$1" + info.getWxunionid() + "?" + age + "-" + System.currentTimeMillis();
        HttpCookie cookie = new HttpCookie(COOKIE_AUTOLOGIN, UserService.encryptAES(key));
        cookie.setHttpOnly(true);
        cookie.setPath("/");
        cookie.setMaxAge(age);
        resp.addCookie(cookie);
    }
    if (access_token == null || access_token.isEmpty()) { //WEB登录
        resp.setHeader("Location", req.getParameter("url", "/"));
        resp.finish(302, null);
    } else { //APP 模式
        resp.finishJson(rr);
    }
}