JavaでHttpOnly Cookieを設定するにはどうすればよいですか?

PHPz
リリース: 2023-04-22 18:37:08
転載
1695 人が閲覧しました

Httponly cookie は cookie セキュリティ ソリューションです。

httponly Cookie (IE6、FF3.0) をサポートするブラウザーでは、Cookie に「httponly」属性が設定されている場合、JavaScript スクリプトは Cookie 情報を読み取ることができなくなり、XSS を効果的に防止できます。攻撃を許可し、Web サイトにアプリケーションの安全性を高めます。

ただし、J2EE4 および J2EE5 Cookie には httponly 属性を設定するメソッドが用意されていないため、httponly 属性を設定する必要がある場合は、自分で処理する必要があります。

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());
    }
 
}
ログイン後にコピー

Java Ee 6.0 の Cookie は httponly を設定していることに注意してください。そのため、Java EE 6.0 互換コンテナ (Tomcat 7 など) と互換性がある場合は、cookie.sethttponly を使用して HTTPONLY を設定できます。 :

cookie.setHttpOnly(true);
ログイン後にコピー

Java HttpCookie クラスの setHttpOnly(Boolean httpOnly) メソッドは、Cookie を HTTPOnly とみなせるかどうかを示すために使用されます。 true に設定すると、JavaScript などのスクリプト エンジンから Cookie にアクセスできなくなります。

Syntax

public void setHttpOnly(boolean httpOnly)
ログイン後にコピー

Scope

上記のメソッドに必要なパラメータは 1 つだけです:

httpOnly - Cookie が HTTP のみの場合は true、つまり、Cookie が HTTP のみで表示されることを意味します。 HTTP リクエストの一部。

Return

該当なし

例 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());  
     }  
 }
ログイン後にコピー

出力:

Cookie が HTTPOnly であるかどうかを確認する: true

例 2

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());  
   }  
}
ログイン後にコピー

出力:

Cookie が HTTPOnly かどうかを確認します: false

例 3

import java.net.HttpCookie;  
public class JavaHttpCookieSetHttpOnlyExample3 {  
    public static void main(String[] args) {  
        HttpCookie cookie1 = new HttpCookie("Student1", "1");  
        HttpCookie cookie2 = new HttpCookie("Student2", "2");  
        //Indicate whether the cookie can be considered as HTTP Only or not.  
        cookie1.setHttpOnly(true);  
        cookie2.setHttpOnly(false);  
        System.out.println("Check whether the first cookie is HTTPOnly:"+cookie1.isHttpOnly());  
        System.out.println("Check whether the second cookie is HTTPOnly:"+cookie2.isHttpOnly());  
       }  
    }
ログイン後にコピー

出力:

最初の Cookie が HTTPOnly:true であるかどうかを確認します
2 番目の Cookie が HTTPOnly:false であるかどうかを確認します

以上がJavaでHttpOnly Cookieを設定するにはどうすればよいですか?の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

関連ラベル:
ソース:yisu.com
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
最新の問題
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!