http://smalltalllong.iteye.com/blog/912046

********************************************

    什么是HTTP Basic Authentication?直接看http://en.wikipedia.org/wiki/Basic_authentication_scheme吧。
在你访问一个需要HTTP Basic Authentication的URL的时候,如果你没有提供用户名和密码,服务器就会返回401,如果你直接在浏览器中打开,浏览器会提示你输入用户名和密码(google浏览器不会,bug?)。
你可以尝试点击这个url看看效果:http://api.minicloud.com.cn/statuses/friends_timeline.xml
要在发送请求的时候添加HTTP Basic Authentication认证信息到请求中,有两种方法:
一是在请求头中添加Authorization:
Authorization: "Basic 用户名和密码的base64加密字符串"
二是在url中添加用户名和密码:
http://userName:password@api.minicloud.com.cn/statuses/friends_timeline.xml //需要Base64见:http://www.webtoolkit.info/javascript-base64.html
function make_base_auth(user, password) {
var tok = user + ':' + pass;
var hash = Base64.encode(tok);
return "Basic " + hash;
} var auth = make_basic_auth('QLeelulu','mypassword');
var url = 'http://example.com'; // 原始JavaScript
xml = new XMLHttpRequest();
xml.setRequestHeader('Authorization', auth);
xml.open('GET',url) // ExtJS
Ext.Ajax.request({
url : url,
method : 'GET',
headers : { Authorization : auth }
}); // jQuery
$.ajax({
url : url,
method : 'GET',
beforeSend : function(req) {
req.setRequestHeader('Authorization', auth);
}
});
    1. 以下是一段Jsp鉴权操作
    2. 1、server发送一个要求认证代码401和一个头信息WWW-authenticate,激发browser弹出一个认证窗口
    3. 2、server取得browser送来的认证头"Authorization",它是加密的了,要用Base64方法解密,取得明文的用户名和密码
    4. 3、检查用户名和密码,根据结果传送不同的页面</pre>
    <jsp:useBean id="base64" scope="page" class="Base64"/>
<%
if(request.getHeader("Authorization")==null){
response.setStatus(401);
response.setHeader("WWW-authenticate", "Basic realm="unixboy.com"");
}else{
String encoded=(request.getHeader("Authorization"));
String tmp=encoded.substring(6);
String up=Base64.decode(tmp);
String user="";
String password="";
if(up!=null){
user=up.substring(0,up.indexOf(":"));
password=up.substring(up.indexOf(":")+1);
}
if(user.equals("unixboy")&&password.equals("123456")){
//认证成功
}else{
//认证失败
}
}
%> =======Java段代码================== //消息加解密class
public class Base64
{
/** decode a Base 64 encoded String.
* <p><h4>String to byte conversion</h4>
* This method uses a naive String to byte interpretation, it simply gets each
* char of the String and calls it a byte.</p>
* <p>Since we should be dealing with Base64 encoded Strings that is a reasonable
* assumption.</p>
* <p><h4>End of data</h4>
* We don′t try to stop the converion when we find the "=" end of data padding char.
* We simply add zero bytes to the unencode buffer.</p>
*/
public static String decode(String encoded)
{
StringBuffer sb=new StringBuffer();
int maxturns;
//work out how long to loop for.
if(encoded.length()%3==0)
maxturns=encoded.length();
else
maxturns=encoded.length()+(3-(encoded.length()%3));
//tells us whether to include the char in the unencode
boolean skip;
//the unencode buffer
byte[] unenc=new byte[4];
byte b;
for(int i=0,j=0; i<maxturns; i++)
{
skip=false;
//get the byte to convert or 0
if(i<encoded.length())
b=(byte)encoded.charAt(i);
else
b=0;
//test and convert first capital letters, lowercase, digits then ′+′ and ′/′
if(b>=65 && b<91)
unenc[j]=(byte)(b-65);
else if(b>=97 && b<123)
unenc[j]=(byte)(b-71);
else if(b>=48 && b<58)
unenc[j]=(byte)(b+4);
else if(b==′+′)
unenc[j]=62;
else if(b==′/′)
unenc[j]=63;
//if we find "=" then data has finished, we′re not really dealing with this now
else if(b==′=′)
unenc[j]=0;
else
{
char c=(char)b;
if(c==′ ′ || c==′ ′ || c==′ ′ || c==′ ′)
skip=true;
else
//could throw an exception here? it′s input we don′t understand.
;
}
//once the array has boiled convert the bytes back into chars
if(!skip && ++j==4)
{
//shift the 6 bit bytes into a single 4 octet word
int res=(unenc[0] << 18)+(unenc[1] << 12)+(unenc[2] << 6)+unenc[3];
byte c;
int k=16;
//shift each octet down to read it as char and add to StringBuffer
while(k>=0)
{
c=(byte)(res >> k);
if ( c > 0 )
sb.append((char)c);
k-=8;
}
//reset j and the unencode buffer
j=0;
unenc[0]=0;unenc[1]=0;unenc[2]=0;unenc[3]=0;
}
}
return sb.toString();
} /** encode plaintext data to a base 64 string
* @param plain the text to convert. If plain is longer than 76 characters this method
* returns null (see RFC2045).
* @return the encoded text (or null if string was longer than 76 chars).
*/
public static String encode(String plain)
{
if(plain.length()>76)
return null;
int maxturns;
StringBuffer sb=new StringBuffer();
//the encode buffer
byte[] enc=new byte[3];
boolean end=false;
for(int i=0,j=0; !end; i++)
{
char _ch=plain.charAt(i);
if(i==plain.length()-1)
end=true;
enc[j++]=(byte)plain.charAt(i);
if(j==3 || end)
{
int res;
//this is a bit inefficient at the end point
//worth it for the small decrease in code size?
res=(enc[0] << 16)+(enc[1] << 8)+enc[2];
int b;
int lowestbit=18-(j*6);
for(int toshift=18; toshift>=lowestbit; toshift-=6)
{
b=res >>> toshift;
b&=63;
if(b>=0 && b<26)
sb.append((char)(b+65));
if(b>=26 && b<52)
sb.append((char)(b+71));
if(b>=52 && b<62)
sb.append((char)(b-4));
if(b==62)
sb.append(′+′);
if(b==63)
sb.append(′/′);
if(sb.length()%76==0)
sb.append(′ ′);
}
//now set the end chars to be pad character if there
//was less than integral input (ie: less than 24 bits)
if(end)
{
if(j==1)
sb.append("==");
if(j==2)
sb.append(′=′);
}
enc[0]=0; enc[1]=0; enc[2]=0;
j=0;
}
}
return sb.toString();
}
}

HTTP Basic Authentication认证的更多相关文章

  1. HTTP Basic Authentication认证的各种语言 后台用的

    访问需要HTTP Basic Authentication认证的资源的各种语言的实现 无聊想调用下嘀咕的api的时候,发现需要HTTP Basic Authentication,就看了下. 什么是HT ...

  2. 访问需要HTTP Basic Authentication认证的资源的各种开发语言的实现

    什么是HTTP Basic Authentication?直接看http://en.wikipedia.org/wiki/Basic_authentication_scheme吧. 在你访问一个需要H ...

  3. HttpClient 三种 Http Basic Authentication 认证方式,你了解了吗?

    Http Basic 简介 HTTP 提供一个用于权限控制和认证的通用框架.最常用的 HTTP 认证方案是 HTTP Basic authentication.Http Basic 认证是一种用来允许 ...

  4. HTTP Basic Authentication认证(Web API)

    当下最流行的Web Api 接口认证方式 HTTP Basic Authentication: http://smalltalllong.iteye.com/blog/912046 什么是HTTP B ...

  5. 访问需要HTTP Basic Authentication认证的资源的c#的实现 将账号密码放入url

    string url = ""; string usernamePassword = username + ":" + password; HttpWebReq ...

  6. 访问需要HTTP Basic Authentication认证的资源的c#的实现

    string username="username"; string password="password"; //注意这里的格式哦,为 "usern ...

  7. HTTP Basic Authentication

    Client端发送请求, 要在发送请求的时候添加HTTP Basic Authentication认证信息到请求中,有两种方法:1. 在请求头中添加Authorization:    Authoriz ...

  8. Edusoho之Basic Authentication

    通过如下代码,可以正常请求并获取对应的数据: curl -X POST -H "Accept:application/vnd.edusoho.v2+json" -H "A ...

  9. Atitit HTTP 认证机制基本验证 (Basic Authentication) 和摘要验证 (Digest Authentication)attilax总结

    Atitit HTTP认证机制基本验证 (Basic Authentication) 和摘要验证 (Digest Authentication)attilax总结 1.1. 最广泛使用的是基本验证 ( ...

随机推荐

  1. 【ASP.NET】ASP.NET中权限验证使用OnAuthorization实现

    在项目开发中,通常我们都会涉及到用户登录才能访问的网页,比如购物网站,我们浏览商品,添加购物车(以前开发的时候在这里就需要登录用户,但是现在有了缓存的实现,这里可以将商品加入缓存,等到结账的时候再登录 ...

  2. MYSQL-使用mysqldump创建数据库快照

    对已经有数据的mysql库创建主从的时候,可以使用mysqldump创建数据库快照 #--master-data选项会自动加上开启复制需要的"change master to"语句 ...

  3. nginx last 和break redirect 和 permanent

    一.last & break (1)last 和 break 当出现在location 之外时,两者的作用是一致的没有任何差异. 注意一点就是,他们会跳过所有的在他们之后的rewrite 模块 ...

  4. APP服务端开发遇到的问题总结(后续再整理解决方法)

    IOS  AES对称加密,加密结果不同,问题解决 IOS http post请求,使用AFNetworing 框架,默认请求content-type为application/json ,所以无法使用@ ...

  5. 常用代码之三:jQuery为按钮绑定事件的代码

    如题,比如有一个按钮:<input type='button' class='btn-text' id ='addHtml' value='新增' /> 为它添加onclick事件的代码: ...

  6. Python selenium 滚动条 详解

    在我们使用Python + selenium 爬虫的时候,会遇到如下报错,原因是  当页面上的元素超过一屏后,想操作屏幕下方的元素,是不能直接定位到,会报元素不可见的. selenium.common ...

  7. docker-compose教程(安装,使用, 快速入门)

    1.Compose介绍Docker Compose是一个用来定义和运行复杂应用的Docker工具.一个使用Docker容器的应用,通常由多个容器组成.使用Docker Compose不再需要使用she ...

  8. 安装 Express

    首先假定你已经安装了 Node.js,接下来为你的应用创建一个目录,然后进入此目录并将其作为当前工作目录. $ mkdir myapp $ cd myapp 通过 npm init 命令为你的应用创建 ...

  9. CentOS SVN强制用户提交时写日志

    问题:在项目提交时候不写日志,在后期查看修改历史时需要对比版本才知道提交原因.解决方案:在svn服务端通过hooks在提交时强制要求写日志.#!/bin/shREPOS="$1"T ...

  10. Java数据结构和算法(四):栈

    一.简介 栈(英语:stack)又称为堆栈或堆叠,栈作为一种数据结构,是一种只能在一端进行插入和删除操作的特殊线性表.它按照先进后出的原则存储数据,先进入的数据被压入栈底(Bottom),最后的数据在 ...