Google 每个环境的 App Engine 不同 web.xml 配置?
Google App Engine different web.xml config per environment?
我正在开发要部署在 Google App Engine 上的 Java 应用程序。 Google App Engine 允许我在我的 WEB-INF 文件夹中包含一个 web.xml 文件,我可以在其中为不同的 URL 配置不同级别的身份验证。
如果我只希望管理员用户能够访问 foobar URL,我可以使用此配置:
<security-constraint>
<web-resource-collection>
<web-resource-name>aname</web-resource-name>
<url-pattern>/foobar/*</url-pattern>
</web-resource-collection>
<user-data-constraint>
<transport-guarantee>CONFIDENTIAL</transport-guarantee>
</user-data-constraint>
</security-constraint>
如果我想让任何用户——甚至是未经身份验证的用户——能够访问 foobar URL,我可以使用这个配置:
<security-constraint>
<user-data-constraint>
<transport-guarantee>CONFIDENTIAL</transport-guarantee>
</user-data-constraint>
</security-constraint>
问题来了。在我的测试环境中,我想只授权管理员访问 foobar。但是在我的生产环境中,我想允许所有用户(包括未经身份验证的用户)访问 foobar。我怎样才能做到这一点?如何更改每个环境的 web.xml 配置?
您可以为每个环境保留不同的 web.xml,并在部署到每个环境时替换。
例如web_dev.xml、web_test.xml、web_prod.xml
将 web_prod.xml 替换为 web.xml
我决定实施一个类似于 Vikram 建议但略有不同的解决方案。
而不是在我的 WEB-INF 文件夹中只有一个 web.xml 文件:
- WEB-INF
+ web.xml
我在 WEB-INF 文件夹中创建了两个文件夹(每个环境一个):
- WEB-INF
+ local
| + web.xml
+ prod
+ web.xml
现在,为了让 Google App Engine 获取配置文件,它需要位于 WEB-INF 中,而不是我创建的子目录中。因此,在构建期间,我使用 Maven War 插件将文件从两个文件夹之一复制到父文件夹中。
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<webResources>
<resource>
<directory>${basedir}/src/main/webapp/WEB-INF/${configDirectory}</directory>
<filtering>false</filtering>
<targetPath>WEB-INF</targetPath>
<includes>
<include>web.xml</include>
</includes>
</resource>
</webResources>
</configuration>
</plugin>
并且我使用 Maven 配置文件来指定包含我实际要使用的文件的文件夹的名称。
<profiles>
<profile>
<id>local</id>
<properties>
<configDirectory>local</configDirectory>
</properties>
</profile>
<profile>
<id>prod</id>
<properties>
<configDirectory>prod</configDirectory>
</properties>
</profile>
</profiles>
因此,web.xml 文件从正确的子目录复制到父目录,从而允许我控制每次构建应用程序时使用哪个配置。
我正在开发要部署在 Google App Engine 上的 Java 应用程序。 Google App Engine 允许我在我的 WEB-INF 文件夹中包含一个 web.xml 文件,我可以在其中为不同的 URL 配置不同级别的身份验证。
如果我只希望管理员用户能够访问 foobar URL,我可以使用此配置:
<security-constraint>
<web-resource-collection>
<web-resource-name>aname</web-resource-name>
<url-pattern>/foobar/*</url-pattern>
</web-resource-collection>
<user-data-constraint>
<transport-guarantee>CONFIDENTIAL</transport-guarantee>
</user-data-constraint>
</security-constraint>
如果我想让任何用户——甚至是未经身份验证的用户——能够访问 foobar URL,我可以使用这个配置:
<security-constraint>
<user-data-constraint>
<transport-guarantee>CONFIDENTIAL</transport-guarantee>
</user-data-constraint>
</security-constraint>
问题来了。在我的测试环境中,我想只授权管理员访问 foobar。但是在我的生产环境中,我想允许所有用户(包括未经身份验证的用户)访问 foobar。我怎样才能做到这一点?如何更改每个环境的 web.xml 配置?
您可以为每个环境保留不同的 web.xml,并在部署到每个环境时替换。
例如web_dev.xml、web_test.xml、web_prod.xml 将 web_prod.xml 替换为 web.xml
我决定实施一个类似于 Vikram 建议但略有不同的解决方案。
而不是在我的 WEB-INF 文件夹中只有一个 web.xml 文件:
- WEB-INF
+ web.xml
我在 WEB-INF 文件夹中创建了两个文件夹(每个环境一个):
- WEB-INF
+ local
| + web.xml
+ prod
+ web.xml
现在,为了让 Google App Engine 获取配置文件,它需要位于 WEB-INF 中,而不是我创建的子目录中。因此,在构建期间,我使用 Maven War 插件将文件从两个文件夹之一复制到父文件夹中。
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<webResources>
<resource>
<directory>${basedir}/src/main/webapp/WEB-INF/${configDirectory}</directory>
<filtering>false</filtering>
<targetPath>WEB-INF</targetPath>
<includes>
<include>web.xml</include>
</includes>
</resource>
</webResources>
</configuration>
</plugin>
并且我使用 Maven 配置文件来指定包含我实际要使用的文件的文件夹的名称。
<profiles>
<profile>
<id>local</id>
<properties>
<configDirectory>local</configDirectory>
</properties>
</profile>
<profile>
<id>prod</id>
<properties>
<configDirectory>prod</configDirectory>
</properties>
</profile>
</profiles>
因此,web.xml 文件从正确的子目录复制到父目录,从而允许我控制每次构建应用程序时使用哪个配置。