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

时间:2015-10-20 20:58:21

标签: java spring java-ee web.xml production-environment

我在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用于官方/生产版本?

2 个答案:

答案 0 :(得分:4)

如果您正在使用Maven(我强烈建议),那么您可以将配置文件与maven-war-plugin一起使用。您可以在每个配置文件中指定不同的web.xml(因此您可以为prod环境指定一个,为dev设置其他配置文件)。这是一个例子

<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

答案 1 :(得分:4)

解决方案使用一个非常简单的方法:

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

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

session.cookie.secure=false

在测试/制作中,我可以将其设置为true

相关问题