GWT + Guice + Shiro

1,237 views
Skip to first unread message

Fabricio Pizzichillo

unread,
Aug 4, 2011, 6:21:48 PM8/4/11
to google-we...@googlegroups.com
Hello Friends
I have an application with GWT + Gin + Guice + Postgresql + Mybatis. Can anyoneguide me on how to configure ApacheShiro to authenticate users?
thanks

Subhrajyoti Moitra

unread,
Aug 5, 2011, 11:50:03 AM8/5/11
to google-we...@googlegroups.com
I am using Shiro with GWT, but no Guice. I also, don't use out of the box Shiro's web application security/servlet, since my app is not really a page based application.

After login get the sessionId and pass it along with every RPC call.

Sample Shiro.ini (change as per your requirements)

[main]
ds = com.mysql.jdbc.jdbc2.optional.MysqlDataSource
ds.serverName = localhost
ds.user = root
ds.password=xxxxx
ds.databaseName = somedb

md5matcher=org.apache.shiro.authc.credential.Md5CredentialsMatcher
permissionResolver = org.apache.shiro.authz.permission.WildcardPermissionResolver

somedbRealm=org.apache.shiro.realm.jdbc.JdbcRealm
somedbRealm.permissionResolver =$permissionResolver
somedbRealm.credentialsMatcher = $md5matcher
#authenticate users.
somedbRealm.authenticationQuery = select password from user where username= ?
# Get roles for the user
somedbRealm.userRolesQuery =select r.rolename from role r, rolemap rm, user u where u.username=? and u.userid=rm.userid and r.roleid=rm.roleid;
# Permissions for roles for authorization.
somedbRealm.permissionsQuery=select p.permission from cms_permissions p,role r,role_permissions rp where r.rolename=? and r.roleid=rp.roleid && p.id=rp.perm_id;
somedbRealm.dataSource = $ds
# without this , permissions and authorization wont work.
somedbRealm.permissionsLookupEnabled=true
cacheManager = org.apache.shiro.cache.ehcache.EhCacheManager


securityManager.realms=$somedbRealm
securityManager.cacheManager = $cacheManager
securityManager.sessionManager.globalSessionTimeout = 7200000

Init code to initialize Shiro sessions manager. I do this in some InitServlet (load-on-startup as 1)

         Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro.ini");
         SecurityManager securityManager =null;
         securityManager = factory.getInstance();
         SecurityUtils.setSecurityManager(securityManager);      



Authenticate users using this code piece.

org.apache.shiro.subject.Subject subject = new org.apache.shiro.subject.Subject.Builder().sessionId(sessionId).buildSubject();
        if (subject != null && subject.isAuthenticated() && subject.getPrincipal() != null) {
            String user = subject.getPrincipal().toString();
       }

How u store the users and roles and permissions, is upto you.
See the shiro.ini file above for examples and you can guess the schema. :)

HTH.

Thanks,
Subhro.


--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To post to this group, send email to google-we...@googlegroups.com.
To unsubscribe from this group, send email to google-web-tool...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.

Levent Bayindir

unread,
Aug 5, 2011, 9:54:42 AM8/5/11
to Google Web Toolkit
+1. I would like to learn more about this too.
Best way to implement Shiro auth. in GWT client side?

On Aug 5, 1:21 am, Fabricio Pizzichillo <fpizzichi...@gmail.com>
wrote:

objectuser

unread,
Aug 7, 2011, 8:37:50 AM8/7/11
to google-we...@googlegroups.com
There's not much to do with Shiro in GWT, but I have a blog post about setting up Shiro with Guice on App Engine.  Most of it should be relevant to you since I don't talk much about the datastore.

One thing you should be able to do differently is to use the DefaultWebSessionManager, which doesn't work on App Engine.

Fabricio Pizzichillo

unread,
Aug 7, 2011, 10:39:14 AM8/7/11
to google-we...@googlegroups.com
Thanks, I'll see it

2011/8/7 objectuser <kevin....@gmail.com>
There's not much to do with Shiro in GWT, but I have a blog post about setting up Shiro with Guice on App Engine.  Most of it should be relevant to you since I don't talk much about the datastore.

One thing you should be able to do differently is to use the DefaultWebSessionManager, which doesn't work on App Engine.

--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.

Les

unread,
Aug 8, 2011, 8:15:11 PM8/8/11
to Google Web Toolkit
Hi folks,

I also wanted to mention the Shiro development team will support Guice
in Shiro 1.2 and later as an officially supported integration module.

Shiro's reference manual is here: http://shiro.apache.org/reference.html
Shiro's Guice-specific documentation is here: http://shiro.apache.org/guice.html

1.2 is not yet released, but you'll be able to use what we have if you
use Shiro's development snapshot releases.

Also, Objectuser's blog article has been a great help and is still
quite useful - both resources should get you off the ground nicely.
Also, Objectuser (or anyone else for that matter!), if you'd like to
help join the Shiro dev list discussion around Guice support so you
can influence the direction of the integration, please feel free to do
so.

HTH!

Cheers,

Les Hazlewood
Apache Shiro team

objectuser

unread,
Aug 10, 2011, 8:43:04 AM8/10/11
to google-we...@googlegroups.com
Thanks, Les!  That's good news.  Shiro is a great framework and I'm excited to see it moving forward.
Reply all
Reply to author
Forward
0 new messages