在jsp中作HTTP认证的方法

2016-01-29 12:14 7 1 收藏

在jsp中作HTTP认证的方法,在jsp中作HTTP认证的方法

【 tulaoshi.com - Java 】

 

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

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/java/)

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

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

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

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/java/)


以下是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<h4String 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
          *<pSince we should be dealing with Base64 encoded Strings that is a reasonable
          * assumption.</p
          *<p<h4End 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];
   &n

来源:https://www.tulaoshi.com/n/20160129/1485185.html

延伸阅读
标签: Web开发
1.设置errorPage:errorPage.jsp %@page isErrorPage="true"% html     head         meta http-equiv="Content-Type" content="text/html; charset=UTF-8"         titleJSP Page/title     /head     body  &nbs...
标签: Web开发
FCKeditor是sourceforge.net上面的一个开源项目,主要是实现在线网页编辑器的功能,可以让web程序拥有如MS Word这样强大的编辑功能。官方网站为http://www.fckeditor.net ,在服务器端支持ASP.Net、ASP、ClodFusion、PHP、Java等语言,并且支持IE 5+、Mozilla 、Netscape等主流浏览器。  首先在官方网站下载fckeditor,注意有两个...
标签: Web开发
我们最希望任何事都是完美的,如在企业应用中,我们只需要选择一个Web平台就可以部署所有的企业应用。但往往理想与现实相去甚远。在现实世界中,信息系统往往是由很多不同的操作系统、平台以及应用环境混合而成的。而且为了保持与旧的系统兼容,系统总是将当前的技术和以前的遗留技术进行混合,这样周而复始。就使系统变得越来越复杂。 &n...
标签: Web开发
1.设置errorPage:errorPage.jsp %@page isErrorPage="true"% html head meta http-equiv="Content-Type" content="text/html; charset=UTF-8" titleJSP Page/title /head body Error~! %=exception.getMessage()% /body /html 2.应用 %@page info="Bad page"% %@p...
标签: Java JAVA基础
      在页面中,当检索的数据很多时,通常需要分页显示数据,并要实现翻页。 下面将通过一些例程来说明实现JSP页面翻页技术的实现。 首先,在JSP中,通过JAVA servlet 来检索数据,而用JSP来调用结果来显示。 因而,此技术可分为两个部分(依赖关系): 1. 在服务器端的servlet 中的实现 要点: &将查询...

经验教程

695

收藏

98
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部