注册 | 登录
收藏 | 帮助
热门文章
编辑推荐
相关文章  
用MailSpy拦截局域网内危险的病毒
Win XP SP2拖放IE窗口可能引发黑
防范ASP木马的十大基本原则
杀毒软件如何被XP SP2的安全中心
服务器如何防范asp木马
杜绝入侵:八大法则防范ASP网站漏
黑客知识 巧妙配合asp木马取得管
实例讲解:全程追踪入侵JSP网站服
浅谈除不尽理还乱的Spyware间谍软
Microsoft AntiSpyware微软出品的
您现在的位置: 顶尖设计 >> IT学院 >> 编程开发 >> Jsp >> 文章正文
在jsp中作HTTP认证的方法
作者:佚名  来源:不详  点击:  更新:2006-12-17
简介:

    最近研究了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)
                        {
               

[1] [2] [3] 下一页






  • 上一篇文章:
  • 下一篇文章:
  • 分享此文:该页面添加到 Mister Wong 添加到雅虎Yahoo!收藏 Add to:Del.icio.us Post to Furl Digg this 添加到Google书签 reddit spurl blogmarks 365Key 评论  收藏  分享  打印
     我来说两句
    姓名:       验证码:   
    主页: 
    评分: 1分 2分 3分 4分 5分
    本频道近期热评文章:
      关于我们 | 联系我们 | 站点地图 | 广告投放 | 友情链接 | 在线留言 | 版权申明
    版权所有 © 2004-2007 顶尖设计(bobd.cn)
    未经授权禁止转载,摘编,复制本站内容或建立镜像. 沪ICP备07504942号 
    网络110
    报警服务