这可能是“XY 问题”问题的一个实例;如果是,请随时通过更正我的假设来回答。
更新:有一次,我曾尝试HttpServletRequest
通过执行来使用loginRequest.login(username, password);
,但这不会编译,产生关于login(String, String)
未在 中找到的编译错误HttpServletRequest
,所以我放弃了那个角度。但是,我刚刚发现一篇文章表明这是正确的更改,所以我又回到了这条路上。
有人告诉我,也许我使用的是旧版本的课程。HttpServletRequest
我的编译时类路径上可能有旧的。我现在正在研究这个。
从 Tomcat(在 JBoss 5 中)切换到 Undertow(在 JBoss 7 中)后,我们的用户身份验证页面被破坏了。
我们有一个HttpServlet
曾经在 JBoss 5 中的 Tomcat Web 服务器中运行的。因此,它WebAuthentication
像这样使用 Tomcat :
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
来完成这项工作?
显然,一个简单的类替换会很棒,但如果它必须比这更复杂,那么只要我们能让它工作就行了。
通常,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
您的 中的节web.xml
),您的方法甚至不会被调用,并且您不必验证用户是否已登录。
一开始有点令人生畏,但一旦它起作用就非常简单了。
说了这么多,我已经删除了这样的代码并转移到Keycloak。这是一个单独的授权引擎,也与 Tomcat 集成。它具有大量功能,例如社交登录等。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句