【翻译教程】enc28J60 和 Arduino (后续)
张老师的文章好久没有更新了,地址在这里:【翻译教程】enc28J60 和 Arduino (汇总)
看了以后获益匪浅,正好看到还有后续,就贴上来了,大家看看吧
enc28J60 和 Arduino (15)——基本身份认证
今天的教程是关于马丁的请求:写一个小小的访问受保护的区域(使用一个网站的用户名和密码)。
基本身份验证
最简单的验证方法的HTTP协议支持为基本身份验证。
如果您试图访问一个安全区域,服务器响应您的请求与代码401,要求浏览器指定一个有效的用户名和密码。通常情况下,浏览器会显示一个用于插入所请求的值的对话框:
http://www.lucadentella.it/blog/wp-content/uploads/2013/08/ba_dialog.jpg
译者:去掉匿名访问,增加基本身份验证,XP下可以,WIN10未成功
用户名和密码都是加入了一个字符串,它们之间用一个冒号(用户:密码)分隔。然后base64编码和发送到服务器使用HTTP标头:
授权:基本stringa_base64
例如,如果您输入的用户名是luca,密码是mys3cr3t,你可以使用一个在线转换器获得授权头正确的字符串:转换网址在张老师前面的帖子里面有的。实测这个可以http://tool.diannao.wang/htaccess/#a_basic
http://www.lucadentella.it/blog/wp-content/uploads/2013/08/ba_base64.jpg
服务器配置
你需要配置你的网络服务器上启用文件夹的基本认证。大多数Web服务器支持通过.htaccess文件来配置,保存在同一文件夹中。
首先,准备一个与用户和密码文件;该文件通常命名为.htpasswd。使用一个在线工具对数据进行编码,并在您的文件中键入所产生的字符串,然后将其上载到要被保护的文件夹中:
http://www.lucadentella.it/blog/wp-content/uploads/2013/08/ba_htpasswd.jpg
现在创建一个新的.htaccess文件并粘贴以下配置:win10无法新建,提示必须键入文件名,需要进CMD创建。
AuthType Basic
AuthName "Secure folder"
AuthUserFile /htdocs/demo/secure/.htpasswd
Require valid-user
你用authType配置身份验证类型(“基本”),而authname可以为安全区域指定一个描述性名称。您必须指定,htpasswd位置,使用绝对路径。你可能会发现它使用一个简单的PHP脚本。
最后,您可以配置Web服务器进行身份验证的任何有效的用户包括在.htpasswd文档(“有效用户”)或指定授权用户需要单用户的名称。
上传.htaccess文件的文件夹:
http://www.lucadentella.it/blog/wp-content/uploads/2013/08/ba_ftp.jpg
Arduino代码
#include <EtherCard.h>
static byte mymac[] = {0xDD,0xDD,0xDD,0x00,0x00,0x01};
byte Ethernet::buffer;
byte session_id;
Stash stash;
char website[] PROGMEM = "www.lucadentella.it";
char authorization[] PROGMEM = "bHVjYTpNeVMzY3Izdb==";
void setup () {
Serial.begin(9600);
Serial.println("Basic authentication demo");
Serial.println();
if (!ether.begin(sizeof Ethernet::buffer, mymac, 10)) {
Serial.println( "Failed to access Ethernet controller");
while(1);
} else Serial.println("Ethernet controller initialized");
Serial.println();
if (!ether.dhcpSetup()) {
Serial.println("Failed to get configuration from DHCP");
while(1);
} else Serial.println("DHCP configuration done:");
ether.printIp("IP Address:\t", ether.myip);
ether.printIp("Netmask:\t", ether.netmask);
ether.printIp("Gateway:\t", ether.gwip);
//对方网络IP
if (!ether.dnsLookup(website)) {
Serial.println("DNS failed");
while(1);
} else ether.printIp("Website IP:\t", ether.hisip);
Serial.println();
Stash::prepare(PSTR("GET /demo/secure/ HTTP/1.1" "\r\n"
"Host: $F" "\r\n"
"Authorization: Basic $F" "\r\n"
"\r\n"), website, authorization);
session_id = ether.tcpSend();
Serial.println("Request sent");
}
void loop() {
ether.packetLoop(ether.packetReceive());
const char* reply = ether.tcpReply(session_id);
if(reply != 0) {
if(strstr(reply, "HTTP/1.1 401") != 0) Serial.println("Authorization required :("); //需要验证
else if(strstr(reply, "HTTP/1.1 200") != 0) Serial.println("Access granted! :)"); //允许访问
Serial.println();
Serial.println("---------- RAW RESPONSE ----------");
Serial.println(reply);
Serial.println("----------------------------------");
}
}
谢谢分享学习一下 据说张老师已经退出Arduino圈子了 T.T 幻生幻灭 发表于 2016-7-12 08:02 static/image/common/back.gif
据说张老师已经退出Arduino圈子了 T.T
只要他的帖子还在,就一直在 备注一下,那个代码中网络传输过程发送的授权码是基本base编码生成的,通过user:password生成的。搜索”base64编码“就有好多网站。
页:
[1]