I am very new to the whole J2EE architecture. Could somebody help me out?
I have a Swing client with Login, Password fields on machine A.
Properties p = new Properties();
p.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
p.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");
p.put(Context.PROVIDER_URL, "jnp://localhost:1099/");
InitialContext ctx = new InitialContext(p);
ejb = (MyBeanRemote) ctx.lookup("DemoServer/MyBean/remote");
I have an EJB 3.0 on JBoss5 on machine B:
@Stateful
@DeclareRoles({"editor", "viewer"})
public class MyBean implements MyBeanRemote, Serializable {
@RolesAllowed({"editor"})
public boolean modify() throws Exception {
if(!ctx.isCallerInRole("editor")) throw new SecurityException("Can't modify");
return true;
}
@RolesAllowed({"viewer","editor"})
public boolean view() throws Exception {
if(!ctx.isCallerInRole("viewer")) throw new SecurityException("Can't view");
return true;
}
}
I have a DB with users and roles on server (conf/login-conf.xml):
<application-policy name="jboss-secure">
<authentication>
<login-module code="org.jboss.security.auth.spi.DatabaseServerLoginModule" flag="required">
<module-option name="unauthenticatedIdentity">guest</module-option>
<module-option name="dsJndiName">java:/MyDerby</module-option>
<module-option name="principalsQuery">SELECT Password FROM Users WHERE Username=?</module-option>
<module-option name="rolesQuery">SELECT Role, 'Roles' FROM Users WHERE Username=?</module-option>
</login-module>
</authentication>
</application-policy>
META-INF/jboss.xml:
<jboss>
<security-domain>java:/jaas/jboss-secure</security-domain>
</jboss>
As I understand, I shouldn't get ejb reference until client is authenticated properly. How do I use LoginContext, and do I use it at all? Can/should I use @EJB private MyBeanRemote ejb
? And how do I make the whole thing work?
I am just trying to build a seemingly trivial thing: authorization of application client. I feel stupid.
Thank you very much.