使用Tomcat 7访问特定于上下文的领域 [英] Accessing a context specific realm with Tomcat 7

查看:65
本文介绍了使用Tomcat 7访问特定于上下文的领域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在JSF Web应用程序中使用自制"领域.

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.

我想访问此领域,以在我的JSF Web应用程序中公开其配置属性.

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;
    }
}

我应该使用什么来访问在webapp上下文中声明的领域?

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

谢谢.

推荐答案

您可以在实例上添加已有代码的小扩展名.

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

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

这篇关于使用Tomcat 7访问特定于上下文的领域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆