简体   繁体   中英

Apache Shiro: ClassCastException on Realm

Ok, I am officially confused.

In Shiro I have my own realm (DatastoreRealm) that extends AuthorizingRealm. In my DatastoreRealm, I have the method " clearCachedAuthorizationInfo " which allows me to clear the users permissions, etc, (and then re-check) when those permissions change on the fly.

In order to get to that method, I have to get access to my DatastoreRealm object. I do this in the following way...

private static Realm lookupRealm(String realmName)  {
     SecurityManager securityManager = SecurityUtils.getSecurityManager();
     RealmSecurityManager realmSecurityManager = (RealmSecurityManager) securityManager;

     Collection<Realm> realms = realmSecurityManager.getRealms();
     for (Realm realm : realms) {
         if (realm.getName().equalsIgnoreCase(realmName)) {
             log.info("look up realm name is : " + realm.getName());
             return realm;
         }
     }
    return null; }

This seems to work fine. It returns me a "DatastoreRealm" object.

Although when I call this method, I am forced to do the following...

 DatastoreRealm dsRealm =  (DatastoreRealm) lookupRealm("rfRealm");

Which throws a " ClassCastException " telling me...

rf.gae.DatastoreRealm cannot be cast to rf.gae.DatastoreRealm

How/Why is this happening???

If I DON'T cast, and simply use the " Realm " object, the " clearCachedAuthorizationInfo " is not available to me!

Thanks in advance for the help!

Well with a bit more digging, I figured out the problem.

The web framework that I'm using has the ability to "hot reload" classes which prevents having to restart the server on each code change.

The problem with this is that a new classloader loads the edited class, and thus when a cast is attempted, the class in memory cannot be cast to the new class loaded by the classloader!

For classes to be cast, they have to be of the same type and loaded by the same classloader.

Turning this feature off corrected my casting issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM