`
lijuntian526
  • 浏览: 46673 次
  • 性别: Icon_minigender_1
最近访客 更多访客>>
社区版块
存档分类
最新评论

在jsp中作HTTP认证的方法

    博客分类:
  • JAVA
阅读更多

最近研究了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()v==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();
}
}
分享到:
评论

相关推荐

    JSP&Servlet;学习笔记

    本书是作者多年来教学实践经验的总结,汇集了...本书在讲解的过程中,以“微博”项目贯穿全书,随着每一章的讲述都在适当的时候将JSP & Servlet技术应用于“微博”程序之中,以便读者能了解完整的应用程序构建方法。

    jsp机试题目、程序员的考试认证

    很大同学的jsp考试不过关,我希望我上传的资料能够给他们很好的帮助

    JSP_Servlet学习笔记(第2版)

    本书在讲解的过程中,以“微博”项目贯穿全书,随着每一章的讲述都在适当的时候将JSP&Servlet技术应用于“微博”程序之中,使读者能够了解完整的应用程序构建方法。 本书适合JSP&Servlet初学者以及广大JSP&Servlet...

    JSP & Servlet学习笔记

    本书是作者多年来教学实践经验的总结,汇集了..., 本书在讲解的过程中,以“微博”项目贯穿全书,随着每一章的讲述都在适当的时候将JSP & Servlet技术应用于“微博”程序之中,以便读者能了解完整的应用程序构建方法。

    JSP&Servlet学习笔记.pdf

    本书是作者多年来教学实践经验的总结,汇集了... 本书在讲解的过程中,以“微博”项目贯穿全书,随着每一章的讲述都在适当的时候将JSP & Servlet技术应用于“微博”程序之中,以便读者能了解完整的应用程序构建方法。

    jsp认证题库

    有关JSP认证让我们更好的迎接认证考试,让我们轻松过认证

    springboot+jsp+xquery实现登录认证

    springboot+jsp+xquery实现登录认证

    JSP & Servlet学习笔记(第2版)

    本书是作者多年来教学实践经验的总结,汇集了...本书在讲解的过程中,以“微博”项目贯穿全书,随着每一章的讲述都在适当的时候将JSP & Servlet技术应用于“微博”程序之中,以便读者能了解完整的应用程序构建方法。

    JSP网络编程学习笔记源代码 part2

    第六篇为“Web应用高级专题”,主要讲述Servlet过滤器、JSP异常处理、JSP日志、认证和安全、部署等内容;第七篇为“Web应用开发实例”,围绕一个电子商务网站,从需求分析、架构选取、数据存储、开发、测试及部署等...

    JSP_Servlet学习笔记(第2版).pdf

    《JSP & Servlet学习笔记(第2版)》针对Servlet 3.0的新功能全面改版,无论章节架构还是范例程序代码,都做了全面更新,是作者多年来教学实践的经验总结,汇集了学员在教学过程中遇到的概念、操作、应用或认证考试上...

    jsp&servlet电子书

    作者多年来教学实践经验的总结,汇集了教学过程中学生在学习jsp & servlet时遇到的概念、操作、应用或认证考试等...

    jsp高级编程(适合高级用户使用)

    讲述JSP的一些高级应用,适合高级用户使用

    Head First Servlet & JSP

    Head First Servlet & JSP, 经典教程,适用于SCWCD,我就是用这本书98%通过认证。本人注释版,非扫描。

    简单权限管理系统(JSP+SQL+SSH)

    使用JSP+SQL Server2005+Struts、Spring、Hibernate制作的简单权限管理系统。适合初学者使用、学习。

    在JSP中如何实现MD5加密的方法

    在各种应用系统的开发中,经常需要存储用户信息,很多地方都要存储用户密码,而将用户密码直接存储在服务器上显然是不安全的,本文简要介绍在JSP中如何实现MD5加密的方法,希望能抛砖引玉。 (一)消息摘要简介 一...

    使用JSP实现用户登录验证功能

    使用JSP实现用户登录验证功能 创建index.jsp 代码: Insert title here 账号: 密码: 验证成功: 验证失败: 这是上机实验(第三次) 作者:艺博东

    JSP会员认证登录源码

    本代码是会员认证登录,分为普通会员登录和VIP会员登录

    JSP2.0技术手册pdf(带示例源码).zip

    JSP 2.0,重点介绍Java在展示层的两项重要技术:Java Servlet与JavaServer Pages。 它们是最重要的 Java 核心技术。对这两项技术的深入了解,将有助于您未来对于 JavaServer Faces(JSF)技术以及Java Web Services...

Global site tag (gtag.js) - Google Analytics