动网论坛,站长建站首选,国内使用量最多的论坛软件 动网论坛官方技术讨论区 站长工具 申请属于您自己的免费论坛
首页 | 新闻资讯 | 网站运营 | 网络编程 | 数据库 | 服务器 | 网页设计 | 图像媒体 | 网络应用 | 搜索优化 | 资源下载 | 动网主机 | DVBOX
    本站内  互联网 ASP论坛  ASP.Net论坛  PHP论坛
  
   Jsp → 阅读文章

 在jsp中作HTTP认证的方法

作者来源: 
阅读 数 254 人次 , 2006-3-29 4:23:00 


  最近研究了jsp中作http认证的问题,它的工作方式如下:

1、server发送一个要求认证代码401和一个头信息www-authenticate,激发browser弹出一个认证窗口

2、server取得browser送来的认证头"authorization",它是加密的了,要用base64方法解密,取得明文的用户名和密码

3、检查用户名和密码,根据结果传送不同的页面


以下是jsp的片断,你也可以把它做成include文件。和base64的加解密的class源码。
如有兴趣可与我联系:unixboy@yeah.net

<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{
  //认证失败
}
}
%>


//消息加解密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==''\n'' || c==''\r'' || c=='' '' || c==''\t'')
  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(''\n'');
  }
  //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();
  }
}

 
 收藏本文  打印本文  论坛讨论  关闭窗口
· 上一篇:JSP通过JDBC与Oracle相连
· 下一篇:在JSP页面中实现检索数据的分页显示
· JSP由浅入深(2)—— 第一个JSP
· JSP语法(13)
· JSP连接各类数据库大全(上)
· JSP由浅入深(1)—— 熟悉JSP服务器
· 小窗口大学问--玩转弹出窗口(2)


关于本站 | 联系我们 | 业务合作 | 客户案例 | 诚聘英才 | 广告合作 | 收藏本站
海口动网先锋网络科技有限公司版权所有
Copyright © 2000 - 2006 Cndw.Com
中华人民共和国电信与信息服务业务经营许可证编号 琼 ICP 020077