我不确定是否对该策略有效有个好主意。在我的应用程序中,我有2个单独的登录表单,一个用于普通用户,另一个用于管理员(不要问为什么,那就是:))
我试图达到的目标是正确的重定向。在我的WebApplication类中,我有:
init() {
//...ommited some not related code
CompoundAuthorizationStrategy compoundAuthorizationStrategy = new CompoundAuthorizationStrategy();
IAuthorizationStrategy membertPageAuthStrategy = new SimplePageAuthorizationStrategy( MemberTemplatePage.class,
HomePage.class )
{
@Override
protected boolean isAuthorized()
{
//here's my way to define if authorization is completed
}
};
IAuthorizationStrategy adminPageAuthStrategy = new SimplePageAuthorizationStrategy( AdminTemplatePage.class,
AdminLoginPage.class )
{
@Override
protected boolean isAuthorized()
{
//here's my way to define if authorization is completed
}
};
compoundAuthorizationStrategy.add( membertPageAuthStrategy );
compoundAuthorizationStrategy.add( adminPageAuthStrategy );
getSecuritySettings().setAuthorizationStrategy( compoundAuthorizationStrategy );
//...
}
MemberTemplatePage是用于登录用户页面的模板,而AdminTemplatePage对于admin是相同的。
现在,尽管如此,使用这种链接式身份验证策略,当我输入MemberTemplatePage未经授权时,我将被重定向到HomePage;当我输入AdminTemplatePage未经授权时,我将被重定向到AdminLoginPage,但我仍将重定向到AdminTemplatePage。
无法在CompoundAuthorizationStrategy上搜索任何内容,这似乎是我的最佳选择。知道我在做什么错吗?
我知道RoleAuth〜,但这不适合我。
最佳答案
@roby您可能是正确的,在检票口中进行了一些搜索之后,我在代码中进行了更改:
init() {
//...ommited some not related code
CompoundAuthorizationStrategy compoundAuthorizationStrategy = new CompoundAuthorizationStrategy();
IAuthorizationStrategy membertPageAuthStrategy = new SimplePageAuthorizationStrategy( MemberTemplatePage.class,
HomePage.class )
{
@Override
protected boolean isAuthorized()
{
if ( /* user not authorized */ )
{
throw new RestartResponseAtInterceptPageException( HomePage.class );
}
return true;
}
};
IAuthorizationStrategy adminPageAuthStrategy = new SimplePageAuthorizationStrategy( AdminTemplatePage.class,
AdminLoginPage.class )
{
@Override
protected boolean isAuthorized()
{
if ( /* user not authorized */ )
{
throw new RestartResponseAtInterceptPageException( AdminLoginPage.class );
}
return true;
}
};
compoundAuthorizationStrategy.add( membertPageAuthStrategy );
compoundAuthorizationStrategy.add( adminPageAuthStrategy );
getSecuritySettings().setAuthorizationStrategy( compoundAuthorizationStrategy );
//...
}
它就像我想要的那样工作:)