简体   繁体   English

Apache Shiro:Realm上的ClassCastException

[英]Apache Shiro: ClassCastException on Realm

Ok, I am officially confused. 好的,我正式感到困惑。

In Shiro I have my own realm (DatastoreRealm) that extends AuthorizingRealm. 在Shiro,我有自己的领域(DatastoreRealm),它扩展了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. 在我的DatastoreRealm中,我有方法“ clearCachedAuthorizationInfo ”,它允许我清除用户权限等,然后在这些权限即时更改时重新检查。

In order to get to that method, I have to get access to my DatastoreRealm object. 为了获得该方法,我必须访问我的DatastoreRealm对象。 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. 它返回一个“DatastoreRealm”对象。

Although when I call this method, I am forced to do the following... 虽然当我称这种方法时,我被迫做以下事情......

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

Which throws a " ClassCastException " telling me... 抛出“ ClassCastException ”告诉我......

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! 如果我不投射,只是使用“ Realm ”对象,我就无法使用“ clearCachedAuthorizationInfo ”!

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. 我正在使用的Web框架具有“热重新加载”类的能力,这可以防止在每次代码更改时重新启动服务器。

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. 关闭此功能可以纠正我的投射问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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