我正在测试 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 );
}
}
关于java - Apache shiro 和 JSESSIONID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25976314/