簡體   English   中英

Java socket Basic WWW-Authentication

[英]Java socket Basic WWW-Authentication

我想通過使用WWW-Authenticate: Basic header 在 Web 瀏覽器中驗證套接字連接,但沒有提示輸入用戶名和密碼。 go怎么辦呢?

   public static void responseView(Socket socket) throws IOException {
        responseHeaders(200);
        DataOutputStream outputStream = new DataOutputStream(socket.getOutputStream());
        for(String header : headers) {
            outputStream.writeBytes(header + "\r\n");
        }
        outputStream.writeBytes("\r\n");
        outputStream.writeBytes("<!DOCTYPE html><html><head><title>Java Web Server</title></head><body></body></html>");
        outputStream.writeBytes("\r\n");
        outputStream.flush();
    }
    
    public static void responseHeaders(int statusCode) {
        SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd 'at' HH:mm:ss z");
        headers.add("HTTP/1.0 " + Integer.toString(statusCode) + " " + Config.STATUS_CODES.get(statusCode));
        headers.add("Content-Type: text/html");
        headers.add("Date: " + formatter.format(new Date(System.currentTimeMillis())));
        headers.add("Server: Simple Java Web Server");
        headers.add("WWW-Authenticate: Basic");
    }

我想在 Web 瀏覽器中驗證套接字連接

基本訪問身份驗證不驗證套接字連接,而是驗證 HTTP 請求。 在單個底層套接字連接中可以有多個 HTTP 請求,具有不同的身份驗證或沒有身份驗證。 並且請求認證和發送認證請求可以在不同的連接中完成。

 responseHeaders(200);

請求身份驗證時的狀態碼必須是 401,而不是 200

 headers.add("WWW-Authenticate: Basic");

WWW-Authenticate header 必須包含realm屬性以向用戶顯示身份驗證的用途。

有關該主題的簡短概述,請參閱Wikipedia:基本訪問身份驗證:服務器端 最終的參考是標准

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM