首页 > 解决方案 > Undertow 是否具有与 Tomcat WebAuthentication 等效的 Web 层身份验证功能?

问题描述

这可能是“XY 问题”问题的一个实例;如果是,请随时纠正我的假设来回答。


更新:在某一时刻,我曾尝试使用HttpServletRequestby doing loginRequest.login(username, password);,但这不会编译,产生关于login(String, String)not being found in 的编译错误HttpServletRequest,所以我放弃了这个角度。但是,我刚刚发现一篇文章表明这​​是要进行的正确更改,所以我又回到了这条路上。

有人告诉我,也许我使用的是旧版本的课程。HttpServletRequest我的编译时类路径中可能有旧的。我现在正在调查。


从 Tomcat(在 JBoss 5 中)切换到 Undertow(在 JBoss 7 中)后,我们的用户身份验证页面被破坏了。

我们有一个HttpServlet曾经在 JBoss 5 的 Tomcat Web 服务器中运行的。因此,它使用 Tomcat,WebAuthentication如下所示:

import org.jboss.web.tomcat.security.login.WebAuthentication;
import java.io.IOException;
import javax.servlet.http.*;
// ... import etc.

public class MyHttpServlet extends HttpServlet
{
    private void loginCase( HttpServletRequest loginRequest,
        HttpServletResponse loginResponse ) throws ServletException, IOException, RemoteException
    {
        String username;
        String password;

        // ... other stuff

        // Authenticate with the web tier. This invokes the MyLoginModule,
        // which uses the user database tables to authenticate and establish
        // the user's role(s). This servlet does role checking, but this step
        // is necessary to get the user's credentials into the container, which
        // secures remote EJBs. If this is not done, the servlets and JSPs will
        // not be able to properly access secured EJBs.
        WebAuthentication webAuth = new WebAuthentication();
        boolean loginResult = webAuth.login(username, password);

        // ...
    }
}

在使用 之前的评论WebAuthentication,关于使用网络层进行身份验证的评论,不是我添加的。那是在原始代码中,看起来它是关于为什么WebAuthentication选择作为身份验证机制的解释。

我的理解是 Undertow/JBoss7 不再有WebAuthentication可用的类。

在运行时,当用户提交他们的姓名/密码条目时,会有一条ClassNotFoundExceptionwith 消息org.jboss.web.tomcat.security.login.WebAuthentication from [module "deployment.myproject.ear.viewers.war" from Service Module Loader]

现在我们不再可以访问WebAuthentication(我什至将包含必要类的 Tomcat 中的 jar 文件复制到我的 .ear 文件的 lib/ 目录中,但 Undertow 似乎没有意识到这一点,我得到了不同的错误),我正在寻找一个替代品。

我的理解是,我只需要让用户通过 Web 层进行身份验证,本质上是WebAuthentication.login. 的唯一用途WebAuthentication是上面代码示例中提到的两行。

我可以用什么代替WebAuthentication来完成这项工作?

显然,一个简单的类替换会很棒,但如果它必须比这更复杂,那么只要我们能够让它工作就可以了。

标签: tomcatservletsundertow

解决方案


一般来说,用户的管理被排除在 JEE 规范之外,因此每个供应商都采用不同的方式。查看JBoss/Wildfly 中内置的DatabaseServerLoginModule的文档。如果这还不够(而且我不喜欢它使用的表格布局),另一种方法是编写自己的:

...

import org.jboss.security.SimpleGroup;
import org.jboss.security.SimplePrincipal;
import org.jboss.security.auth.spi.UsernamePasswordLoginModule;

...

/**
 * Extends a WildFly specific class to implement a custom login module.
 * 
 * Note that this class is referenced in the standalone.xml in a 
 * security-domain/authentication/login-module section.  If the package or name
 * changes then that needs to be updated too.
 * 
 */
public class MyLoginModule extends UsernamePasswordLoginModule {
    private MyPrincipal principal;

    @Override
    public void initialize(Subject subject, CallbackHandler callbackHandler,
            Map<String, ?> sharedState, Map<String, ?> options) {

        super.initialize(subject, callbackHandler, sharedState, options);
    }

    /**
     * While we have to override this method the validatePassword method ignores
     * the value.
     */
    @Override
    protected String getUsersPassword() throws LoginException {
        return null;
    }

    /**
     * Validates the password passed in (inputPassword) for the given username.
     */
    @Override
    protected boolean validatePassword(String inputPassword, String expectedPassword) {
        // validate as you do today - IGNORE "expectedPassword"
    }

    /**
     * Get the roles that are tied to the user.
     */
    @Override
    protected Group[] getRoleSets() throws LoginException {
        SimpleGroup group = new SimpleGroup("Roles");

        List<String> userRoles = // get roles for a user the way you do currently

        for( String nextRoleName: userRoles ) {
            group.addMember(new SimplePrincipal(nextRoleName));
        }

        return new Group[] { group };
    }

    /**
     * This method is what ends up triggering the other methods.
     */
    @Override
    public boolean login() throws LoginException {
        boolean login;

        login = super.login();
        if (login) {
            principal = // populate the principal

        return login;
    }

    @Override
    protected Principal getIdentity() {
        return principal != null ? principal : super.getIdentity();
    }
}

这使您更接近标准 JEE 安全性。security-constraint如果不允许(基于your 中的节)甚至不会调用您的方法,web.xml并且您不必验证用户是否已登录。

起初它有点令人生畏,但一旦它起作用,它就会非常简单。

说了这么多,我已经删除了这样的代码并转移到Keycloak。这是一个单独的授权引擎,也与 Tomcat 集成。它具有大量功能,例如社交登录等。


推荐阅读