简体   繁体   English

如何向表单添加基本身份验证标头

[英]How to add basic authentication header to form

I'm using node server built on https://github.com/passport/express-4.x-local-example(just changed app.get('/login' ... to app.post('/login' … in server.js. 我正在使用基于https://github.com/passport/express-4.x-local-example构建的节点服务器app.get('/login'只是将app.get('/login' ...更改为app.post('/login' …在server.js中。

In pug, I created a page with a login form based on https://www.w3schools.com/howto/howto_css_login_form.asp and when I submit form (input names changed to username and password, method="post", action "/login" ) everything works fine. 在哈巴狗中,我基于https://www.w3schools.com/howto/howto_css_login_form.asp创建了一个带有登录表单的页面,当我提交表单时(输入名称更改为用户名和密码, method="post", action "/login" ),一切正常。 Since I don't want to send passwords in a body without authentification, I need to add basic auth to my post request. 由于我不想在没有身份验证的情况下在主体中发送密码,因此我需要在发布请求中添加基本身份验证。 How do I do that? 我怎么做?

I tried adding event listener submit to my form and stopping default action with event.preventDefault(); 我尝试将事件侦听器提交添加到表单,并使用event.preventDefault();停止默认操作event.preventDefault(); . I then created new XMLHttpRequest() , set request header to basic auth and sent the XML request to server. 然后,我创建了new XMLHttpRequest() ,将请求标头设置为基本身份验证,并将XML请求发送到服务器。 When using console I can verify the request came in, did the job, but the reply from server (which should redirect) returned to my XML request, not actually redirecting the page. 使用控制台时,我可以验证请求是否已完成,但是来自服务器的响应(应重定向)返回到我的XML请求,而不是实际重定向页面。 When I try sending the same POST request via POSTMAN, the response is a redirect page. 当我尝试通过POSTMAN发送相同的POST请求时,响应为重定向页面。 If I remove event listener the form gets submitted and everything works fine (but without adding auth headers). 如果我删除事件侦听器,则将提交表单,并且一切正常(但不添加auth标头)。

  doctype html
  head
    meta(charset='UTF-8')
    title Login
    link(rel='stylesheet', href='stylesheets/main.css')
    link(rel='icon', type="image/png", href='favicon.png')
  body 
    form(action='/login', id='form1' method='post')
      .imgcontainer
        img.avatar(src='images/img_avatar2.png', alt='Avatar')
      .container
        label(for='username')
            b Username
        input(type='text', placeholder='Enter Username', name='username', autofocus, required, id='uname')
        label(for='password')
            b Password
        input(type='password', placeholder='Enter Password', name='password', required, id='pword')
        button(type='submit') Login

  script. 
       document.getElementById("form1").addEventListener("submit",   submitFunction);

       function submitFunction() {
            event.preventDefault(); 
            var usr=document.getElementById('uname').value;
            var pwd=document.getElementById('pword').value;
            var obj = {'username' : usr, 'password' : pwd};
            var request = new XMLHttpRequest();
            request.open("POST", "/login", false);
            request.setRequestHeader('Authorization', 'Basic Y2xpZW50SUQ6c2VjcmV0S2V5'); 
            request.setRequestHeader('Content-Type', 'application/json');
            request.send(JSON.stringify(obj));
       }

I found a workaround to include headers. 我找到了一种解决方法,包括标头。 First of all, I was using the wrong passport strategy. 首先,我使用了错误的护照策略。 I used local and should have used ('passport-http').BasicStrategy. 我使用本地,应该使用('passport-http')。BasicStrategy。 Here is an example https://github.com/passport/express-3.x-http-basic-example I added a placeholder for response in my XMLHttpRequest so the script part of my pug looks now like 这是一个示例https://github.com/passport/express-3.x-http-basic-example我在XMLHttpRequest中添加了用于响应的占位符,所以哈巴狗的脚本部分现在看起来像

script.
    document.getElementById("form1").addEventListener("submit", submitFunction); 
    function submitFunction() {
         event.preventDefault();   
         var username = document.getElementById('uname').value;
         var password = document.getElementById('pword').value;
         var request = new XMLHttpRequest();
         request.onreadystatechange = function() {
             if (this.readyState == 4) {
                    // Typical action to be performed when the document is ready:
                    // accept and redirect code in here based on the answer from the server
             }
         };
         request.open("POST", "/login", false);
         request.setRequestHeader('Authorization', "Basic " + btoa(username + ":" + password)); 
         request.setRequestHeader('Content-Type', 'application/json;charset=UTF-8');
         request.send();
       }

of course, as Absor said ih his answer (thank you) it's still just plain text so maybe it will not add security to my request. 当然,正如Absor所说的那样(谢谢你),它仍然只是纯文本,因此也许不会为我的请求增加安全性。

Authentication is not needed and will not make your request secure as without encryption the HTTP request is still plain text. 不需要身份验证,不会确保您的请求的安全性,因为未经加密,HTTP请求仍然是纯文本。

Encryption will make your request secure, your page and API should both use HTTPS, and when using encryption you do not need the additional authentication. 加密将使您的请求变得安全,您的页面和API都应使用HTTPS,并且在使用加密时,您不需要其他身份验证。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM