@Override public void visit(final Cookie cookie) { cookie.setDomain(this.domain); } }
@Override public Cookie domain(String domain) { nettyCookie.setDomain(domain); return this; }
CookieBasedSsoHandler(String cookieName, String cookieValue) { requireNonNull(cookieName, "cookieName"); requireNonNull(cookieValue, "cookieValue"); final Cookie cookie = new DefaultCookie(cookieName, cookieValue); cookie.setDomain(spHostname); cookie.setPath("/"); cookie.setHttpOnly(true); setCookie = ServerCookieEncoder.STRICT.encode(cookie); }
/** * Invoked when the SAML authentication process is finished and a user is authenticated. You can get * information about the authenticated user from the {@link Response}, especially his or her login name. * In this example, an email address is used as a login name. The login name is transferred to a web * browser via {@code Set-Cookie} header. */ @Override public HttpResponse loginSucceeded(ServiceRequestContext ctx, AggregatedHttpMessage req, MessageContext<Response> message, @Nullable String sessionIndex, @Nullable String relayState) { final String username = getNameId(message.getMessage(), SamlNameIdFormat.EMAIL).map(NameIDType::getValue) .orElse(null); if (username == null) { return HttpResponse.of(HttpStatus.UNAUTHORIZED, MediaType.HTML_UTF_8, "<html><body>Username is not found.</body></html>"); } logger.info("{} user '{}' has been logged in.", ctx, username); final Cookie cookie = new DefaultCookie("username", username); cookie.setHttpOnly(true); cookie.setDomain("localhost"); cookie.setMaxAge(60); cookie.setPath("/"); return HttpResponse.of( HttpHeaders.of(HttpStatus.OK) .contentType(MediaType.HTML_UTF_8) .add(HttpHeaderNames.SET_COOKIE, ServerCookieEncoder.LAX.encode(cookie)), HttpData.ofUtf8("<html><body onLoad=\"window.location.href='/welcome'\"></body></html>")); }
@Override public Response cookie(@NonNull com.blade.mvc.http.Cookie cookie) { Cookie nettyCookie = new io.netty.handler.codec.http.cookie.DefaultCookie(cookie.name(), cookie.value()); if (cookie.domain() != null) { nettyCookie.setDomain(cookie.domain()); } if (cookie.maxAge() > 0) { nettyCookie.setMaxAge(cookie.maxAge()); } nettyCookie.setPath(cookie.path()); nettyCookie.setHttpOnly(cookie.httpOnly()); nettyCookie.setSecure(cookie.secure()); this.cookies.add(nettyCookie); return this; }
@Override public Response cookie(@NonNull com.blade.mvc.http.Cookie cookie) { Cookie nettyCookie = new io.netty.handler.codec.http.cookie.DefaultCookie(cookie.name(), cookie.value()); if (cookie.domain() != null) { nettyCookie.setDomain(cookie.domain()); } if (cookie.maxAge() > 0) { nettyCookie.setMaxAge(cookie.maxAge()); } nettyCookie.setPath(cookie.path()); nettyCookie.setHttpOnly(cookie.httpOnly()); nettyCookie.setSecure(cookie.secure()); this.cookies.add(nettyCookie); return this; }
@Test public void testAddOrReplaceCookies() { RequestBuilder requestBuilder = new RequestBuilder(); Cookie cookie = new DefaultCookie("name", "value"); cookie.setDomain("google.com"); cookie.setPath("/"); cookie.setMaxAge(1000); cookie.setSecure(true); cookie.setHttpOnly(true); requestBuilder.addOrReplaceCookie(cookie); assertEquals(requestBuilder.cookies.size(), 1, "cookies size should be 1 after adding one cookie"); assertEquals(requestBuilder.cookies.get(0), cookie, "cookie does not match"); Cookie cookie2 = new DefaultCookie("name", "value"); cookie2.setDomain("google2.com"); cookie2.setPath("/path"); cookie2.setMaxAge(1001); cookie2.setSecure(false); cookie2.setHttpOnly(false); requestBuilder.addOrReplaceCookie(cookie2); assertEquals(requestBuilder.cookies.size(), 1, "cookies size should remain 1 as we just replaced a cookie with same name"); assertEquals(requestBuilder.cookies.get(0), cookie2, "cookie does not match"); Cookie cookie3 = new DefaultCookie("name2", "value"); cookie3.setDomain("google.com"); cookie3.setPath("/"); cookie3.setMaxAge(1000); cookie3.setSecure(true); cookie3.setHttpOnly(true); requestBuilder.addOrReplaceCookie(cookie3); assertEquals(requestBuilder.cookies.size(), 2, "cookie size must be 2 after adding 1 more cookie i.e. cookie3"); }
@Override public Cookie setDomain(final String domain) { nettyCookie.setDomain(domain); this.changed = true; return this; }
@Override protected void applyCookies() { for (String name : getCookies().keySet()) { for (ResponseCookie httpCookie : getCookies().get(name)) { Cookie cookie = new DefaultCookie(name, httpCookie.getValue()); if (!httpCookie.getMaxAge().isNegative()) { cookie.setMaxAge(httpCookie.getMaxAge().getSeconds()); } if (httpCookie.getDomain() != null) { cookie.setDomain(httpCookie.getDomain()); } if (httpCookie.getPath() != null) { cookie.setPath(httpCookie.getPath()); } cookie.setSecure(httpCookie.isSecure()); cookie.setHttpOnly(httpCookie.isHttpOnly()); this.response.addCookie(cookie); } } }
@Override protected void applyCookies() { for (String name : getCookies().keySet()) { for (ResponseCookie httpCookie : getCookies().get(name)) { Cookie cookie = new DefaultCookie(name, httpCookie.getValue()); if (!httpCookie.getMaxAge().isNegative()) { cookie.setMaxAge(httpCookie.getMaxAge().getSeconds()); } if (httpCookie.getDomain() != null) { cookie.setDomain(httpCookie.getDomain()); } if (httpCookie.getPath() != null) { cookie.setPath(httpCookie.getPath()); } cookie.setSecure(httpCookie.isSecure()); cookie.setHttpOnly(httpCookie.isHttpOnly()); this.response.addCookie(cookie); } } }
@Test public void getWithCookies() throws Throwable { withClient().run(client -> withServer(server).run(server -> { final Cookie coo = new DefaultCookie("foo", "value"); coo.setDomain("/"); coo.setPath("/"); server.enqueueEcho(); client.prepareGet(getTargetUrl()) .addCookie(coo) .execute(new AsyncCompletionHandlerAdapter() { @Override public Response onCompleted(Response response) { assertEquals(response.getStatusCode(), 200); List<Cookie> cookies = response.getCookies(); assertEquals(cookies.size(), 1); assertEquals(cookies.get(0).toString(), "foo=value"); return response; } }).get(TIMEOUT, SECONDS); })); }
@Override public Cookie setDomain(final String domain) { nettyCookie.setDomain(domain); this.changed = true; return this; }
@Override public void setDomain(String domain) { cookie.setDomain(domain); }
@Override public void setDomain(final String domain) { nettyCookie.setDomain(domain); }
/** * 设定返回给客户端的Cookie * * @param name cookie名 * @param value cookie值 * @param maxAgeInSeconds -1: 关闭浏览器清除Cookie. 0: 立即清除Cookie. n>0 : Cookie存在的秒数. * @param path Cookie的有效路径 * @param domain the Cookie可见的域,依据 RFC 2109 标准 * @return 自己 */ public Response addCookie(String name, String value, int maxAgeInSeconds, String path, String domain) { Cookie cookie = new DefaultCookie(name, value); if (domain != null) { cookie.setDomain(domain); } cookie.setMaxAge(maxAgeInSeconds); cookie.setPath(path); return addCookie(cookie); }
@Override public Response cookie(@NonNull com.blade.mvc.http.Cookie cookie) { Cookie nettyCookie = new io.netty.handler.codec.http.cookie.DefaultCookie(cookie.name(), cookie.value()); if (cookie.domain() != null) { nettyCookie.setDomain(cookie.domain()); } if (cookie.maxAge() > 0) { nettyCookie.setMaxAge(cookie.maxAge()); } nettyCookie.setPath(cookie.path()); nettyCookie.setHttpOnly(cookie.httpOnly()); nettyCookie.setSecure(cookie.secure()); this.cookies.add(nettyCookie); return this; }
static io.netty.handler.codec.http.cookie.Cookie getNettyCookie(Cookie c) { io.netty.handler.codec.http.cookie.Cookie ret = new io.netty.handler.codec.http.cookie.DefaultCookie(c.getName(), c.getValue()); ret.setDomain(c.getDomain()); ret.setHttpOnly(c.isHttpOnly()); ret.setMaxAge(c.getMaxAge()); ret.setPath(c.getPath()); ret.setSecure(c.isSecure()); return ret; }
@Override protected void applyCookies() { for (String name : getCookies().keySet()) { for (ResponseCookie httpCookie : getCookies().get(name)) { Cookie cookie = new DefaultCookie(name, httpCookie.getValue()); if (!httpCookie.getMaxAge().isNegative()) { cookie.setMaxAge(httpCookie.getMaxAge().getSeconds()); } if (httpCookie.getDomain() != null) { cookie.setDomain(httpCookie.getDomain()); } if (httpCookie.getPath() != null) { cookie.setPath(httpCookie.getPath()); } cookie.setSecure(httpCookie.isSecure()); cookie.setHttpOnly(httpCookie.isHttpOnly()); this.response.addCookie(cookie); } } }
@Override protected void applyCookies() { for (String name : getCookies().keySet()) { for (ResponseCookie httpCookie : getCookies().get(name)) { Cookie cookie = new DefaultCookie(name, httpCookie.getValue()); if (!httpCookie.getMaxAge().isNegative()) { cookie.setMaxAge(httpCookie.getMaxAge().getSeconds()); } if (httpCookie.getDomain() != null) { cookie.setDomain(httpCookie.getDomain()); } if (httpCookie.getPath() != null) { cookie.setPath(httpCookie.getPath()); } cookie.setSecure(httpCookie.isSecure()); cookie.setHttpOnly(httpCookie.isHttpOnly()); this.response.addCookie(cookie); } } }