最新更新 sitemap 网站制作设计本站搜索
网页设计
国外网站 韩国网站 个人主页 手提袋设计 CSS 网页特效 平面设计 网站设计 Flash CMS技巧 服装网站 php教程 photoshop 画册 服务器选用 数据库 Office
虚拟主机 域名注册 云主机 网页设计 客服QQ:8208442
当前位置:首页 > 编程开发 > jsp教程

jsp中作HTTP认证的方法

日期:11-11    来源:中国设计秀    作者:cnwebshow.com

    最近研究了jsp中作HTTP认证的问题,它的工作方式如下:iH9中国设计秀

1、server发送一个要求认证代码401和一个头信息WWW-authenticate,激发browser弹出一个认证窗口iH9中国设计秀

2、server取得browser送来的认证头"Authorization",它是加密的了,要用Base64方法解密,取得明文的用户名和密码iH9中国设计秀

3、检查用户名和密码,根据结果传送不同的页面iH9中国设计秀

iH9中国设计秀
以下是jsp的片断,你也可以把它做成include文件。和Base64的加解密的class源码。 iH9中国设计秀
如有兴趣可与我联系:unixboy@yeah.netiH9中国设计秀

<jsp:useBean id="base64"scope="page"class="Base64"/> iH9中国设计秀
<% iH9中国设计秀
if(request.getHeader("Authorization")==null){ iH9中国设计秀
   response.setStatus(401); iH9中国设计秀
   response.setHeader("WWW-authenticate","Basic realm="unixboy.com""); iH9中国设计秀
}else{ iH9中国设计秀
   String encoded=(request.getHeader("Authorization")); iH9中国设计秀
   String tmp=encoded.substring(6); iH9中国设计秀
   String up=Base64.decode(tmp); iH9中国设计秀
   String user=""; iH9中国设计秀
   String password=""; iH9中国设计秀
   if(up!=null){ iH9中国设计秀
        user=up.substring(0,up.indexOf(":")); iH9中国设计秀
    password=up.substring(up.indexOf(":")+1); iH9中国设计秀
   } iH9中国设计秀
   if(user.equals("unixboy")&&password.equals("123456")){ iH9中国设计秀
        //认证成功 iH9中国设计秀
   }else{ iH9中国设计秀
        //认证失败 iH9中国设计秀
   } iH9中国设计秀
} iH9中国设计秀
%>iH9中国设计秀

iH9中国设计秀
//消息加解密class iH9中国设计秀
public class Base64 iH9中国设计秀
{ iH9中国设计秀
        /** decode a Base 64 encoded String. iH9中国设计秀
          *<p><h4>String to byte conversion</h4> iH9中国设计秀
          * This method uses a naive String to byte interPRetation, it simply gets each iH9中国设计秀
          * char of the String and calls it a byte.</p> iH9中国设计秀
          *<p>Since we should be dealing with Base64 encoded Strings that is a reasonable iH9中国设计秀
          * assumption.</p> iH9中国设计秀
          *<p><h4>End of data</h4> iH9中国设计秀
          * We don''t try to stop the converion when we find the"="end of data padding char. iH9中国设计秀
          * We simply add zero bytes to the unencode buffer.</p> iH9中国设计秀
        */ iH9中国设计秀
        public static String decode(String encoded) iH9中国设计秀
        { iH9中国设计秀
                StringBuffer sb=new StringBuffer(); iH9中国设计秀
                int maxturns; iH9中国设计秀
                //work out how long to loop for. iH9中国设计秀
                if(encoded.length()%3==0) iH9中国设计秀
                maxturns=encoded.length(); iH9中国设计秀
                else iH9中国设计秀
                maxturns=encoded.length()+(3-(encoded.length()%3)); iH9中国设计秀
                //tells us whether to include the char in the unencode iH9中国设计秀
                boolean skip; iH9中国设计秀
                //the unencode buffer iH9中国设计秀
                byte[] unenc=new byte[4]; iH9中国设计秀
                byte b; iH9中国设计秀
                for(int i=0,j=0;i<maxturns;i++) iH9中国设计秀
                { iH9中国设计秀
                        skip=false; iH9中国设计秀
                        //get the byte to convert or 0 iH9中国设计秀
                        if(i<encoded.length()) iH9中国设计秀
                        b=(byte)encoded.charAt(i); iH9中国设计秀
                        else iH9中国设计秀
                        b=0; iH9中国设计秀
                        //test and convert first capital letters, lowercase, digits then ''+'' and ''/'' iH9中国设计秀
                        if(b>=65&&b<91) iH9中国设计秀
                        unenc[j]=(byte)(b-65); iH9中国设计秀
                        else if(b>=97&&b<123) iH9中国设计秀
                        unenc[j]=(byte)(b-71); iH9中国设计秀
                        else if(b>=48&&b<58) iH9中国设计秀
                        unenc[j]=(byte)(b+4); iH9中国设计秀
                        else if(b==''+'') iH9中国设计秀
                        unenc[j]=62; iH9中国设计秀
                        else if(b==''/'') iH9中国设计秀
                        unenc[j]=63; iH9中国设计秀
                        //if we find"="then data has finished, we''re not really dealing with this now iH9中国设计秀
                        else if(b==''='') iH9中国设计秀
                        unenc[j]=0; iH9中国设计秀
                        else iH9中国设计秀
                        { iH9中国设计秀
                                char c=(char)b; iH9中国设计秀
                                if(c==''n'' || c==''r'' || c=='' '' || c==''t'') iH9中国设计秀
                                skip=true; iH9中国设计秀
                                else iH9中国设计秀
                                //could throw an exception here? it''s input we don''t understand. iH9中国设计秀
                                ; iH9中国设计秀
                        } iH9中国设计秀
                        //once the array has boiled convert the bytes back into chars iH9中国设计秀
                        if(!skip&&++j==4) iH9中国设计秀
                        { iH9中国设计秀
                                //shift the 6 bit bytes into a single 4 octet word iH9中国设计秀
                                int res=(unenc[0]<<18)+(unenc[1]<<12)+(unenc[2]<<6)+unenc[3]; iH9中国设计秀
                                byte c; iH9中国设计秀
                                int k=16; iH9中国设计秀
                                //shift each octet down to read it as char and add to StringBuffer iH9中国设计秀
                                while(k>=0) iH9中国设计秀
                                { iH9中国设计秀
                                        c=(byte)(res>>k); iH9中国设计秀
                                        if ( c>0 ) iH9中国设计秀
                                        sb.append((char)c); iH9中国设计秀
                                        k-=8; iH9中国设计秀
                                } iH9中国设计秀
                                //reset j and the unencode buffer iH9中国设计秀
                                j=0; iH9中国设计秀
                                unenc[0]=0;unenc[1]=0;unenc[2]=0;unenc[3]=0; iH9中国设计秀
                        } iH9中国设计秀
                } iH9中国设计秀
                return sb.toString(); iH9中国设计秀
        } iH9中国设计秀
         iH9中国设计秀
        /** encode plaintext data to a base 64 string iH9中国设计秀
          * @param plain the text to convert. If plain is longer than 76 characters this method iH9中国设计秀
          *             returns null (see RFC2045). iH9中国设计秀
          * @return the encoded text (or null if string was longer than 76 chars). iH9中国设计秀
        */ iH9中国设计秀
        public static String encode(String plain) iH9中国设计秀
        { iH9中国设计秀
                if(plain.length()>76) iH9中国设计秀
                return null; iH9中国设计秀
                int maxturns; iH9中国设计秀
                StringBuffer sb=new StringBuffer(); iH9中国设计秀
                //the encode buffer iH9中国设计秀
                byte[] enc=new byte[3]; iH9中国设计秀
                boolean end=false; iH9中国设计秀
                for(int i=0,j=0;!end;i++) iH9中国设计秀
                { iH9中国设计秀
                        char _ch=plain.charAt(i); iH9中国设计秀
                        if(i==plain.length()-1) iH9中国设计秀
                        end=true; iH9中国设计秀
                        enc[j++]=(byte)plain.charAt(i); iH9中国设计秀
                        if(j==3 || end) iH9中国设计秀
                        { iH9中国设计秀
                                int res; iH9中国设计秀
                                //this is a bit inefficient at the end point iH9中国设计秀
                                //worth it for the small decrease in code size? iH9中国设计秀
                                res=(enc[0]<<16)+(enc[1]<<8)+enc[2]; iH9中国设计秀
                                int b; iH9中国设计秀
                                int lowestbit=18-(j*6); iH9中国设计秀
                                for(int toshift=18;toshift>=lowestbit;toshift-=6) iH9中国设计秀
                                { iH9中国设计秀
                                        b=res>>>toshift; iH9中国设计秀
                                        b&=63; iH9中国设计秀
                                        if(b>=0&&b<26) iH9中国设计秀
                                        sb.append((char)(b+65)); iH9中国设计秀
                                        if(b>=26&&b<52) iH9中国设计秀
                                        sb.append((char)(b+71)); iH9中国设计秀
                                        if(b>=52&&b<62) iH9中国设计秀
                                        sb.append((char)(b-4)); iH9中国设计秀
                                        if(b==62) iH9中国设计秀
                                        sb.append(''+''); iH9中国设计秀
                                        if(b==63) iH9中国设计秀
                                        sb.append(''/''); iH9中国设计秀
                                        if(sb.length()%76==0) iH9中国设计秀
                                        sb.append(''n''); iH9中国设计秀
                                } iH9中国设计秀
                                //now set the end chars to be pad character if there iH9中国设计秀
                                //was less than integral input (ie: less than 24 bits) iH9中国设计秀
                                if(end) iH9中国设计秀
                                { iH9中国设计秀
                                        if(j==1) iH9中国设计秀
                                        sb.append("=="); iH9中国设计秀
                                        if(j==2) iH9中国设计秀
                                        sb.append(''=''); iH9中国设计秀
                                } iH9中国设计秀
                                enc[0]=0;enc[1]=0;enc[2]=0; iH9中国设计秀
                                j=0; iH9中国设计秀
                        } iH9中国设计秀
                } iH9中国设计秀
                return sb.toString(); iH9中国设计秀
        } iH9中国设计秀
} iH9中国设计秀
 iH9中国设计秀

本文引用地址:/bc/article_46753.html
网站地图 | 关于我们 | 联系我们 | 网站建设 | 广告服务 | 版权声明 | 免责声明