【问题标题】:Does Undertow have an equivalent to Tomcat WebAuthentication for authenticating in web tier?Undertow 是否具有与 Tomcat Web 身份验证等效的 Web 层身份验证功能?
【发布时间】:2019-01-24 21:33:27
【问题描述】:

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


更新:在某一时刻,我曾尝试通过执行loginRequest.login(username, password); 来使用HttpServletRequest,但这无法编译,产生关于在HttpServletRequest 中找不到login(String, String) 的编译错误,所以我放弃了这个角度.但是,我刚刚找到了一个article which suggests that this is the correct change to make,所以我又回到了那条路上。

有人告诉我,我可能正在使用旧版本的课程。我的编译时类路径中可能有旧的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 之前的评论,关于使用Web 层进行身份验证的评论,不是我添加的。那是在原始代码中,看起来它解释了为什么选择WebAuthentication 作为身份验证机制。

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

在运行时,当用户提交他们的姓名/密码条目时,会有一个ClassNotFoundException 和消息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 来完成这项工作?

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

【问题讨论】:

    标签: tomcat servlets undertow


    【解决方案1】:

    一般来说,用户的管理被排除在 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 安全性。如果不允许(基于 web.xml 中的 security-constraint 节)甚至不会调用您的方法,并且您不必验证用户是否已登录。

    一开始有点令人生畏,但一旦奏效就非常简单了。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-07-22
      • 1970-01-01
      • 2017-10-22
      • 2018-07-13
      • 2020-10-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多