0

我想通过使用WWW-Authenticate: Basic标头在 Web 浏览器中验证套接字连接,但没有出现输入用户名和密码的提示。怎么做?

   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");
    }
4

1 回答 1

0

我想在 Web 浏览器中验证套接字连接

基本访问身份验证不验证套接字连接,而是验证 HTTP 请求。在单个底层套接字连接中可以有多个 HTTP 请求,具有不同的身份验证或没有身份验证。并且请求认证和发送认证请求可以在不同的连接中完成。

   responseHeaders(200);

请求身份验证时的状态码必须是 401,而不是 200

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

WWW-Authenticate头必须包含realm属性以向用户显示身份验证的用途。

有关该主题的简短概述,请参阅Wikipedia:基本访问身份验证:服务器端。最终的参考是标准

于 2022-01-31T03:00:19.773 回答