【问题标题】:Apache shiro and JSESSIONIDApache shiro 和 JSESSIONID
【发布时间】:2014-11-16 13:13:29
【问题描述】:

我正在测试 apache shiro,刚刚构建了位于 https://github.com/pires/simple-shiro-web-app 的简单演示

这只是 shiro 的一个简单用法,它尝试使用 jdbcrealm 进行身份验证。一切都很好,除了

  • Shiro 成功后不会更改 SESSIONID 验证。这意味着 SESSIONID 是相同的,当用户 到达登录页面,并且一旦用户通过身份验证。

  • 还注意到如果我在成功验证后关闭
    浏览器,下次我打开并导航到该页面时,我需要登录 又进来了。

这是shiro的惯常行为吗。如果是,为什么?

【问题讨论】:

    标签: java apache shiro


    【解决方案1】:

    如前所述,shiro 不会在用户登录时生成新 ID。不过,您可以自己轻松实现:

    @Override
    protected boolean executeLogin( final ServletRequest request, final ServletResponse response )
            throws Exception
    {
        final AuthenticationToken token = createToken( request, response );
        if ( token == null )
        {
            throw new IllegalStateException( "Your error message here" );
        }
        try
        {
            // Stop session fixation issues.
            // https://issues.apache.org/jira/browse/SHIRO-170
            final Subject subject = getSubject( request, response );
            Session session = subject.getSession();
            // Store the attributes so we can copy them to the new session after auth.
            final LinkedHashMap<Object, Object> attributes = new LinkedHashMap<Object, Object>();
            final Collection<Object> keys = session.getAttributeKeys();
            for ( Object key : keys )
            {
                final Object value = session.getAttribute( key );
                if ( value != null )
                {
                    attributes.put( key, value );
                }
            }
            session.stop();
            subject.login( token );
            // Restore the attributes. 
            session = subject.getSession();
            for ( final Object key : attributes.keySet() )
            {
                session.setAttribute( key, attributes.get( key ) );
            }
            return onLoginSuccess( token, subject, request, response );
        }
        catch ( AuthenticationException e )
        {
            return onLoginFailure( token, e, request, response );
        }
    }
    

    Reference

    【讨论】:

      【解决方案2】:

      AFAIK shiro 在登录时不会更改会话 ID。我确实在我的项目中使用了它,但没有看到它确实改变了会话 ID 的迹象。我认为这只是他们不支持的功能。当我切换到 spring 时,我遇到了各种各样的问题,因为 spring 确实支持这种行为。

      至于您的第二点,我认为这与会话的寿命有关。该会话可能配置为仅持续到浏览器关闭。如果 Shiro 使用 url 重写来维护会话,它必须请求另一个登录,因为 url 上的会话 id 后缀将丢失。使用 cookie 可以支持这种行为,尽管执行此操作的典型方法是使用记住我的 cookie。

      【讨论】:

      • 嗯,我很好奇 shiro 中是否有一个选项来保留一个 cookie 来识别用户会话,直到它在服务器端超时。在这种情况下,需要更改身份验证时的 sessionid 以避免任何会话固定攻击。作为旁注,shiro 中的记住我功能并没有给出我所期望的。
      • 它已被请求 ;) issues.apache.org/jira/browse/SHIRO-170 迁移到 Spring Security 后,我确实发现 Spring Security 比 shiro 具有一些额外的安全功能。
      猜你喜欢
      • 2014-09-18
      • 2013-03-13
      • 2015-09-18
      • 2014-10-31
      • 2013-03-19
      • 2012-03-08
      • 2014-04-07
      • 2017-09-20
      • 2016-05-07
      相关资源
      最近更新 更多