views:

1484

answers:

2

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.

+1  A: 

There is a tutorial here

Maurice Perry
+1  A: 

I did some work with JAAS recentely and found it can be quite tricky. Check out these two resources I found them very useful http://www.jaasbook.com/ and http://docs.jboss.org/jbossas/admindevel326/html/ch8.chapter.html

I've also got a load of other JAAS resources book marked at http://delicious.com/chronosMark/JAAS hopefully one of them will help you out if the other two don't.

Mark Davidson
I checked them all out already and I don't really need this anymore. But thanks anyway. This is the best answer. Hopefully it will be useful to some people.
pitr