【发布时间】:2014-02-19 09:03:33
【问题描述】:
嗯,我在 Shiro 的论坛上发布了以下问题 1 周,但直到现在没有回复。
我只是想在一个简单的 Spring 应用程序中使用 Shiro。安全管理器定义为DefaultWebSecurityManager,默认情况下将会话管理器设置为ServletContainerSessionManager,它适用于我。但是,在我将会话管理器更改为 DefaultWebSessionManager 这意味着我想使用 Shiro 的本机会话后,它在 Chrome 上不起作用。我挖出了以下一些信息:-
- 当使用
DefaultWebSessionManager时,Shiro 会尝试从请求 cookie 中获取会话 ID。逻辑SimpleCookie循环所有请求 cookie 并在 cookie 的名称为“JSESSIONID”时返回。但是,在来自 Chrome 的请求中,有 2 个名为“JSESSIONID”的 cookie。第一个的值与request中的'requestedSessionid'不同,另一个等于。这会在身份验证成功后导致新的重定向到登录页面。为了简单起见,现在的过程是:访问任何地址 -> Shiro 重定向到登录页面 -> 提交主体和凭据 -> 身份验证成功并通过 Shiro 重定向到主页 -> 一个新请求出现并找到另一个会话 id未缓存在 Shiro 中,导致resolveSession中的InvalidSessionException方法DefaultSecurityManager-> 重定向回登录 页。 - 使用
ServletContainerSessionManager时,由于session id来自request.getSession(false),重定向后可以找到session。
我的问题是这是一个缺陷还是任何配置或代码可以解决这个问题?请注意,只有 Chrome 会触发此问题。我想这是关于 Chrome 的缓存,但你能解释更多吗?
以下是您复制的必要来源:-
web.xml
<listener>
<listener-class>test.shiro.framework.WebSessionListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-servlet.xml</param-value>
</context-param>
<filter>
<filter-name>shiroFilter</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
<init-param>
<param-name>targetFilterLifecycle</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>shiroFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/controller/*</url-pattern>
</servlet-mapping>
spring-servlet.xml:
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<property name="securityManager" ref="securityManager" />
<property name="loginUrl" value="/login.jsp"/>
<property name="successUrl" value="/home.jsp"/>
<property name="unauthorizedUrl" value="/unauthorized.jsp"/>
<property name="filterChainDefinitions">
<value>
/** = authc
</value>
</property>
</bean>
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<property name="realm" ref="myRealm" />
<property name="sessionManager">
<bean class="org.apache.shiro.web.session.mgt.DefaultWebSessionManager"></bean>
</property>
</bean>
<bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor" />
<bean id="myRealm" class="org.apache.shiro.realm.text.TextConfigurationRealm">
<property name="userDefinitions">
<value>
huzj=12345678,authc
guodg=12345678,operator
sadd=12345678,guest
</value>
</property>
<property name="roleDefinitions">
<value>
authc=*
operator=book:*
guest=book:view:*
</value>
</property>
</bean>
2 月 18 日更新:
进一步测试表明,仅当我尝试从ServletContainerSessionManager 更改为DefaultWebSessionManager 时才出现问题。我猜chrome在这种情况下错误地记录了一个垃圾cookie。
毕竟我尝试了@paulochf 的解决方案,它确实有效。我再次调试,相信你明白了。非常感谢!
【问题讨论】:
标签: google-chrome shiro