2016-04-22 163 views
0

我使用的是嵌入式的jetty 9。挑戰登錄,基本身份驗證

我想在我的屬性文件中不存在用戶名時發送質詢。

我擴展了BasicAuthenticator類來捕獲當用戶名不存在時通常拋出的異常。在用戶再次登錄後,我無法看到如何操作。

public class MyBasicAuthenticator extends BasicAuthenticator{ 
    @Override 
    public Authentication validateRequest(ServletRequest req, 
     ServletResponse res, boolean mandatory) 
    { 
     Authentication authentication = null; 
     try{ 
      authentication = super.validateRequest(req,res,mandatory); 
      return authentication; 
     }catch (Exception e){ 
      return Authentication.SEND_FAILURE; 
     } 
    } 

Authentication.SEND_FAILURE防止ErrorHandler被調用,但它只是加載一個空白頁。 SEND_FAILURE來自認證界面。

的JavaDoc:http://download.eclipse.org/jetty/stable-9/apidocs/org/eclipse/jetty/security/authentication/BasicAuthenticator.html

按照這個問題的答案Jetty6有一個發送挑戰方法,Jetty9沒有。 Jetty UserRealm redirect on 3th failed login

回答

0

這是一個基本身份驗證問題。

try { 
     authentication = super.validateRequest(req, res, mandatory); 
    } catch (Exception e) { 
     try{ 
      HttpServletResponse r = (HttpServletResponse) res; 
      r.setHeader("WWW-Authenticate", "Basic"); 
      r.setStatus(401); 
      } catch(Exception ew){} 
     authentication = Authentication.SEND_FAILURE; 
    } 

返回401和HTTP報頭,其包括「WWW驗證」:「基本」

Authentication.SEND_FAILURE進一步防止異常向下的處理程序鏈。