@Override protected boolean isAuth()
{
String authMode = (String)(getSession(true).getAttribute("authMode"));
if (authMode == null) { return false; }
if (authMode.equals("open id") {
//do open id authentication steps here
//return true if authentication passes
}
else if (authMode.equals("some other authentication") {
//do some other authentication steps here
//return true if authentication passes
}
...
return false;
}
# 1 楼答案
是的,这是可能的,但它的实现往往有点棘手
例如,开箱即用的SpringSecurity支持本地身份验证、OpenId、X509和其他方案,但将它们结合起来,以便用户有其他登录方式,需要自定义类和自定义连接
# 2 楼答案
对
但是,我建议使用servlet过滤器,而不是servlet本身
http://brendangraetz.wordpress.com/2010/06/17/use-servlet-filters-for-user-authentication/
按照那篇文章中的步骤,重写
isAuth()
方法,这样它就可以按照您希望的多种模式执行身份验证。在(非常粗糙、未经测试的)代码中:当然,我假设您已经知道如何在每个模式中分别实现身份验证步骤
“诀窍”是在HTTP会话中,在用户执行登录身份验证后立即在HTTP会话中存储一个值。根据这个值,过滤器将知道在加载servlet之前应该检查或查询什么