Undertow是否具有与Tomcat WebAuthentication等效的web层身份验证?
这可能是“XY问题”问题的一个例子;如果是的话,请通过更正我的假设来自由回答Undertow是否具有与Tomcat WebAuthentication等效的web层身份验证?,tomcat,servlets,undertow,Tomcat,Servlets,Undertow,这可能是“XY问题”问题的一个例子;如果是的话,请通过更正我的假设来自由回答 更新:有一次,我试图通过loginRequest.login(用户名、密码)来使用HttpServletRequest,但这不会编译,导致编译错误,即在HttpServletRequest中找不到login(String,String),因此我放弃了这个角度。然而,我刚刚找到了一个,所以我又回到了那个轨道上 有人告诉我,也许我使用的是旧版本的类。我的编译时类路径上可能有旧的HttpServletRequest。我现在
更新:有一次,我试图通过
loginRequest.login(用户名、密码)来使用HttpServletRequest
代码>,但这不会编译,导致编译错误,即在HttpServletRequest
中找不到login(String,String)
,因此我放弃了这个角度。然而,我刚刚找到了一个,所以我又回到了那个轨道上
有人告诉我,也许我使用的是旧版本的类。我的编译时类路径上可能有旧的HttpServletRequest
。我现在正在调查此事
在从Tomcat(在JBoss5中)切换到Undertow(在JBoss7中)之后,我们的用户身份验证页面被破坏
我们有一个HttpServlet
,它曾经在JBoss5的TomcatWeb服务器中运行。因此,它使用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(来自服务模块加载器的[module“deployment.myproject.ear.viewers.war”)
现在,我们不再能够访问WebAuthentication
(我甚至将包含必要类的jar文件从Tomcat复制到.ear文件的lib/目录中,但Undertow似乎并不理解这一点,我遇到了不同的错误),我正在寻找替代者
我的理解是,我只需要让用户通过web层进行身份验证,本质上是对WebAuthentication.login
的替代。WebAuthentication
的唯一用途是上面代码示例中提到的两行
我可以用什么来取代WebAuthentication
,以实现此功能?
显然,一个简单的类替换是非常棒的,但是如果它必须比这更复杂,那么只要我们能让它工作,就可以了。一般来说,用户的管理被排除在JEE规范之外,所以每个供应商都以不同的方式进行。看看JBoss/Wildfly中内置的文档。如果这还不够(我不喜欢它使用的表格布局),另一种方法是编写自己的:
...
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();
}
}
。。。
导入org.jboss.security.SimpleGroup;
导入org.jboss.security.SimplePrincipal;
导入org.jboss.security.auth.spi.UsernamePasswordLoginModule;
...
/**
*扩展特定于WildFly的类以实现自定义登录模块。
*
*请注意,该类在中的standalone.xml中引用
*安全域/身份验证/登录模块部分。如果是包或名称
*然后需要更新的更改。
*
*/
公共类MyLoginModule扩展了UsernamePasswordLoginModule{
私家侦探;
@凌驾
public void initialize(主题、CallbackHandler、CallbackHandler、,
地图共享状态,地图选项){
初始化(主题、回调处理程序、共享状态、选项);
}
/**
*虽然我们必须重写此方法,但是validatePassword方法忽略了
*价值。
*/
@凌驾
受保护的字符串getUsersPassword()引发LoginException{
返回null;
}
/**
*验证为给定用户名传入的密码(inputPassword)。
*/
@凌驾
受保护的布尔validatePassword(字符串inputPassword、字符串expectedPassword){
//像今天一样验证-忽略“expectedPassword”
}
/**
*获取绑定到用户的角色。
*/
@凌驾
受保护组[]getRoleSets()引发LoginException{
SimpleGroup=新的SimpleGroup(“角色”);
List userRoles=//以当前的方式获取用户的角色
for(字符串nextRoleName:userRoles){
addMember(新的SimplePrincipal(nextRoleName));
}
返回新组[]{Group};
}
/**
*这个方法最终会触发其他方法。
*/
@凌驾
public boolean login()引发LoginException{
布尔登录;
login=super.login();
如果(登录){
主体=//填充主体
返回登录;
}
@凌驾
受保护的主体getIdentity(){
返回主体!=null?主体:super.getIdentity();
}
}
这使您更接近标准的JEE安全性。如果不允许,您的方法甚至不会被调用(基于web.xml
中的security constraint
节),并且您不必验证用户是否登录
一开始有点让人畏缩,但一旦成功就相当直截了当了
话虽如此,我已经删除了这样的代码并转移到了。这是一个独立的授权引擎,也与Tomcat集成。它有大量的功能,如社交登录等