您好我想在EJB中尝试JAAS,我不知道如何做到这一点,但我想在Web应用程序中使用表单身份验证时,我必须使用与request.login
类似的东西。
我正在使用WildFly 8.2.1 Final,eclipse luna。
这是我已经完成的事情。
standalone.xml
<security-realm name="myJdbcRealm">
<authentication>
<jaas name="myJdbcSecurityDomain" />
</authentication>
</security-realm>
<security-domain name="myJdbcSecurityDomain" cache-type="default">
<authentication>
<login-module code="Database" flag="required">
<module-option name="dsJndiName" value="java:jboss/appmysql_ds"/>
<module-option name="principalsQuery" value="select user_password from users where username = ?"/>
<module-option name="rolesQuery" value="select role_name, 'Roles' from user_roles where username = ?"/>
<module-option name="hashAlgorithm" value="SHA-256"/>
<module-option name="hashCharset" value="UTF-8"/>
<module-option name="hashEncoding" value="base64"/>
</login-module>
</authentication>
</security-domain>
的JBoss-ejb3.xml
<?xml version="1.1" encoding="UTF-8"?>
<jboss:ejb-jar xmlns:jboss="http://www.jboss.com/xml/ns/javaee"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:s="urn:security:1.1"
version="3.1"
impl-version="2.0">
<assembly-descriptor>
<s:security>
<ejb-name>*</ejb-name>
<s:security-domain>myJdbcSecurityDomain</s:security-domain>
<s:run-as-principal>Admin</s:run-as-principal>
<s:missing-method-permissions-deny-access>false</s:missing-method-permissions-deny-access>
</s:security>
</assembly-descriptor>
</jboss:ejb-jar >
LoginImpl
@Stateless
@PermitAll
public class LoginImpl implements LoginRemote, LoginLocal {
public void login() throws EJBException {
LoginContext loginContext; // here I do not know what to do
}
}
更新 - 新的LoginImpl.java
以下是新的LoginImpl.java
,我已成功登录,但现在的问题是,当我尝试拨打getCallerPrincipal()
时,会给我anonymous
@Stateless
public class LoginImpl implements LoginRemote, LoginLocal {
@Resource
private SessionContext sessionContext;
public void login(String username, String password) throws LoginException {
MyCallbackHandler sec = new MyCallbackHandler(username, password.toCharArray());
PrivilegedAction privilegedAction = new MyPrivilegeAction("admin");
LoginContext loginContext = new LoginContext("myJdbcSecurityDomain", sec);
loginContext.login();
Subject user = loginContext.getSubject();
Subject.doAs(user, privilegedAction);
System.out.println("Login Successful");
}
public void printPricipal() {
System.out.println("Principal : " + sessionContext.getCallerPrincipal());
}
}