1

我正在将 Shiro 集成到我的 dropwizard webapp 中。我已经到了这样的地步

  • shiro 识别出需要身份验证的资源,并将我发送到登录页面 (/auth/login)
  • 如果由于密码错误导致登录失败,shiro 会提醒我 (/auth/login?loginFailed=true)
  • 如果我尝试访问受保护的资源(如 /admin/**),shiro 只会重定向
  • shiro 成功后重定向到定义的页面

但是,如果我尝试访问受保护的资源,shiro 总是将我发送到登录页面,无论我是否已登录。我相信登录是有效的;我也相信 shiro 不尊重我是否已经登录。

我的 shiro.ini:

[main]
# some other stuff
authc = org.apache.shiro.web.filter.authc.PassThruAuthenticationFilter
authc.loginUrl = /auth/login

# NOTE: URLs are evaluated in order, and the first matching pattern is accepted.
[urls]
/static/** = noSessionCreation, anon
/auth/** = authc
/admin/** = authc
/** = anon

另请注意:我收到一条错误消息

0:0:0:0:0:0:0:1%0 - - [10/Oct/2013:18:23:54 +0000] "GET /auth/login;JSESSIONID=65e06b39-30e5-45dd-85f9-b2a1c29fc3af HTTP/1.1" 200 739 4 4
WARN  [2013-10-10 18:24:08,485] com.sun.jersey.spi.container.servlet.WebComponent:
A servlet request, to the URI http://blah:8080/auth/login, contains form
parameters in the request body but the request body has been consumed by the servlet
or a servlet filter accessing the request parameters. Only resource methods using
@FormParam will work as expected. Resource methods consuming the request body by other
means will not work as expected.

每次访问 /admin 都会导致错误 302。我不知道还有什么与回答这个问题相关。我已经探索了其他几个“Shiro 不断将我重定向到登录”的问题,但大多数直接引用带有 web.xmls 的 Jetty,Dropwizard 不使用它,我也不知道正在应用哪些过滤器。我的同事使用相同的方法让 shiro 使用他的 dropwizard 项目。

在决定是否将用户发送到登录页面之前,shiro 如何对用户进行身份验证?

4

1 回答 1

1

我发现了问题。这里有更多的 shiro.ini,特别是会话管理部分:

sessionManager = org.apache.shiro.web.session.mgt.DefaultWebSessionManager
# NOTE: The session ID cookie seems to be required in order for authentication to work as intended.
# If the presence of cookies is a deal-breaker, further investigation will be required.
# Here we enable the Secure attribute (serve over SSL only) for this cookie;
# the HttpOnly attribute (not accessible by JavaScript) is enabled by default.
#sessionManager.sessionIdCookie.secure = true
# enabled Ehcache following advice from Shiro docs
sessionDAO = org.apache.shiro.session.mgt.eis.EnterpriseCacheSessionDAO
sessionManager.sessionDAO = $sessionDAO
# can optionally add session listeners here if actions need to be performed on session start/stop/expiration
# sessionManager.sessionListeners = $listener_1, $listener_2, ...
securityManager.sessionManager = $sessionManager

我已经对 sessionIdCookie.secure 进行了评论(稍后将删除它。)事实证明,如果没有启用 ssl,当该变量设置为 true 时,cookie 不会被存储。这解释了 url 栏中的 JSESSIONID 查询参数,但也解释了为什么在验证后它立即忘记了我并决定我需要再次登录。

于 2013-10-10T20:25:12.613 回答