我有一个使用 Spring MVC 的 java webapp。 Web 应用程序运行在 JBoss AS7.1 服务器上,该服务器使用具有表单例份验证的 JAAS 登录模块。当用户在表单上填写用户名和密码时,登录会很顺利。
我现在想创建一个“登录用户”的 java Controller ,就像用户填写登录表单一样。
public void logInProgrammatically(String username, string password)
{
//???
}
当上述方法完成时,应允许对任何安全页面的任何访问,因为用户被视为已登录。
这可以通过访问 Jboss 的 loginModule 实现并设置一些属性来编程吗?
最佳答案
您可以使用 JAAS 标准类以编程方式实现身份验证。假设我们正在使用我们的自定义 LoginModule 实现(或任何标准实现)com.sample.CustomLoginModule
。这个登录模块是在 jboss 配置 XML 中配置的。
第 1 步:在 JBoss 中定义一个安全域。对于其他服务器,可以在JAAS config中配置相同的信息。
<security-domain name="customlogin" cache-type="default">
<authentication>
<login-module code="com.sample.CustomLoginModule" flag="required">
</login-module>
</authentication>
</security-domain>
第 2 步:使用自定义登录模块以编程方式登录用户。
public void logInProgrammatically(String username, string password){
CallbackHandler handler = //use proper implementation to capture username and password arguments.
LoginContext ctx = new LoginContext("customlogin", handler);
ctx.login();
使用此解决方案,您不会绑定(bind)到特定的应用程序服务器 API。此代码可移植到任何具有 JAAS 配置更改的应用程序服务器。
要使用 JAAS 以编程方式进行身份验证,请参阅 Oracle 的教程: http://docs.oracle.com/javase/7/docs/technotes/guides/security/jaas/tutorials/GeneralAcnOnly.html
关于java - JAAS 自动登录而不显示登录页面/表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34271679/