Hi,

How did you put pkg.local.MySecurityManager into the system? You might have 
bigger issues and this is just a symptom.
In order for Jakarta EE module to work correctly, any customizations need to be 
done in a supported way, as described in 
https://shiro.apache.org/web.html#custom_webenvironment_class 
<https://shiro.apache.org/web.html#custom_webenvironment_class>
And your custom environment class needs to inherit from 
org.apache.shiro.ee.listeners.IniEnvironment as describer in 
https://shiro.apache.org/jakarta-ee.html 
<https://shiro.apache.org/jakarta-ee.html>

The problem is that this custom security manager isn’t really supported well, 
and should be avoided. Currently, the Shiro-EE InitEnvironment class isn’t 
really set up return custom SecurityManager.
I would avoid that at all costs if you can.

To access Realms, in particular, or configuration from shiro.ini in general, I 
would do the following:

- Create @ApplicationScoped @Named RealmAccessor bean
- add setRealms(Collection<Realm>) method (and getter for the same)

In your shiro.ini, use the following syntax:
realmAccessor = realmAccessor
realmAccessor.realms = securityManager.realms


This way, your RealmsAccessor bean will have the values you need from your 
configuration, so you can iterate it,
or do whatever needs to be done.

In the mean time, I have an idea how to make IniEnvironment more flexible

Another way is to use FormResubmitSupport.unwrapSecurityManager() method. It’s 
private (for a good reason) and you would need to use reflection to get to it.

Hope this helps.

> On Apr 19, 2024, at 1:25 AM, Andrew G10i <andrewg...@outlook.com> wrote:
> 
> Trying to migrate to Shiro v2.x (from v1.13) and stuck with the following 
> code (which was working fine in 1.13):
> 
> public static MySecurityManager getSecurityManager() {
>     return (MySecurityManager) 
> org.apache.shiro.SecurityUtils.getSecurityManager();
> }
> 
> Getting the following exception:
> java.lang.ClassCastException: class 
> org.apache.shiro.ee.filters.ShiroFilter$WrappedSecurityManager cannot be cast 
> to class pkg.local.MySecurityManager 
> (org.apache.shiro.ee.filters.ShiroFilter$WrappedSecurityManager and 
> pkg.local.MySecurityManager are in unnamed module of loader 
> org.glassfish.web.loader.WebappClassLoader @26652402)
> 
> The issue is that some of my code need to iterate over realms, as result this 
> code doesn't work anymore:getSecurityManager().getRealms().
> 
> Any suggestions how to get access to own custom implementation of Security 
> Manager (MySecurityManager as per the code above) or at least get access to 
> realms?
> 
> Thanks!

Reply via email to