`
blueyanghualong
  • 浏览: 225343 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

HTTP Basic Authentication认证

阅读更多

 

什么是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);
    }
});
下面摘录一段 Jsp实现鉴权的代码逻辑
 以下是一段Jsp鉴权操作 
1、server发送一个要求认证代码401和一个头信息WWW-authenticate,激发browser弹出一个认证窗口
 2、server取得browser送来的认证头"Authorization",它是加密的了,要用Base64方法解密,取得明文的用户名和密码
  
3、检查用户名和密码,根据结果传送不同的页面
 
=========Jsp代码===================
<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();   
}   
}   
分享到:
评论
1 楼 yiqing 2016-05-22  
   不错 有帮助  

相关推荐

    http basic authentication通过post方式访问api示例分享 basic认证示例

    HTTP基本认证(Basic Authentication)是HTTP协议中一种简单的身份验证机制。它要求用户在访问受保护资源时提供用户名和密码。这种认证方式是基于HTTP头部的,通过将用户名和密码编码为Base64字符串并将其放入`...

    RestTemplate如何通过HTTP Basic Auth认证.docx

    HTTP Basic Authentication 是一种简单的身份验证机制,它要求客户端在每个请求中都包含一个认证头(Authorization)。在 Basic Auth 中,用户名和密码被组合成一个字符串(例如 "admin:admin"),然后使用 Base64 ...

    java实现HTTP 基本认证 (Basic Authentication)

    java实现HTTP 基本认证 (Basic Authentication) 大家在登录网站的时候,大部分时候是通过一个表单提交登录信息。 但是有时候浏览器会弹出一个登录验证的对话框,如下图,这就是使用 HTTP 基本认证。 下面来看看一...

    2.CXF安全访问之Http Basic Auth(一)

    本文将深入探讨CXF安全访问的一个重要方面:HTTP基本认证(Http Basic Auth)。这是一种简单但有效的身份验证机制,适用于对Web服务进行安全控制。 HTTP基本认证是基于HTTP协议的,它在请求头中包含一个Base64编码...

    webService添加basic验证

    3. **指定认证方式**:在`&lt;login-config&gt;`元素中设置`&lt;auth-method&gt;`为`BASIC`,并定义`&lt;realm-name&gt;`来标识验证区域。 4. **定义角色**:通过`&lt;security-role&gt;`元素定义角色名称,例如`bank_member`。 5. **完整...

    nginx系列(十五)nginx下启用http_auth_basic

    **标题:“Nginx系列(十五):在Nginx下启用HTTP Basic Authentication”** **内容概述:** 本文将深入探讨如何在Nginx服务器上启用HTTP Basic Authentication,这是一种广泛使用的身份验证方法,用于保护网站...

    BasicAuth的Java服务端实现

    **标题解析:** "BasicAuth的Java服务端实现" 指的是使用Java语言在服务端实现HTTP的基本认证(Basic Authentication)机制。这种认证方式是HTTP协议标准的一部分,用于在网络服务器上验证用户身份。 **基本认证...

    C#进阶系列 WebApi身份认证解决方案推荐:Basic基础认证

    【C#进阶系列:WebApi身份认证解决方案推荐——Basic基础认证】 在Web开发中,尤其是在涉及API服务时,确保接口的安全性至关重要。C#的WebApi提供了多种身份验证方式,其中Basic认证是一种简单但实用的策略。本文将...

    acegi basic认证具体demo

    在本Demo中,我们将深入探讨如何使用Acegi实现Basic认证,这是一种常见的HTTP身份验证方法,适用于简单的应用场景。 Basic认证是一种客户端-服务器认证方式,用户凭据(用户名和密码)以Base64编码的形式包含在请求...

    浅谈HTTP使用BASIC认证的原理及实现方法

    HTTP基本认证(BASIC Authentication)是一种简单的身份验证机制,常用于Web服务器对客户端进行身份验证。本文将深入探讨BASIC认证的原理以及如何在实际环境中实现这一机制。 一、BASIC认证概述 BASIC认证是HTTP...

    调用API

    然后进入新浪微博开放平台查看相关文档,在文档中(使用Basic Auth进行用户验证)发现新浪微博开发团队推荐了园子里的Q.Lee.lulu写的一篇博文:访问需要HTTP Basic Authentication认证的资源的各种语言的实现。...

    java 发送带Basic Auth认证的http post请求实例代码

    在Java编程中,有时我们需要向HTTP服务器发送带有特定身份验证的POST请求,例如使用Basic Authentication。在这种情况下,我们可以使用Apache HttpClient库来实现。下面将详细解释如何使用Java发送带Basic Auth认证...

    ring-basic-authentication:强制执行基本身份验证的环形中间件

    Ring-Basic-Authentication是针对Clojure Web开发框架Ring的一个中间件,它允许开发者在Web应用程序中实现基本的HTTP身份验证机制。这个中间件是基于HTTP协议的“Basic Authentication”规范,该规范要求用户在访问...

    Tomcat Basic Form认证实例!

    1. **Basic认证**:这是一种简单的HTTP认证方式,用户凭据(用户名和密码)通过Base64编码的HTTP头发送到服务器。这种方式不安全,因为凭据在网络中明文传输,但适用于测试环境或内部网络。 2. **Form认证**:Form...

    路由器Basic Auth暴力破解器 v1.0 (VC源码)

    采用VC编程,源碼VS2010编译...针对使用Basic Authentication认证的管理后台登录方式做暴力破解 程序有小BUG,但是猜解没有问题 相关开发进度请参阅CSDN BLOG http://blog.csdn.net/yehjordan/article/details/18892355

    aspnet-core-3-basic-authentication-api:ASP.NET Core 3.1-基本的HTTP身份验证API

    基本的HTTP身份验证(Basic Authentication)是一种常见的身份验证机制,它通过在HTTP头中传递用户名和密码来实现。下面我们将深入探讨ASP.NET Core 3.1中的基本HTTP身份验证API及其使用方法。 首先,让我们了解...

    关于Rails登录和验证插件http_authentication restful-authentication

    首先,让我们了解HTTP基本认证(http_authentication)。HTTP基本认证是一种简单的身份验证机制,它通过HTTP头信息进行用户凭据的传递。在Rails中,你可以通过集成ActionController::HttpAuthentication模块来实现这...

    Basic-Authentication-in-Postman:轻松实现POSTMAN中的脚本

    本文将深入探讨如何在Postman中实现基本身份验证(Basic Authentication),并利用JavaScript脚本来简化这一过程。 基本身份验证是一种简单的身份验证机制,它通过在HTTP请求头中添加一个Authorization字段,该字段...

Global site tag (gtag.js) - Google Analytics