2016-04-22 130 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进一步防止异常向下的处理程序链。