[英]Java Conditional configuration (web.xml) - Development/Production
我在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
(因此您可以为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
使用propety解决方案非常简单:
<secure>${session.cookie.secure}</secure>
在我的开发.properties
文件中,我添加了:
session.cookie.secure=false
在测试/制作中,我可以将其设置为true
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.