|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
但是对于JAVA技术类的学习,我觉得大课堂反而会影响自身独立思考的过程,因为上课的时候,老师讲课的速度很快为了不遗漏要点,通常会仔细的听,js
比来研讨了jsp中作HTTP认证的成绩,它的事情体例以下:
1、server发送一个请求认证代码401和一个头信息WWW-authenticate,引发browser弹出一个认证窗口
2、server获得browser送来的认证头"Authorization",它是加密的了,要用Base64办法解密,获得明文的用户名和暗码
3、反省用户名和暗码,依据了局传送分歧的页面
以下是jsp的片段,你也能够把它做成include文件。和Base64的加解密的class源码。
若有乐趣可与我接洽:unixboy@yeah.net
<jsp:useBeanid="base64"scope="page"class="Base64"/>
<%
if(request.getHeader("Authorization")==null){
response.setStatus(401);
response.setHeader("WWW-authenticate","Basicrealm="unixboy.com"");
}else{
Stringencoded=(request.getHeader("Authorization"));
Stringtmp=encoded.substring(6);
Stringup=Base64.decode(tmp);
Stringuser="";
Stringpassword="";
if(up!=null){
user=up.substring(0,up.indexOf(":"));
password=up.substring(up.indexOf(":")+1);
}
if(user.equals("unixboy")&&password.equals("123456")){
//认证乐成
}else{
//认证失利
}
}
%>
//动静加解密class
publicclassBase64
{
/**decodeaBase64encodedString.
*<p><h4>Stringtobyteconversion</h4>
*ThismethodusesanaiveStringtobyteinterpretation,itsimplygetseach
*charoftheStringandcallsitabyte.</p>
*<p>SinceweshouldbedealingwithBase64encodedStringsthatisareasonable
*assumption.</p>
*<p><h4>Endofdata</h4>
*Wedonttrytostoptheconverionwhenwefindthe"="endofdatapaddingchar.
*Wesimplyaddzerobytestotheunencodebuffer.</p>
*/
publicstaticStringdecode(Stringencoded)
{
StringBuffersb=newStringBuffer();
intmaxturns;
//workouthowlongtoloopfor.
if(encoded.length()%3==0)
maxturns=encoded.length();
else
maxturns=encoded.length()+(3-(encoded.length()%3));
//tellsuswhethertoincludethecharintheunencode
booleanskip;
//theunencodebuffer
byte[]unenc=newbyte[4];
byteb;
for(inti=0,j=0;i<maxturns;i++)
{
skip=false;
//getthebytetoconvertor0
if(i<encoded.length())
b=(byte)encoded.charAt(i);
else
b=0;
//testandconvertfirstcapitalletters,lowercase,digitsthen+and/
if(b>=65&&b<91)
unenc[j]=(byte)(b-65);
elseif(b>=97&&b<123)
unenc[j]=(byte)(b-71);
elseif(b>=48&&b<58)
unenc[j]=(byte)(b+4);
elseif(b==+)
unenc[j]=62;
elseif(b==/)
unenc[j]=63;
//ifwefind"="thendatahasfinished,werenotreallydealingwiththisnow
elseif(b===)
unenc[j]=0;
else
{
charc=(char)b;
if(c==
||c==||c==||c== )
skip=true;
else
//couldthrowanexceptionhere?itsinputwedontunderstand.
;
}
//oncethearrayhasboiledconvertthebytesbackintochars
if(!skip&&++j==4)
{
//shiftthe6bitbytesintoasingle4octetword
intres=(unenc[0]<<18)+(unenc[1]<<12)+(unenc[2]<<6)+unenc[3];
bytec;
intk=16;
//shifteachoctetdowntoreaditascharandaddtoStringBuffer
while(k>=0)
{
c=(byte)(res>>k);
if(c>0)
sb.append((char)c);
k-=8;
}
//resetjandtheunencodebuffer
j=0;
unenc[0]=0;unenc[1]=0;unenc[2]=0;unenc[3]=0;
}
}
returnsb.toString();
}
/**encodeplaintextdatatoabase64string
*@paramplainthetexttoconvert.Ifplainislongerthan76charactersthismethod
*returnsnull(seeRFC2045).
*@returntheencodedtext(ornullifstringwaslongerthan76chars).
*/
publicstaticStringencode(Stringplain)
{
if(plain.length()>76)
returnnull;
intmaxturns;
StringBuffersb=newStringBuffer();
//theencodebuffer
byte[]enc=newbyte[3];
booleanend=false;
for(inti=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)
{
intres;
//thisisabitinefficientattheendpoint
//worthitforthesmalldecreaseincodesize?
res=(enc[0]<<16)+(enc[1]<<8)+enc[2];
intb;
intlowestbit=18-(j*6);
for(inttoshift=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(
);
}
//nowsettheendcharstobepadcharacterifthere
//waslessthanintegralinput(ie:lessthan24bits)
if(end)
{
if(j==1)
sb.append("==");
if(j==2)
sb.append(=);
}
enc[0]=0;enc[1]=0;enc[2]=0;
j=0;
}
}
returnsb.toString();
}
}
JAVA学习必须明确这是一项投资,对于大多数的人来说,学习JAVA是为了就业,还有就是刚走向工作位置的朋友想尽快赶上工作的节奏。 |
|