You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@shiro.apache.org by Martin Nielsen <mn...@gmail.com> on 2016/04/05 10:21:10 UTC

Using Shiro in OSGi and registering realms dynamically

Hello.
I am looking into using Shiro for an OSGi based application, but i have run
into a few things that i have trouble figuring out: I have dabbled in Shiro
before and i don't think i can properly express how much i prefer it to
JAAS. But this will be my first attempt to implement a Shiro-backed
application in OSGi, and i am having some trouble figuring out how exactly
to use it correctly.

Looking around the interwebs, i have found some guides to using shiro in
OSGi, but i have found none that actually use Shiro in an "OSGi way", doing
dynamic registrations of realms and such.


1. Is it possible for a Bundle to register its own realms to Shiro at
runtime? The examples and resources i have looked at gives me the
impression that the realms are bound to the shiro securitymanager once, and
they do not seem easy to add to or change.

2. Can sessions and cache be shared across a karaf cluster, or is the shiro
bundle only local to  single Karaf instance.

Re: Using Shiro in OSGi and registering realms dynamically

Posted by Martin Nielsen <mn...@gmail.com>.
No, unfortunately not. I really need the runtime registration of realms,
otherwise the whole design needs to be reworked, which i would like to
avoid:)

Does anyone have any experience with this?

On Tue, Apr 5, 2016 at 2:59 PM, scSynergy <ro...@scsynergy.de>
wrote:

> I am not sure whether this information will help you, but here is some code
> we use to initialize Shiro programmatically from an entry in MongoDB (we
> use
> https://ops4j1.jira.com/wiki/display/PAXSHIRO/OPS4J+Pax+Shiro for CDI
> integration). Notice how we add the realm 'CamelRealm.CAMELREALM (=
> "camelRealm")' through ini.setSectionProperty(...). We do not modify
> Shiro's
> configuration after it has been initialized, so I have no suggestion for
> you
> how to *change* realms, but *adding them programmatically at the time of
> initialization* works:
>
> MongoClientOptions.Builder builder =
> MongoClientOptions.builder().socketKeepAlive(true).connectionsPerHost(1);
> MongoClientURI uri = new MongoClientURI("mongodb://" + hostPort + "/" +
> dbName + "?" + options, builder);
> MongoClient mongo = new MongoClient(uri);
> DBCollection collection = mongo.getDB(dbName).getCollection("shiro-ini");
> DBCursor cursor = collection.find().sort(new BasicDBObject("timestamp",
> -1)).limit(1);
> if (cursor != null && cursor.hasNext()) {
>             String iniFile = cursor.next().get("conf").toString();
>             Ini ini = new Ini();
>             ini.load(iniFile);
>             String realms = ini.getSectionProperty("main",
> "securityManager.realms");
>             if (realms != null && !realms.contains(CamelRealm.CAMELREALM))
> {
>                 ini.setSectionProperty("main", "securityManager.realms",
> realms + ", $" + CamelRealm.CAMELREALM);
>             }
>             IniSecurityManagerFactory factory = new
> CdiIniSecurityManagerFactory(ini, manager);
>             SecurityManager securityManager = (SecurityManager)
> factory.getInstance();
>             SecurityUtils.setSecurityManager(securityManager);
> }
> mongo.close();
>
>
>
> --
> View this message in context:
> http://shiro-user.582556.n2.nabble.com/Using-Shiro-in-OSGi-and-registering-realms-dynamically-tp7581004p7581006.html
> Sent from the Shiro User mailing list archive at Nabble.com.
>

Re: Using Shiro in OSGi and registering realms dynamically

Posted by scSynergy <ro...@scsynergy.de>.
I am not sure whether this information will help you, but here is some code
we use to initialize Shiro programmatically from an entry in MongoDB (we use
https://ops4j1.jira.com/wiki/display/PAXSHIRO/OPS4J+Pax+Shiro for CDI
integration). Notice how we add the realm 'CamelRealm.CAMELREALM (=
"camelRealm")' through ini.setSectionProperty(...). We do not modify Shiro's
configuration after it has been initialized, so I have no suggestion for you
how to *change* realms, but *adding them programmatically at the time of
initialization* works:

MongoClientOptions.Builder builder =
MongoClientOptions.builder().socketKeepAlive(true).connectionsPerHost(1);
MongoClientURI uri = new MongoClientURI("mongodb://" + hostPort + "/" +
dbName + "?" + options, builder);
MongoClient mongo = new MongoClient(uri);
DBCollection collection = mongo.getDB(dbName).getCollection("shiro-ini");
DBCursor cursor = collection.find().sort(new BasicDBObject("timestamp",
-1)).limit(1);
if (cursor != null && cursor.hasNext()) {
            String iniFile = cursor.next().get("conf").toString();
            Ini ini = new Ini();
            ini.load(iniFile);
            String realms = ini.getSectionProperty("main",
"securityManager.realms");
            if (realms != null && !realms.contains(CamelRealm.CAMELREALM)) {
                ini.setSectionProperty("main", "securityManager.realms",
realms + ", $" + CamelRealm.CAMELREALM);
            }
            IniSecurityManagerFactory factory = new
CdiIniSecurityManagerFactory(ini, manager);
            SecurityManager securityManager = (SecurityManager)
factory.getInstance();
            SecurityUtils.setSecurityManager(securityManager);
}
mongo.close();



--
View this message in context: http://shiro-user.582556.n2.nabble.com/Using-Shiro-in-OSGi-and-registering-realms-dynamically-tp7581004p7581006.html
Sent from the Shiro User mailing list archive at Nabble.com.