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