Java 条件配置 (web.xml) - 开发/生产



我在 Spring 应用程序的 web.xml 文件中有以下配置:

<session-config>
   <cookie-config>
      <name>mycookie</name>
      <http-only>true</http-only>
      <secure>true</secure>
    </cookie-config>       
</session-config>

问题是:<secure>true</secure>强制cookie仅通过HTTPS发送,并且在我的开发机器中我没有设置HTTPS。但是,此配置在生产环境中是必不可少的。

有没有办法使此配置对环境敏感 - 即 false用于开发和官方/生产版本的true

如果您

使用的是Maven(我强烈建议(,那么您可以将配置文件与maven-war-plugin一起使用。您可以在每个配置文件中指定不同的web.xml(因此,您可以将一个用于生产环境,另一个用于开发(。这是给您的示例

<properties>
    <webXmlPath>path/to/your/dev/xml</webXmlPath>
</properties>
<profiles>
    <profile>
        <id>prod</id>
        <properties>
            <webXmlPath>path/to/prod/webXml</webXmlPath>
        </properties>
    </profile>
</profiles>
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>${war-plugin.version}</version>
            <configuration>
                <webXml>${webXmlPath}</webXml>
            </configuration>
        </plugin>
    </plugins>
</build>

现在,默认情况下,每个构建都将具有具有开发设置的web.xml。当您想将其更改为生产版本时,只需执行

mvn clean install -Pprod

使用属性的解决方案非常简单:

<secure>${session.cookie.secure}</secure>

在我的开发.properties文件中,我添加了:

session.cookie.secure=false

在测试/生产中,我可以将其设置为true

最新更新