java - Apache shiro 和 JSESSIONID

标签 java apache shiro

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

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

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

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

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

最佳答案

如前所述,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

关于java - Apache shiro 和 JSESSIONID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25976314/

相关文章:

java - 在不退出 google.com 的情况下退出 Appengine 应用程序

java - 如何使用 Apache Shiro 检查用户是否已经登录?

apache - 反向代理模式Apache拦截或捕获来自后端服务器的302响应,并在内部重定向,而无需将302响应发送回客户端

KeepAlive 关闭时的 Apache MaxClients

linux - 设置 Apache

java - 在shiro中,哪个更好地为用户分配多个权限或多个角色?

java - Shiro 匹配凭证匹配器和用户密码创建不匹配

java - 删除可绘制图标时出错

java - 使用 resttemplate 发布请求但有 401 未经授权

Java-如何通过 GUI 获取数字数组