【问题标题】:Expected response in Apache Shiro when auth fails?身份验证失败时 Apache Shiro 中的预期响应?
【发布时间】:2014-08-11 09:50:24
【问题描述】:

我已经实现了不记名令牌身份验证(在每个请求中使用客户端 ID 和标头中的访问令牌进行身份验证)。

当我使用错误的凭据 (Access-Token) 时,我得到一个空正文的“200 OK”,这是预期的吗?不应该是401或404吗?当我使用正确的凭据时,我会返回“200 OK”预期的 Json 响应以及正文内容。

我正在使用 DefaultPasswordService 和 AuthorizingRealm。也许我错过了什么?

使用 Shiro 1.2.3

【问题讨论】:

    标签: authentication shiro bearer-token


    【解决方案1】:

    我想我解决了这个问题。

    在我的 onAccessDenied() 看起来像这样之前:

    @Override 
    protected boolean onAccessDenied(ServletRequest request, ServletResponse response) 
            throws Exception {       
        if (hasAuthorizationToken(request)) { 
            // Proceed with authentication 
            return executeLogin(request, response);           
        } 
        // Return 401 if authentication failed 
              WebUtils.toHttp(response).sendError( 
                    Status.UNAUTHORIZED.getStatusCode(), 
                    "Oops, Authentication required"); 
        return false; 
    } 
    

    现在看起来像这样:

    @Override  
    protected boolean onAccessDenied(ServletRequest request, ServletResponse response) 
                throws Exception { 
            boolean authenticated = false; 
            if (hasAuthorizationToken(request)) { 
                // Proceed with authentication 
                authenticated = executeLogin(request, response);           
            } 
            // Return 401 if authentication failed 
            if (!authenticated) 
                WebUtils.toHttp(response).sendError( 
                        Status.UNAUTHORIZED.getStatusCode(), 
                        "Oops, Authentication required"); 
            return authenticated;  
    }
    

    身份验证失败时,我需要手动返回 Status.UNAUTHORIZED。

    【讨论】:

      猜你喜欢
      • 2015-05-17
      • 2017-06-28
      • 2014-12-20
      • 2016-04-02
      • 2014-12-06
      • 2016-10-29
      • 2013-11-08
      • 2013-03-01
      • 2014-08-17
      相关资源
      最近更新 更多