Accessing a context specific realm with Tomcat 7

半城伤御伤魂 提交于 2019-12-24 19:16:52

问题


I am using a "home made" realm in a JSF webapp.

<Context docBase="senateurs" jndiExceptionOnFailedWrite="false" path="/senateurs" reloadable="true">
    <Resource auth="Container" description="Extraction des roles" maxActive="3" maxIdle="1" maxWait="60" name="jdbc/authSen" type="javax.sql.DataSource" validationQuery="select * from senateurs.qua"/>
    <Realm className="org.apache.catalina.realm.SenatLdapDataSourceRealm" dataSourceName="jdbc/authSen" instanceName="realm-senateurs" localDataSource="true" roleNameCol="rolcod" userNameCol="perlogldap" userRoleTable="senateurs.perrol"/>
</Context>

In the following question, it could be just any realm.

I would like to access this realm, to expose its configuration property in my JSF webapp.

To access the DataSource, I can use the following function :

public static BasicDataSource getDataSource(String dataSourceName) throws NamingException {
    InitialContext ic = new InitialContext();
    ic.createSubcontext("java:");
    ic.createSubcontext("java:comp");
    ic.createSubcontext("java:comp/env");
    String subContext = "java:comp/env";
    String[] split = dataSourceName.split("/");
    for(int i = 0 ; i < split.length -1 ; i++) {
        subContext += "/" + split[i];
        ic.createSubcontext(subContext);
    }
    return (BasicDataSource) ic.lookup("java:comp/env/" + dataSourceName);
}

To access a realm declared in host configuration, I can use :

public static Realm getTomcatRealm() {
    try {
        MBeanServer mBeanServer = MBeanServerFactory.findMBeanServer(null).get(0);
        ObjectName name = new ObjectName("Catalina", "type", "Server");
        Server server = (Server) mBeanServer.getAttribute(name, "managedResource");
        Service service = server.findService("Catalina");
        Engine engine = (Engine) service.getContainer();
        Host host = (Host) engine.findChild(engine.getDefaultHost());
        return host.getRealm();
    } catch (MBeanException ex) {
        log.error("Erreur lors de l'accès a serveur Tomcat",ex);
    } catch (AttributeNotFoundException ex) {
        log.error("Erreur lors de l'accès a serveur Tomcat",ex);
    } catch (InstanceNotFoundException ex) {
        log.error("Erreur lors de l'accès a serveur Tomcat",ex);
    } catch (ReflectionException ex) {
        log.error("Erreur lors de l'accès a serveur Tomcat",ex);
    } catch (MalformedObjectNameException ex) {
        log.error("Erreur lors de l'accès a serveur Tomcat",ex);
    } finally {
        return null;
    }
}

What should I use to access the realm declared in the webapp context ?

Thanks in advance.


回答1:


You can get the instance with a small extension to the code you already have.

Context context = (Context) host.findChild("/senateurs");
return context.getRealm();



回答2:


I still did not find the instance, but can access its attributes with

public static Object getTomcatContextRealmAttribute(String attr) {
    try {
        MBeanServer mBeanServer = MBeanServerFactory.findMBeanServer(null).get(0);
        Set<ObjectInstance> realms = mBeanServer.queryMBeans(new ObjectName("Catalina:context=*,host=*,realmPath=*,type=*"), null);
        if(realms.size() != 1) {
            // unhandled case...
            return null;
        }
        return mBeanServer.getAttribute(realms.iterator().next().getObjectName(),attr);
    } catch (MalformedObjectNameException ex) {
        Logger.getLogger(Utils.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        return null;
    }
}

which fits my need.



来源:https://stackoverflow.com/questions/19313883/accessing-a-context-specific-realm-with-tomcat-7

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!