有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

多种java servlet身份验证方法

一个java servlet可以有多种身份验证方法吗?例如,除了基于open id的身份验证之外,还有基于表单的身份验证,这样用户就可以选择如何登录


共 (2) 个答案

  1. # 1 楼答案

    是的,这是可能的,但它的实现往往有点棘手

    例如,开箱即用的SpringSecurity支持本地身份验证、OpenId、X509和其他方案,但将它们结合起来,以便用户有其他登录方式,需要自定义类和自定义连接

  2. # 2 楼答案

    但是,我建议使用servlet过滤器,而不是servlet本身

    http://brendangraetz.wordpress.com/2010/06/17/use-servlet-filters-for-user-authentication/

    按照那篇文章中的步骤,重写isAuth()方法,这样它就可以按照您希望的多种模式执行身份验证。在(非常粗糙、未经测试的)代码中:

    @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;    
    }
    

    当然,我假设您已经知道如何在每个模式中分别实现身份验证步骤

    “诀窍”是在HTTP会话中,在用户执行登录身份验证后立即在HTTP会话中存储一个值。根据这个值,过滤器将知道在加载servlet之前应该检查或查询什么