Heim > Java > javaLernprogramm > Hauptteil

Wie setze ich ein HttpOnly-Cookie in Java?

PHPz
Freigeben: 2023-04-22 18:37:08
nach vorne
1703 Leute haben es durchsucht

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());
    }
 
}
Nach dem Login kopieren

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);
Nach dem Login kopieren

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)
Nach dem Login kopieren

Scope

Die 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 1

import 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());  
     }  
 }
Nach dem Login kopieren

Ausgabe:

#🎜 🎜#Überprüfen Sie, ob das Cookie HTTPOnly ist: true Cookie ist HTTPOnly: false

Beispiel 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());  
   }  
}
Nach dem Login kopieren

Ausgabe: true

Überprüfen Sie, ob das zweite Cookie ist HTTPOnly:false

Das 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!

Verwandte Etiketten:
Quelle:yisu.com
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!