| 网站首页 | Vip会员区 | 教程 | 下载 | 图片 | QQ家园 | 免费资源 | 在线服务 | 论坛 | 博客 | 程序开发 | It学堂 | 作品发布 | 
站点相关
代刻黑客光盘或订做光盘

精品软件程序定制

为您的网站或者服务器保驾护航
相关内容
最 新 热 门
相 关 文 章
没有相关文章
您现在的位置: 红色黑客联盟 >> 程序开发 >> Web开发 >> Jsp编程 >> 正文
在jsp中作HTTP认证的方法
文章录入:7747.Net    责任编辑:7747.Net  更新时间:2008-5-21 23:28:39

【字体:

    最近研究了jsp(SUN企业级应用的首选)中作HTTP认证的问题,它的工作方式如下:

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

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

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


以下是jsp(SUN企业级应用的首选)的片断,你也可以把它做成include文件。和Base64的加解密的class源码。
如有兴趣可与我联系:unixboy@yeah.net

<jsp(SUN企业级应用的首选):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)

您对本文章有什么意见或着疑问吗?请到论坛讨论您的关注和建议是我们前行的参考和动力
  • 上一个文章:

  • 下一个文章:
  • 发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
     | 设为首页 | 加入收藏 | 广告服务 | 我要投稿 | 关于我们 | 版权申明 | 免责声明 | 隐私声明 | 网站地图 |