大家在登錄網站的時候,大部分時候是通過一個表單提交登錄信息。
但是有時候瀏覽器會彈出一個登錄驗證的對話框,如下圖,這就是使用HTTP基本認證。
下面來看看一看這個認證的工作過程:
第一步: 客戶端發送http request 給服務器,服務器驗證該用戶是否已經登錄驗證過了,如果沒有的話,
服務器會返回一個401 Unauthozied給客戶端,並且在Response 的 header "WWW-Authenticate" 中添加信息。
如下圖。
第三步: 服務器將Authorization header中的用戶名密碼取出,進行驗證, 如果驗證通過,將根據請求,發送資源給客戶端。
下面來看一個JAVA的示例代碼
import java.io.IOException; import java.io.PrintWriter; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import sun.misc.BASE64Decoder; public class HTTPAuthServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { if (!BasicAuthenticationUtil.checkUserAuth(request, "basicAuth")) { if (!BasicAuthenticationUtil.checkHeaderAuth(request, "basicAuth")) { response.setStatus(401); response.setHeader("Cache-Control", "no-store"); response.setDateHeader("Expires", 0); response.setHeader("WWW-authenticate", "Basic Realm=\"test\""); return null; } } // 驗證通過后 String client = request.getParameter("client");if (StringUtils.isBlank(client)) { //...... } // 其他操作.... } public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException { doGet(request, response); } }
BasicAuthenticationUtil 幫助類
import java.io.IOException; import java.io.UnsupportedEncodingException; import javax.servlet.http.HttpServletRequest; import org.jfree.util.Log; import sun.misc.*; /** * basic Auth 認證方式 * * @author Geely * */ public class BasicAuthenticationUtil { /** * * @param request * @param response * @param sessionName * @return */ public static boolean checkHeaderAuth(HttpServletRequest request, String sessionName) { String authorization = request.getHeader("Authorization"); if ((authorization != null) && (authorization.length() > 6)) { authorization = authorization.substring(6, authorization.length()); String decodedAuth = base64Decode(authorization); if (StringUtil.isNotBlank(sessionName)) { request.getSession().setAttribute(sessionName, decodedAuth); } return true; } return false; } /** * * @param request * @param response * @param sessionName * @return */ public static boolean checkUserAuth(HttpServletRequest request, String sessionName) { String sessionAuth = null; if (StringUtil.isNotBlank(sessionName)) { sessionAuth = (String) request.getSession().getAttribute(sessionName); } if (sessionAuth != null) { Log.info("this is next step"); return true; } return false; } /** * 編碼 * * @param bstr * @return String */ @SuppressWarnings("restriction") public static String base64Encode(byte[] bstr) { String strEncode = new BASE64Encoder().encode(bstr); return strEncode; } /** * 解碼 * * @param str * @return */ @SuppressWarnings("restriction") public static String base64Decode(String str) { if (StringUtil.isBlank(str)) { return null; } String s = null; try { BASE64Decoder decoder = new BASE64Decoder(); byte[] b = decoder.decodeBuffer(str); s = new String(b, "UTF8"); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block s = null; } catch (IOException e) { // TODO Auto-generated catch block s = null; } return s; } }
當request第一次到達服務器時,服務器沒有認證的信息,服務器會返回一個401 Unauthozied給客戶端。
認證之后將認證信息放在session,以后在session有效期內就不用再認證了。
以上就是HTTP基本認證(Basic Authentication)的JAVA實例代碼全部內容



