星期四 一月 17, 2008

    开发J2EE应用程序时, 如果希望在应用服务中进行用户安全认证而又避免编程,可以使用Sun glassfish中提供的realm. 本文以一个例子简要说明如何使用realm.

     在glassfish 预配置了三种realm: file, certificate, and admin-realm. 以file Realm为例.

     1. 建立"file" Realm 用户

       在Application Server Admin Console中打开Configuration->Security->Realms->file节点.

       点击"Manage Users" 按钮

       点击"New.." 添加一个新的用户, 填入用户ID 并设置密码,点击"Save" 按钮保存更改, 例如建立一个用户"testId", 他的用户组是"commonGroup"

 

     2. 在web.xml中建立设置安全约束
       打开web.xml文件, 以Netbeans 5.5 IDE 为例 (什么? 还没有Netbeans? 去这里http://www.netbeans.org/吧,免费的)

       扩展"登陆配置"选项, 选中"基本", 在"域名称"填入"file". 这个"file"就是在admin Console中看见的"file"按钮.

       扩展"安全角色"选项, 添加一个新的安全角色. 只是一个名称,随便起好了.比如"common-user-role"

       扩展"安全约束"选项, 点击"添加..."按钮, 起一个名字如"Basic Realm", 设置相应的URL模式(可以设置为"/*") 和HTTP方法.  选中"启用验证约束"选项, 点击"编辑" 按钮,选中想要添加的角色, 点击"添加>"按钮 添加这个角色.

      以上的设置完成之后, 在web.xml中,应该多出了如下代码, 包含了刚才所有设置:

          <security-constraint>
            <web-resource-collection>
               <web-resource-name>Basic Realm</web-resource-name>
               <url-pattern>/*</url-pattern>
             </web-resource-collection>
             <auth-constraint>
                <role-name>common-user-role</role-name>
             </auth-constraint>
         </security-constraint>
            <login-config>
              <auth-method>BASIC</auth-method>
              <realm-name>file</realm-name>
            </login-config>
       <security-role>
           <description>common user</description>
           <role-name>common-user-role</role-name>
       </security-role>

   3. 在sun-web.xml中建立映射关系

       打开sun-web.xml, 添加以下代码, 其中我们建立了"common-suer-role"(它是web.xml中定义的用户名称)和"commonGroup"(它是file Realm的用户组)的映射关系! 很晕吧?

         <security-role-mapping>
            <role-name>common-user-role</role-name>
            <group-name>commonGroup</group-name>
         </security-role-mapping>

   4. 重新生成和部署应用

      这是最后一步,就不罗嗦了. 祝好运!



 


 

 

评论:

嗯;很好;日后有机会可以试一试;

发表于 lucky 在 2008年01月23日, 05:09 下午 CST #

发表一条评论:
  • HTML语法: 禁用