HTTP authentication method in jsp

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.