Java Wildfly和JAAS登录模块
我在玩Wildfly-9.0.1.Final和JAAS,但我没有那么多乐趣。。我实现了我的自定义登录模块:Java Wildfly和JAAS登录模块,java,wildfly,jaas,Java,Wildfly,Jaas,我在玩Wildfly-9.0.1.Final和JAAS,但我没有那么多乐趣。。我实现了我的自定义登录模块: public class MongoLoginModule implements LoginModule { @Inject protected MongoDB mongoDb; protected Subject subject; protected Principal identity; protected boolean loginOk; private CallbackHand
public class MongoLoginModule implements LoginModule {
@Inject
protected MongoDB mongoDb;
protected Subject subject;
protected Principal identity;
protected boolean loginOk;
private CallbackHandler callbackHandler;
private Map sharedState;
private Map options;
private Logger log = LoggerFactory.getLogger(MongoLoginModule.class);
public boolean abort() throws LoginException {
log.info("abort!");
subject = null;
return true;
}
public boolean commit() throws LoginException {
// TODO Auto-generated method stub
log.info("commit!");
if(loginOk) {
UserGroup userGroup = new UserGroup("Roles");
userGroup.addMember(new RolePrincipal("userA"));
subject.getPrincipals().add(userGroup);
subject.getPublicCredentials().add(userGroup);
return true;
}
return false;
}
public void initialize(Subject subject, CallbackHandler callbackHandler,
Map<String, ?> sharedState, Map<String, ?> options) {
log.info("Initializing MongoLoginModule.");
this.subject = subject;
this.callbackHandler = callbackHandler;
this.sharedState = sharedState;
this.options = options;
}
public boolean login() throws LoginException {
log.info("login requested.");
NameCallback nameCallback = new NameCallback("username:");
PasswordCallback passwordCallback = new PasswordCallback("password:", false);
try {
callbackHandler.handle(new Callback[]{nameCallback, passwordCallback});
String username = nameCallback.getName();
String password = new String(passwordCallback.getPassword());
log.info("check credentials for: "+username);
if(username.equals("jim") && password.equals("jim")) {
loginOk = true;
identity = new UserPrincipal(username);
subject.getPrincipals().add(identity);
subject.getPublicCredentials().add(identity);
return true;
}
} catch (IOException e) {
e.printStackTrace();
} catch (UnsupportedCallbackException e) {
e.printStackTrace();
}
return false;
}
public boolean logout() throws LoginException {
if(subject != null && identity != null) {
subject.getPrincipals().remove(identity);
return true;
}
return false;
}
public Document getUserByName(String userName) {
FindIterable<Document> results = mongoDb.getCollection().find(new Document("username", userName));
return results.iterator().next();
}
public void getRoles() {
// FindIterable<Document> results = mongoDb.getCollection().find(new Document("username", userName));
// results.iterator().next().get
}
我的登录模块从未执行过。我错过了什么
更新:
将login module.jar移动到它工作的web应用程序中。也许我的野蝇有点不对劲。以下是我的模块结构:
%wildfly_home%/modules/system/layers/base/login/mongodb/main/module.xml
%wildfly_home%/modules/system/layers/base/login/mongodb/main/mongo-java-driver-3.0.3.jar
%wildfly_home%/modules/system/layers/base/login/mongodb/main/MongoLoginModule.jar
这是module.xml
:
<module xmlns="urn:jboss:module:1.1" name="login.mongodb">
<resources>
<resource-root path="MongoLoginModule.jar"/>
<resource-root path="mongo-java-driver-3.0.3.jar"/>
</resources>
<dependencies>
<module name="org.apache.log4j"/>
<module name="javax.api"/>
<module name="org.slf4j"/>
</dependencies>
</module>
我必须弄清楚为什么登录模块不能像Wildfly模块那样访问。这可能是Wildfly的一个问题(或功能)。我把它叫做WildFly JIRA
您仍然可以使用正常工作的标准servlet身份验证
将WEB-INF/jboss WEB.xml
文件添加到部署中:
蒙古族
调整代码以使用HttpServletRequest.login(String,String)
方法
@Context
私有HttpServletRequest请求;
@职位
@路径(“/login”)
@佩尔米塔尔
@使用(MediaType.APPLICATION_JSON)
公共响应登录(用户凭据){
试一试{
请求登录(userCredentials.getUserName(),
userCredentials.getPassword());
Subject Subject=org.jboss.security.SecurityContextAssociation.getSubject();
可选角色组=subject.getPrincipals(Group.class).stream().filter(p->“Roles”.equals(p.getName()))
.findFirst();
if(rolesGroup.isPresent()){
List roleNames=Collections.List(rolesGroup.get().members()).stream().map(p->p.getName())
.collect(Collectors.toList());
// ...
}否则{
// ...
}
}捕获(ServletException e){
log.error(“登录失败”,e);
返回响应。状态(状态。禁止)。实体(“未记录”)
.type(MediaType.APPLICATION_JSON_type).build();
}
}
这可能是WildFly的一个问题(或功能)。我把它叫做WildFly JIRA
您仍然可以使用正常工作的标准servlet身份验证
将WEB-INF/jboss WEB.xml
文件添加到部署中:
蒙古族
调整代码以使用HttpServletRequest.login(String,String)
方法
@Context
私有HttpServletRequest请求;
@职位
@路径(“/login”)
@佩尔米塔尔
@使用(MediaType.APPLICATION_JSON)
公共响应登录(用户凭据){
试一试{
请求登录(userCredentials.getUserName(),
userCredentials.getPassword());
Subject Subject=org.jboss.security.SecurityContextAssociation.getSubject();
可选角色组=subject.getPrincipals(Group.class).stream().filter(p->“Roles”.equals(p.getName()))
.findFirst();
if(rolesGroup.isPresent()){
List roleNames=Collections.List(rolesGroup.get().members()).stream().map(p->p.getName())
.collect(Collectors.toList());
// ...
}否则{
// ...
}
}捕获(ServletException e){
log.error(“登录失败”,e);
返回响应。状态(状态。禁止)。实体(“未记录”)
.type(MediaType.APPLICATION_JSON_type).build();
}
}
我的应用程序找不到该模块,因为我忘了将依赖项
添加到它的清单
,或者将login.mongodb
声明为Wildfly中的全局模块。感谢@kwart的建议和回答,您为我指明了正确的方向。我的应用程序找不到该模块,因为我忘记了在其清单中添加依赖项
,或者在Wildfly中将login.mongodb
声明为全局模块。感谢@kwart的建议和回答,您为我指明了正确的方向。您是如何打包和安装包含自定义LoginModule的服务器模块(“login.mongodb”)的?您可以在第一步尝试避免使用服务器模块,并在应用程序WAR中包含自定义登录模块(在这种情况下,请在standalone.xml中从安全域配置中删除模块属性)。在阅读您的评论之前,我做了您建议的测试。我将登录模块jar包含在web应用程序中,并从standalone.xml登录模块节点中删除了模块属性。这是可行的,现在我必须弄清楚为什么登录模块不能像Wildfly模块那样访问。我更新了我的问题。您是如何打包和安装包含自定义LoginModule的服务器模块(“login.mongodb”)的?您可以在第一步尝试避免使用服务器模块,并在应用程序WAR中包含自定义登录模块(在这种情况下,请在standalone.xml中从安全域配置中删除模块属性)。在阅读您的评论之前,我做了您建议的测试。我将登录模块jar包含在web应用程序中,并从standalone.xml登录模块节点中删除了模块属性。这是可行的,现在我必须弄清楚为什么登录模块不能像Wildfly模块那样访问。我更新了我的问题。
22:39:49,421 ERROR [it.bytebear.web.mongo.UserServices] (default task-1) login fails.: javax.security.auth.login.LoginException: impossibile trovare la classe Login
Module: it.bytebear.jaas.mongo.module.MongoLoginModule from [Module "deployment.MongoWebTest.war:main" from Service Module Loader]
at javax.security.auth.login.LoginContext.invoke(LoginContext.java:822)
at javax.security.auth.login.LoginContext.access$000(LoginContext.java:203)
at javax.security.auth.login.LoginContext$4.run(LoginContext.java:698)
at javax.security.auth.login.LoginContext$4.run(LoginContext.java:696)
at java.security.AccessController.doPrivileged(Native Method)
at javax.security.auth.login.LoginContext.invokePriv(LoginContext.java:695)
at javax.security.auth.login.LoginContext.login(LoginContext.java:594)
at it.bytebear.web.mongo.UserServices.login(UserServices.java:66)
%wildfly_home%/modules/system/layers/base/login/mongodb/main/module.xml
%wildfly_home%/modules/system/layers/base/login/mongodb/main/mongo-java-driver-3.0.3.jar
%wildfly_home%/modules/system/layers/base/login/mongodb/main/MongoLoginModule.jar
<module xmlns="urn:jboss:module:1.1" name="login.mongodb">
<resources>
<resource-root path="MongoLoginModule.jar"/>
<resource-root path="mongo-java-driver-3.0.3.jar"/>
</resources>
<dependencies>
<module name="org.apache.log4j"/>
<module name="javax.api"/>
<module name="org.slf4j"/>
</dependencies>
</module>