Httponly Cookie ist eine Cookie-Sicherheitslösung.
Wenn in Browsern, die nur http-Cookies unterstützen (IE6+, FF3.0+), das Attribut „httponly“ im Cookie festgelegt ist, kann das JavaScript-Skript die Cookie-Informationen nicht lesen, was jedoch möglich ist Verhindern Sie effektiv XSS-Angriffe und machen Sie Website-Anwendungen sicherer.
Aber J2EE4- und J2EE5-Cookies bieten keine Methode zum Festlegen des httponly-Attributs. Wenn Sie also das httponly-Attribut festlegen müssen, müssen Sie sich selbst darum kümmern.
import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletResponse; /** * Cookie Tools */ public class CookieUtil { /** * Set httponly cookie * @param Response HTTP response * @param Cookie cookie object * @param Ishttponly is httponly */ public static void addCookie(HttpServletResponse response, Cookie cookie, boolean isHttpOnly) { String name = cookie.getName();//Cookie name String value = cookie.getValue();//Cookie value int maxAge = cookie.getMaxAge();//Maximum survival time (milliseconds, 0 representative deletion, -1 represents the same as the browser session) String path = cookie.getPath();//path String domain = cookie.getDomain();//area boolean isSecure = cookie.getSecure();//Is there a security protocol? StringBuilder buffer = new StringBuilder(); buffer.append(name).append("=").append(value).append(";"); if (maxAge == 0) { buffer.append("Expires=Thu Jan 01 08:00:00 CST 1970;"); } else if (maxAge > 0) { buffer.append("Max-Age=").append(maxAge).append(";"); } if (domain != null) { buffer.append("domain=").append(domain).append(";"); } if (path != null) { buffer.append("path=").append(path).append(";"); } if (isSecure) { buffer.append("secure;"); } if (isHttpOnly) { buffer.append("HTTPOnly;"); } response.addHeader("Set-Cookie", buffer.toString()); } }
Es ist erwähnenswert, dass das Cookie in Java Ee 6.0 httponly gesetzt hat. Wenn es also mit einem Java EE 6.0-kompatiblen Container (wie Tomcat 7) kompatibel ist, können Sie cookie.sethttponly verwenden set HTTPONLY: # 🎜🎜#
cookie.setHttpOnly(true);Die Methode setHttpOnly(Boolean httpOnly) der Java-Klasse HttpCookie wird verwendet, um anzugeben, ob das Cookie als HTTPOnly betrachtet werden kann. Wenn es auf „true“ gesetzt ist, können Skript-Engines wie JavaScript nicht auf das Cookie zugreifen. Syntax
public void setHttpOnly(boolean httpOnly)
ScopeDie obige Methode erfordert nur einen Parameter: httpOnly – wenn das Cookie HTTP ist bedeutet nur „true“, was bedeutet, dass es als Teil der HTTP-Anfrage sichtbar ist. Rückgabe Nicht zutreffend Beispiel 1import java.net.HttpCookie;
public class JavaHttpCookieSetHttpOnlyExample1 {
public static void main(String[] args) {
HttpCookie cookie = new HttpCookie("Student", "1");
// Indicate whether the cookie can be considered as HTTP Only or not.
cookie.setHttpOnly(true);
// Return true if the cookie is considered as HTTPOnly.
System.out.println("Check whether the cookie is HTTPOnly: "+cookie.isHttpOnly());
}
}
Ausgabe: #🎜 🎜#Überprüfen Sie, ob das Cookie HTTPOnly ist: true Cookie ist HTTPOnly: falseBeispiel 3
import java.net.HttpCookie; public class JavaHttpCookieSetHttpOnlyExample2 { public static void main(String[] args) { HttpCookie cookie = new HttpCookie("Student", "1"); // Indicate whether the cookie can be considered as HTTP Only or not. cookie.setHttpOnly(false); // Return false if the cookie is not considered as HTTPOnly. System.out.println("Check whether the cookie is HTTPOnly: "+cookie.isHttpOnly()); } }
Ausgabe: true
Überprüfen Sie, ob das zweite Cookie ist HTTPOnly:falseDas obige ist der detaillierte Inhalt vonWie setze ich ein HttpOnly-Cookie in Java?. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Heiße KI -Werkzeuge

Undresser.AI Undress
KI-gestützte App zum Erstellen realistischer Aktfotos

AI Clothes Remover
Online-KI-Tool zum Entfernen von Kleidung aus Fotos.

Undress AI Tool
Ausziehbilder kostenlos

Clothoff.io
KI-Kleiderentferner

AI Hentai Generator
Erstellen Sie kostenlos Ai Hentai.

Heißer Artikel

Heiße Werkzeuge

MinGW – Minimalistisches GNU für Windows
Dieses Projekt wird derzeit auf osdn.net/projects/mingw migriert. Sie können uns dort weiterhin folgen. MinGW: Eine native Windows-Portierung der GNU Compiler Collection (GCC), frei verteilbare Importbibliotheken und Header-Dateien zum Erstellen nativer Windows-Anwendungen, einschließlich Erweiterungen der MSVC-Laufzeit zur Unterstützung der C99-Funktionalität. Die gesamte MinGW-Software kann auf 64-Bit-Windows-Plattformen ausgeführt werden.

SublimeText3 chinesische Version
Chinesische Version, sehr einfach zu bedienen

EditPlus chinesische Crack-Version
Geringe Größe, Syntaxhervorhebung, unterstützt keine Code-Eingabeaufforderungsfunktion

Herunterladen der Mac-Version des Atom-Editors
Der beliebteste Open-Source-Editor

Senden Sie Studio 13.0.1
Leistungsstarke integrierte PHP-Entwicklungsumgebung