下面列出了 io.netty.handler.codec.http.cookie.Cookie # setPath ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@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);
}
}
}
private CommonResponse createSession(String username, Set<String> roles, boolean ldap)
throws Exception {
String sessionId = new BigInteger(130, secureRandom).toString(32);
ImmutableSession session = ImmutableSession.builder()
.caseAmbiguousUsername(username)
.ldap(ldap)
.roles(roles)
.lastRequest(clock.currentTimeMillis())
.build();
sessionMap.put(sessionId, session);
String layoutJson = layoutService
.getLayoutJson(session.createAuthentication(central, configRepository));
CommonResponse response = new CommonResponse(OK, MediaType.JSON_UTF_8, layoutJson);
Cookie cookie =
new DefaultCookie(configRepository.getWebConfig().sessionCookieName(), sessionId);
cookie.setHttpOnly(true);
cookie.setPath("/");
response.setHeader(HttpHeaderNames.SET_COOKIE, ServerCookieEncoder.STRICT.encode(cookie));
purgeExpiredSessions();
auditSuccessfulLogin(username);
return response;
}
public DefaultWebRes addCookie(String name, String value) {
String paramsStr = null;
int p = value.indexOf("^");
if (p >= 0) {
paramsStr = value.substring(p + 1);
value = value.substring(0, p);
}
if (cookies == null) cookies = new ArrayList<>();
Cookie c = new DefaultCookie(name, value);
if (paramsStr != null) {
Map<String, String> params = Plugin.defaultSplitParams(paramsStr);
if (params.containsKey("domain"))
c.setDomain(params.get("domain"));
if (params.containsKey("path"))
c.setPath(params.get("path"));
if (params.containsKey("maxAge"))
c.setMaxAge(Long.parseLong(params.get("maxAge")));
if (params.containsKey("httpOnly"))
c.setHttpOnly(Boolean.parseBoolean(params.get("httpOnly")));
else
c.setHttpOnly(true);
if (params.containsKey("secure"))
c.setSecure(Boolean.parseBoolean(params.get("secure")));
if (params.containsKey("wrap"))
c.setWrap(Boolean.parseBoolean(params.get("wrap")));
}
cookies.add(c);
return this;
}
@Override
public void addCookie(String name, String value, String domain, long maxAge) {
if (StrUtil.isNotBlank(name) && StrUtil.isNotBlank(value)) {
Cookie cookie = new DefaultCookie(name, value);
cookie.setPath("/");
if (domain != null && domain.trim().length() > 0) {
cookie.setDomain(domain);
}
if (maxAge > 0) {
cookie.setMaxAge(maxAge);
}
setCookie(cookie);
}
}
@Override
public boolean deleteCookie(String name) {
Cookie cookie = getCookie(name);
if (cookie != null) {
cookie.setMaxAge(0);
cookie.setPath("/");
setCookie(cookie);
return true;
}
return false;
}
@Override
public Response cookie(@NonNull String name, @NonNull String value, int maxAge) {
Cookie nettyCookie = new io.netty.handler.codec.http.cookie.DefaultCookie(name, value);
nettyCookie.setPath("/");
nettyCookie.setMaxAge(maxAge);
this.cookies.add(nettyCookie);
return this;
}
@Override
public Response cookie(@NonNull String name, @NonNull String value, int maxAge, boolean secured) {
Cookie nettyCookie = new io.netty.handler.codec.http.cookie.DefaultCookie(name, value);
nettyCookie.setPath("/");
nettyCookie.setMaxAge(maxAge);
nettyCookie.setSecure(secured);
this.cookies.add(nettyCookie);
return this;
}
@Override
public Response cookie(@NonNull String path, @NonNull String name, @NonNull String value, int maxAge, boolean secured) {
Cookie nettyCookie = new io.netty.handler.codec.http.cookie.DefaultCookie(name, value);
nettyCookie.setMaxAge(maxAge);
nettyCookie.setSecure(secured);
nettyCookie.setPath(path);
this.cookies.add(nettyCookie);
return this;
}
void deleteSessionCookie(CommonResponse response) throws Exception {
Cookie cookie = new DefaultCookie(configRepository.getWebConfig().sessionCookieName(), "");
cookie.setHttpOnly(true);
cookie.setMaxAge(0);
cookie.setPath("/");
response.setHeader(HttpHeaderNames.SET_COOKIE, ServerCookieEncoder.STRICT.encode(cookie));
}
@Test
public void testRequestIsPrepared(TestContext context) {
prepareServer(context, req -> {
req.response().headers().add("set-cookie", ServerCookieEncoder.STRICT.encode(new DefaultCookie("test", "toast")));
});
Consumer<HttpRequest<Buffer>> check = r -> {
Async async = context.async();
Cookie c = new DefaultCookie("test", "localhost");
c.setPath("/");
client.cookieStore().remove(c);
r.send(ar -> {
async.complete();
validate(context, client.cookieStore().get(false, "localhost", "/"),
new String[] { "test" }, new String[] { "toast" });
});
async.await();
};
check.accept(client.delete("/"));
check.accept(client.delete("localhost", "/"));
check.accept(client.delete(PORT, "localhost", "/"));
check.accept(client.deleteAbs("http://localhost/"));
check.accept(client.get("/"));
check.accept(client.get("localhost", "/"));
check.accept(client.get(PORT, "localhost", "/"));
check.accept(client.getAbs("http://localhost/"));
check.accept(client.head("/"));
check.accept(client.head("localhost", "/"));
check.accept(client.head(PORT, "localhost", "/"));
check.accept(client.headAbs("http://localhost/"));
check.accept(client.patch("/"));
check.accept(client.patch("localhost", "/"));
check.accept(client.patch(PORT, "localhost", "/"));
check.accept(client.patchAbs("http://localhost/"));
check.accept(client.post("/"));
check.accept(client.post("localhost", "/"));
check.accept(client.post(PORT, "localhost", "/"));
check.accept(client.postAbs("http://localhost/"));
check.accept(client.put("/"));
check.accept(client.put("localhost", "/"));
check.accept(client.put(PORT, "localhost", "/"));
check.accept(client.putAbs("http://localhost/"));
check.accept(client.request(HttpMethod.GET, new RequestOptions()));
check.accept(client.request(HttpMethod.GET, "/"));
check.accept(client.request(HttpMethod.GET, "localhost", "/"));
check.accept(client.request(HttpMethod.GET, PORT, "localhost", "/"));
check.accept(client.requestAbs(HttpMethod.GET, "http://localhost/"));
}