Recently I have studied HTTP Authentication in jsp (the preferred choice for SUN enterprise-level applications). It works as follows:
1. the server sends a request for authentication code 401 and a header message WWW-authenticate, and an authentication window pops up to stimulate the browser.
2. The server obtains the Authentication Header "Authorization" sent by the browser, which is encrypted and decrypted using Base64 to obtain the plaintext user name and password.
3. Check the user name and password and send different pages based on the results.
The following is a piece of jsp (the preferred choice for SUN Enterprise Applications). You can also make it an include file. And Base64 encryption and decryption class source code.
If you are interested, please contact me: unixboy@yeah.net
<Jsp (preferred for SUN Enterprise Applications): 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 ")){
// Authentication successful
} Else {
// Authentication failed
}
}
%>
// Message encryption/Decryption class
Public class Base64
{
/** Decode a Base 64 encoded String.
* <P> * This method uses a naive String to byte interpretation, it simply gets each
* Char of the String and callit a byte. </p>
* <P> Since we shoshould be dealing with Base64 encoded Strings that is a reasonable
* Assumption. </p>
* <P> * We dont 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.
If (encoded. length () % 3 = 0)