Check the HTTP Digest authentication code example-JSP, digest-jsp

Source: Internet
Author: User
Tags http digest authentication

Check the HTTP Digest authentication code example-JSP, digest-jsp

Check HTTP Digest authentication. since http1.1

The Code is as follows: (this code is not complete, and RFC2617 is not implemented ).

<% @ Page pageEncoding = "UTF-8" contentType = "text/html; charset = UTF-8" %> <% @ page import = "sun. misc. BASE64Decoder "%> <% @ page import =" java. io. IOException "%> <% @ page import =" java. util. UUID "%> <% @ page import =" java. util. hashMap "%> <% @ page import =" java. util. map "%> <%! // Check the Basic Authentication of HTTP. since http1.0 public static boolean checkAuth (HttpServletRequest request, String _ username, String _ password) {boolean authOK = false; // after authentication, the Authorization header is attached to each HTTP request. String Authorization = request. getHeader ("Authorization"); if (null = Authorization | Authorization. trim (). isEmpty () {// return authOK to be authenticated;} // sample data, without line breaks, with commas or spaces // Digest username = "admin", realm = "DIG EST tiemao ", // nonce =" 227c89449fd644a3b9df12e7cb8b0e33 ", uri ="/digest. jsp ", // algorithm = MD5, response =" canonical ", // qop = auth, nc = 00000001, cnonce =" f337ac5d88670ef5 "String [] digestArray = Authorization. split ("\ s +"); if (null = digestArray | digestArray. length <2) {return authOK;} // Map <String, String> authMap = new HashMap <String, String> (); for (int I = 0; I <digestA Rray. length; I ++) {String paraAndValue = digestArray [I]; // String [] pvArray = paraAndValue. split ("="); if (null = pvArray | 2! = PvArray. length) {continue; // do not process 0} String key = pvArray [0]; String value = pvArray [1]; // if (null = key | null = value) {} // value = value. replace ("'", ""); value = value. replace (",", ""); value = value. replace ("\" "," "); value = value. trim (); authMap. put (key, value);} // String username = authMap. get ("username"); String nonce = authMap. get ("nonce"); String response = authMap. get ("response ");/ /Here there should be an RFC2617 algorithm, which is consistent with the client, that is, calculating the user password if (_ username. equalsIgnoreCase (username) & checkAuth_RFC2617 (_ username, _ password, response) {authOK = true; // authentication successful,} // return authOK;} // RFC2617 operation, this algorithm is complex, not implemented at the moment // reference address: http://www.faqs.org/rfcs/rfc2617.html public static boolean checkAuth_RFC2617 (String _ username, String _ password, String response) {boolean authOK = false; if (null! = _ Username | null! = _ Password | response. equalsIgnoreCase (response) {authOK = true; // authentication successful,} return true;} // method that does not depend on the this status, in fact, it should be set to static public static void requireDigestAuth (HttpServletResponse response, String nonce) {// The sending Status Code 401, which cannot use sendError or pitfall response. setStatus (401, "Authentication Required"); // String authHeader = "Digest realm = \" DIGEST tiemao \ ""; authHeader + = ", nonce = \ "" + nonce + "\" "; authHeader + = ", Algorithm = MD5"; authHeader + = ", qop = \" "+" auth "+" \ ""; // The sender must enter the authentication information, then the browser will pop up the input box response. addHeader ("WWW-Authenticate", authHeader); return ;}%> <% // String Authorization = request. getHeader ("Authorization"); // String _ username = "admin"; String pwd = "11111111"; boolean authOK = checkAuth (request, _ username, pwd ); // if (! AuthOK) {// String nonce = UUID. randomUUID (). toString (). replace ("-", ""); // If authentication fails, requireDigestAuth (response, nonce); return ;} %> The code is described above ,.


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.