zoukankan      html  css  js  c++  java
  • HTTP基本认证(Basic Authentication)的JAVA示例

    大家在登录网站的时候,大部分时候是通过一个表单提交登录信息。
    但是有时候浏览器会弹出一个登录验证的对话框,如下图,这就是使用HTTP基本认证。

    下面来看看一看这个认证的工作过程:
    第一步:  客户端发送http request 给服务器,服务器验证该用户是否已经登录验证过了,如果没有的话,
    服务器会返回一个401 Unauthozied给客户端,并且在Response 的 header "WWW-Authenticate" 中添加信息。
    如下图。

    第二步:浏览器在接受到401 Unauthozied后,会弹出登录验证的对话框。用户输入用户名和密码后,
    浏览器用BASE64编码后,放在Authorization header中发送给服务器。如下图:

    第三步: 服务器将Authorization header中的用户名密码取出,进行验证, 如果验证通过,将根据请求,发送资源给客户端。

    下面来看一个JAVA的示例代码:

     1 import java.io.IOException;
     2 import java.io.PrintWriter;
     3 import javax.servlet.http.HttpServlet;
     4 import javax.servlet.http.HttpServletRequest;
     5 import javax.servlet.http.HttpServletResponse;
     6 import sun.misc.BASE64Decoder;
     7 
     8 public class HTTPAuthServlet extends HttpServlet {
     9     
    10     public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
    11         String sessionAuth = (String) request.getSession().getAttribute("auth");
    12 
    13         if (sessionAuth != null) {
    14             System.out.println("this is next step");
    15             nextStep(request, response);
    16 
    17         } else {
    18 
    19             if(!checkHeaderAuth(request, response)){
    20                 response.setStatus(401);
    21                 response.setHeader("Cache-Control", "no-store");
    22                 response.setDateHeader("Expires", 0);
    23                 response.setHeader("WWW-authenticate", "Basic Realm="test"");
    24             }            
    25 
    26         }
    27 
    28     }
    29 
    30     private boolean checkHeaderAuth(HttpServletRequest request, HttpServletResponse response) throws IOException {
    31 
    32         String auth = request.getHeader("Authorization");
    33         System.out.println("auth encoded in base64 is " + getFromBASE64(auth));
    34         
    35         if ((auth != null) && (auth.length() > 6)) {
    36             auth = auth.substring(6, auth.length());
    37 
    38             String decodedAuth = getFromBASE64(auth);
    39             System.out.println("auth decoded from base64 is " + decodedAuth);
    40 
    41             request.getSession().setAttribute("auth", decodedAuth);
    42             return true;
    43         }else{
    44             return false;
    45         }
    46 
    47     }
    48 
    49     private String getFromBASE64(String s) {
    50         if (s == null)
    51             return null;
    52         BASE64Decoder decoder = new BASE64Decoder();
    53         try {
    54             byte[] b = decoder.decodeBuffer(s);
    55             return new String(b);
    56         } catch (Exception e) {
    57             return null;
    58         }
    59     }
    60 
    61     public void nextStep(HttpServletRequest request, HttpServletResponse response) throws IOException {
    62         PrintWriter pw = response.getWriter();
    63         pw.println("<html> next step, authentication is : " + request.getSession().getAttribute("auth") + "<br>");
    64         pw.println("<br></html>");
    65     }
    66 
    67     public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
    68         doGet(request, response);
    69     }
    70 
    71 }
     

    当request第一次到达服务器时,服务器没有认证的信息,服务器会返回一个401 Unauthozied给客户端。
    认证之后将认证信息放在session,以后在session有效期内就不用再认证了。

  • 相关阅读:
    nyoj--767--因子和(模拟)
    poj--1703--Find them, Catch them(并查集巧用)
    nyoj--1009--So Easy[Ⅰ](数学)
    nyoj--1011--So Easy[II](数学几何水题)
    nyoj--311--完全背包(动态规划,完全背包)
    morhpia(4)-更新
    morphia(5)-删除
    morphia(6-1)-查询
    redis 分页
    mongodb-安装&配置&启动
  • 原文地址:https://www.cnblogs.com/azhqiang/p/5944697.html
Copyright © 2011-2022 走看看