Check HTTP for Basic Authentication code example-JSP

Source: Internet
Author: User

Check HTTP for Basic authentication. Since http1.0

The code looks like this:

<%@ page pageencoding= "UTF-8" contenttype= "text/html;charset=utf-8"%><%@ page import= "    Sun.misc.BASE64Decoder "%><%@ page import=" Java.io.IOException "%><%! Check HTTP for Basic authentication.  Since http1.0 public static Boolean Checkauth (HttpServletRequest request, string ID, string pwd) {Boolean Authok        = false;        Each HTTP request after authentication is accompanied by a Authorization header message String Authorization = Request.getheader ("Authorization"); if (null = = Authorization | |        Authorization.trim (). IsEmpty ()) {//need to certify return Authok;        }//string[] Basicarray = Authorization.split ("\\s+");        if (null = = Basicarray | | 2! = basicarray.length) {return authok;        }//String basic = basicarray[0];        String base64 = basicarray[1];            try {byte[] buf = new Base64decoder (). Decodebuffer (base64);            String idpass = new String (buf, "UTF-8"); if (null = = Idpass | | Idpass.trim ().IsEmpty ()) {//need to certify return Authok;            }//string[] Idpassarray = Idpass.split (":");            if (null = = Idpassarray | | 2! = idpassarray.length) {return authok;            } String _id = idpassarray[0];            String _pass = idpassarray[1];            if (Id.equalsignorecase (_id) && pwd.equalsignorecase (_pass)) {Authok = true;//authentication Successful        }} catch (IOException e) {e.printstacktrace ();    }//return Authok;        }//Do not rely on the this state method, in fact should be set to static public static void Requireauth (HttpServletResponse response, String msg) {        Send status code 401, cannot use Senderror, pit Response.setstatus (401, "Authentication Required");        Send a request to enter authentication information, the browser will pop up the input box Response.AddHeader ("Www-authenticate", "Basic realm=" + msg);    Return    }%><%//String Authorization = Request.getheader ("Authorization"); //    String userid = "admin";    String pwd = "11111111";    Boolean Authok = Checkauth (Request, UserID, PWD);        if (!authok) {//If authentication fails, then requires authentication Requireauth (response, "R U OK, Xiaomi");    Return }%>
Please refer to the comments in the code, specific information, you can also refer to "Graphical http". I looked at the HTTP-BASIC certified Preface in this book and wrote a demo code like this.



Check HTTP for Basic Authentication code example-JSP

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.