频道直达 - 学院 - 下载 - 交易 - 特效 - 字库 - 手册 -排名-工具- 繁體
网页教学网站开发 设为首页
加入收藏
联系我们
建站搜索: 常用广告代码   用户注册 | 用户登陆
您当前的位置:中国建站之家 -> 网站开发设计技术教程 -> JSP教程 -> 在jsp中作HTTP认证的方法

在jsp中作HTTP认证的方法

作者:未知  来源:转载  发布时间:2005-7-28 11:32:04  发布人:acx

减小字体 增大字体

    最近研究了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();
        }
}


将本文收藏到QQ书签与更多好友分享
[打 印]
[] [返回上一页] [收 藏]
∷相关文章评论∷    (评论内容只代表网友观点,与本站立场无关!) [更多评论...]
精彩推荐
热门文章
· 注册码大全二
· 注册码大全四
· 注册码大全一
· 要10G免费网络硬盘的请进..
· 通过google 赶快来赚美金..
· 注册码大全十
· 头像-qq头像(qq新头像)4..
· 让你轻松架设FTP服务器1..
· 注册码大全三
· 梦幻背景图片7
· 卡通动物图片6
· 网页制作素材-按钮素材2..
· 让你轻松架设FTP服务器5..
· 风景图片8
· 注册码大全九
· 让你轻松架设FTP服务器2..
关注此文读者还看过
· SQL Server 中易混淆的数..
· 如何在PHP中使用Oracle数..
· 让Calendar在页面调用时..
· 《ASP网页制作教程》笔记..
· 大脚:29个Google AdSen..
· <展现C#> 前言(补充) 和..
· 网页制作深入浅出2
· 安装resin+mysql+IIS+JD..
· AspHTTP的组件的获取使用..
· 聚焦新经济:中国“80后..
· Java 5.0 多线程编程实践..
· 发送带附件的HTML格式邮..
· M327站长联盟开通了。。..
· 怎样查找对方的IP地址
· Asp HTTP 简要介绍
· 正则表达式简介(微软)..
相关文章
· 在JSP环境中配置使用fckedi..
· 实例讲解在JSP开发中的对象..
· 在JSP中使用JavaMail
· PHP+APACHE实现用户认证的方..
· 浅谈PHP+MYSQL身份验证的方..
· 在jsp程序中使用com组件
· 利用iText在JSP中生成PDF报..
· 在jsp中用bean和serv..
· 在JSP中使用JavaMail(二)
· 在JSP中使用JavaMail(一)
· 在jsp中发送email
· 学习在JSP中使用JavaBeans
· 在JSP页面中实现检索数据的..
· javamail在jsp中调用
· 在JSP、ASP和PHP网站网页中..
· PHP+APACHE实现用户论证的方..
关于本站 - 网站帮助 - 广告合作 - 下载声明 - 友情连接 - 网站地图 - 人才招聘
网站合作、内容监督、商务咨询:QQ: 9576619
Copyright ? 2005--2008 中国建站之家版权所有
粤ICP备05092265号