我正在尝试编写自己的以CustomLoginModule
Wildfly 8.0.0.CR1 命名的 LoginModule,它在以下位置注册了一个安全域standalone.xml
:
<security-domain name="other" cache-type="default">
<authentication>
<login-module code="Remoting" flag="optional">
<module-option name="password-stacking" value="useFirstPass"/>
<login-module>
<login-module code="com.someExample.CustomLoginModule" flag="required">
<module-option name="password-stacking" value="useFirstPass"/>
</login-module>
</authentication>
</security-domain>
在我的远程客户端中,我使用以下 jboss-ejb-client.properties:
endpoint.name=client-endpoint
remote.connectionprovider.create.options.org.xnio.Options.SSL_ENABLED=false
remote.connections=default
remote.connection.default.host=[...]
remote.connection.default.port=[...]
remote.connection.default.connect.options.org.xnio.Options.SASL_POLICY_NOANONYMOUS=false
remote.connection.default.username=myUserName
remote.connection.default.password=abcde
在客户端获取 InitialContext 如下所示:
Properties props = new Properties();
props.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
InitialContext ctx = new InitialContext(props);
基本上这工作正常,并且在通过带有正确注释的远程接口访问 EJB 时调用我的登录模块@SecurityDomain
。
在登录模块中,我可以使用回调或sharedState
传递给initialize
方法的方法读取用户名。但我无法获得提供的密码(在这个例子中,我希望得到字符串的位置abcde
)。
我已经尝试了几件事来获取密码。通过回调(就像我在 JBoss 5 上所做的那样),通过给定的 sharedState,... 即使我从org.jboss.security.auth.spi.UsernamePasswordLoginModule
或使用 JBoss-Quickstart 示例中的代码派生,我也看不到在客户端设置的密码。相反,我总是将以下字符串作为密码返回org.jboss.as.security.remoting.RemotingConnectionCredential@...
。
编辑:这是我的LoginModule
(或至少一个版本)的代码:
import javax.security.auth.callback.*;
import javax.security.auth.login.LoginException;
import javax.security.auth.spi.LoginModule;
public class CustomLoginModule implements LoginModule
{
private CallbackHandler callbackHandler;
public void initialize(Subject subject, CallbackHandler callbackHandler,
Map<String, ?> sharedState, Map<String, ?> options)
{
this.callbackHandler = callbackHandler;
}
public boolean login() throws LoginException
{
NameCallback namecallback = new NameCallback("Username");
PasswordCallback passwordcallback = new PasswordCallback("Password", false);
CallbackHandler handler = this.callbackHandler;
try {
handler.handle(new Callback[] { namecallback, passwordcallback });
}
catch (Exception e) {
e.printStackTrace();
}
String username = namecallback.getName();
char[] password2 = passwordcallback.getPassword();
String password = new String(password2);
System.out.println(username + " / " + password);
if (username == null || password == null) {
return false;
}
// do authentication...
return true;
}
public boolean commit() throws LoginException
{ ... }
public boolean abort() throws LoginException
{ ... }
public boolean logout() throws LoginException
{ ... }
}