如何在.wars之间共享安全约束?

时间:2011-10-14 13:10:26

标签: security java-ee war web-inf

我有一个Java EE应用服务器(jboss-eap-4.3)和几个构成一个更大的Web应用程序的.wars。这个想法是.war可以单独运行或与另一个.war链接。由于它们都是同一个应用程序的概念,因此我们不想提供多个登录信息。

我想配置.wars,以便它们共享相同的安全约束和安全角色。基本上这部分是web.xml:

<security-constraint>
   <web-resource-collection>
      <url-pattern>/*</url-pattern>
   </web-resource-collection>
   <auth-constraint>
      <role-name>Admin</role-name>
   </auth-constraint>
<security-constraint>

<security-role>
   <role-name>Admin</role-name>
</security-role>

<login-config>
   <auth-method>BASIC</auth-method>
   <realm-name>WebApp</realm-name>
</login-config>

我们的角色最近经常发生变化,我们也会定期添加新的.wars。另外,我们根据部署环境更改auth方法,这增加了调整的另一个原因。理想情况下,我想要一种方法来中断web.xml的安全部分,以便其他人可以“继承”它。我认为领域可能是一个寻找这个的好地方,但我没有发现任何有希望的东西。

请注意,此容器中还有其他Web应用程序具有完全不同的安全域,因此tomcat的全局设置可能不合适。

1 个答案:

答案 0 :(得分:0)

不是一个很好的答案,但我最终使用如下所示的ant macrodef自动执行脏工作。

  <!-- 
   | Take a "plain" web.xml and add security settings to it.  
   | This will add BASIC authentication with Admin, Operator, and Guest role access 
   |
   -->
   <taskdef resource="net/sf/antcontrib/antlib.xml" /> 
   <macrodef name="addSecurityToWeb.xml">
      <attribute name="file"/>
      <sequential>
         <if>
            <not>
                <isfileselected file="@{file}">
                    <contains text="login-config" ignorewhitespace="true"/>
                </isfileselected>
            </not>
            <then>
               <replace file="@{file}">
                  <replacetoken><![CDATA[</web-app>]]></replacetoken>
                  <replacevalue>
   <![CDATA[
    <security-constraint>
        <web-resource-collection>
            <web-resource-name>Protected Area</web-resource-name>
            <url-pattern>/*</url-pattern>
        </web-resource-collection>
        <auth-constraint>
            <role-name>Admin</role-name>
        </auth-constraint>

        <transport-guarantee>NONE</transport-guarantee>
    </security-constraint>

    <!-- Security roles referenced by this web application -->
    <security-role>
        <role-name>Admin</role-name>
    </security-role>

    <login-config>
        <auth-method>BASIC</auth-method>
        <realm-name>WebApp</realm-name>
    </login-config>
</web-app>
   ]]>    
                  </replacevalue>
               </replace>
            </then>
         </if>
      </sequential>
  </macrodef>
相关问题