如何以编程方式将用户添加到tomcat UserDatabaseRealm? [英] how to programatically add users to tomcat UserDatabaseRealm?

查看:181
本文介绍了如何以编程方式将用户添加到tomcat UserDatabaseRealm?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的Java Web应用程序,要登录的用户不超过20-25.我目前正在使用tomcat服务器托管它,并使用UderDatabaseRealm进行访问控制.我想向此应用程序添加功能,其中管理员可以通过应用程序本身将用户添加到系统中.我想知道是否可以以编程方式将用户添加到此文件. 我可以想到的一种方法是在应用程序中打开tomcat_users.xml文件,并进行XML操作以添加用户.有没有比这更好的方法了?

I am having a simple Java web application with not more than 20-25 users who would be logged in. I am currently using tomcat server to host it and am using the UderDatabaseRealm for access control. I would like to add a feature to this application wherein the administrator can add users to the system through the application itself. I would like to know is it possible to programmatically add users to this file. One method I can think of is to open the tomcat_users.xml file within my application and do XML manipulation to add the users. Is there a better way than this?

我的领域在servers.xml中配置为:-

My realm is configured in servers.xml as :-

<Realm className="org.apache.catalina.realm.LockOutRealm">
        <!-- This Realm uses the UserDatabase configured in the global JNDI
             resources under the key "UserDatabase".  Any edits
             that are performed against this UserDatabase are immediately
             available for use by the Realm.  -->
        <Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase"/>
</Realm>

My tomcat_users.xml file is as follows:-
<tomcat-users>

  <role rolename="admin"/>
  <role rolename="local"/>
  <user username="tomcat" password="tomcat" roles="admin"/>
</tomcat-users>

推荐答案

您可以使用JNDI从正在运行的tomcat中获取UserDatabase对象,其中包含有关用户和角色的所有信息.您必须在server.xml中将UserDatabase定义为全局资源,然后将资源链接添加到context.xml文件中,如下所示:

You can use JNDI to get the UserDatabase Object from your running tomcat with all information about your users and roles. You have to define your UserDatabase as global resource in your server.xml and add a resource link to your context.xml file like this:

server.xml

server.xml

<GlobalNamingResources>

     <Resource auth="Container" description="User database that can be updated and saved"
        factory="org.apache.catalina.users.MemoryUserDatabaseFactory" name="UserDatabase"
        pathname="/home/user/tomcat-users.xml" type="org.apache.catalina.UserDatabase"
        readonly="false" />

</GlobalNamingResources>

<Realm className="org.apache.catalina.realm.LockOutRealm">

    <Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase"/>

</Realm>

Web应用程序的

context.xml

context.xml of your webapp

<Context>

    <ResourceLink name="UserDatabase" global="UserDatabase"
        type="org.apache.catalina.UserDatabase" />

</Context>

现在,您可以使用InitialContext获取UserDatabase对象:

Now you can use the InitialContext to get the UserDatabase Object:

UserDatabase ud = (UserDatabase) new InitialContext().lookup("java:comp/env/UserDatabase");

现在您可以像这样调用该数据库的方法:

Now you can call methods of this Database like :

ud.createUser("username", "password", "fullname");

别忘了调用ud.save();方法,以便可以将更改写入xmlfile.要保存此设置,全局资源的readonly属性必须为false.

Don't forget to call the ud.save(); method so that the changes can be written to the xmlfile. To save this, the readonly attribute of the global resource has to be false.

这篇关于如何以编程方式将用户添加到tomcat UserDatabaseRealm?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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