好吧,我已经在 Shiro 的论坛上发布了以下问题 1 周,但直到现在都没有回应。

我只是想在一个简单的 Spring 应用程序中使用 Shiro。安全管理器定义为 DefaultWebSecurityManager,默认情况下将 session 管理器设置为 ServletContainerSessionManager,它对我有用。但是,在我将 session 管理器更改为 DefaultWebSessionManager 这意味着我想使用 Shiro 的 native session 后,它在 Chrome 上不起作用。我挖出一些以下信息:-

  • 使用 DefaultWebSessionManager 时,Shiro 尝试从请求 cookie 中获取 session ID。逻辑 SimpleCookie 循环所有请求 cookie 并在 cookie 的名称为“JSESSIONID”时返回。但是,在来自 Chrome 的请求中,有 2 个名为“JSESSIONID”的 cookie。第一个的值与 request 中的 'requestedSessionid' 不同,另一个相等。这会导致在身份验证成功后重新重定向到登录页面。为了简单起见,现在的流程是:访问任何地址 -> Shiro 重定向到登录页面 -> 提交主体和凭据 -> 身份验证成功并由 Shiro 重定向到主页 -> 一个新请求出现并找到另一个 session ID未缓存在 Shiro 中,导致 InvalidSessionException 中的 resolveSession 方法中的 DefaultSecurityManager -> 重定向回登录
    页。
  • 使用 ServletContainerSessionManager 时,由于 session id 来自 request.getSession(false),重定向后可以找到 session。

  • 我的问题是这是缺陷还是任何配置或代码可以解决此问题?请注意,只有 Chrome 会触发此问题。我想这是关于 Chrome 的缓存,但你能解释更多吗?

    以下是您复制的必要来源:-

    网页.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 的解决方案,它确实有效。我再次调试,相信你明白了。非常感谢!

    最佳答案

    在我的工作中,我们遇到了同样的问题,但我们解决了它。

    使用谷歌浏览器时,应用程序无法保持 session ,因为它的 ID 几乎在每次发出请求时都会更改。同时,Firefox 没有它。打开 DefaultWebSessionManager 的人使用了网络上的一个示例,它将 cookie 名称设置为“cookie”。

    他解决了将cookie的名称更改为另一个,例如,

    cookie = org.apache.shiro.web.servlet.SimpleCookie
    cookie.name = your.cookie
    cookie.path = /
    sessionManager.sessionIdCookie = $cookie
    

    我希望这对你有用。

    关于google-chrome - DefaultWebSessionManager 不适用于 Chrome,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21399256/

    10-12 00:19
    查看更多