2015-10-20 34 views
2

我在web.xml文件中的下列配置在Spring應用程序:Java條件配置文件(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官方/生產版本?

+1

我會做到這一點反過來:讓你的開發機器支持HTTPS。 (對於擁有自簽名證書的Tomcat來說相對容易)。 – Ralph

+0

@Ralph,使用真正的證書變得更容易。 –

回答

4

如果您使用Maven(我強烈建議),那麼您可以使用配置文件連同maven-war-plugin。您可以在每個配置文件中指定不同的web.xml(這樣您可以爲prod環境和其他開發人員指定一個)。這裏是你

<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 
+0

這是一個簡單而有趣的解決方案!當生產和開發之間有很多配置差異時非常有用。 –

3

的解決方案是非常簡單的使用歡迎使用屬性:

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

在我的開發.properties文件我補充說:

session.cookie.secure=false 

在測試/生產中,我可以將其設置爲true