diff --git a/scm-webapp/src/main/java/sonia/scm/ScmServletModule.java b/scm-webapp/src/main/java/sonia/scm/ScmServletModule.java index ec4d568d51..d48ac12394 100644 --- a/scm-webapp/src/main/java/sonia/scm/ScmServletModule.java +++ b/scm-webapp/src/main/java/sonia/scm/ScmServletModule.java @@ -44,6 +44,8 @@ import sonia.scm.api.rest.UriExtensionsConfig; import sonia.scm.cache.CacheManager; import sonia.scm.cache.EhCacheManager; import sonia.scm.config.ScmConfiguration; +import sonia.scm.filter.GZipFilter; +import sonia.scm.filter.SSLFilter; import sonia.scm.filter.SecurityFilter; import sonia.scm.plugin.DefaultPluginManager; import sonia.scm.plugin.Plugin; @@ -83,7 +85,6 @@ import java.util.Map; import java.util.Set; import javax.xml.bind.JAXB; -import sonia.scm.filter.GZipFilter; /** * @@ -92,6 +93,9 @@ import sonia.scm.filter.GZipFilter; public class ScmServletModule extends ServletModule { + /** Field description */ + public static final String PATTERN_ALL = "/*"; + /** Field description */ public static final String PATTERN_DEBUG = "/debug.html"; @@ -184,6 +188,7 @@ public class ScmServletModule extends ServletModule * filter(PATTERN_PAGE, * PATTERN_STATIC_RESOURCES).through(StaticResourceFilter.class); */ + filter(PATTERN_ALL).through(SSLFilter.class); filter(PATTERN_PAGE, PATTERN_COMPRESSABLE).through(GZipFilter.class); filter(PATTERN_RESTAPI, PATTERN_DEBUG).through(SecurityFilter.class); diff --git a/scm-webapp/src/main/java/sonia/scm/config/ScmConfiguration.java b/scm-webapp/src/main/java/sonia/scm/config/ScmConfiguration.java index 90ba9fc552..8efe5d71f7 100644 --- a/scm-webapp/src/main/java/sonia/scm/config/ScmConfiguration.java +++ b/scm-webapp/src/main/java/sonia/scm/config/ScmConfiguration.java @@ -76,6 +76,8 @@ public class ScmConfiguration { this.servername = other.servername; this.pluginUrl = other.pluginUrl; + this.sslPort = other.sslPort; + this.enableSSL = other.enableSSL; } //~--- get methods ---------------------------------------------------------- @@ -102,8 +104,41 @@ public class ScmConfiguration return servername; } + /** + * Method description + * + * + * @return + */ + public int getSslPort() + { + return sslPort; + } + + /** + * Method description + * + * + * @return + */ + public boolean isEnableSSL() + { + return enableSSL; + } + //~--- set methods ---------------------------------------------------------- + /** + * Method description + * + * + * @param enableSSL + */ + public void setEnableSSL(boolean enableSSL) + { + this.enableSSL = enableSSL; + } + /** * Method description * @@ -126,6 +161,17 @@ public class ScmConfiguration this.servername = servername; } + /** + * Method description + * + * + * @param sslPort + */ + public void setSslPort(int sslPort) + { + this.sslPort = sslPort; + } + //~--- fields --------------------------------------------------------------- /** Field description */ @@ -134,4 +180,10 @@ public class ScmConfiguration /** Field description */ private String servername = "localhost"; + + /** Field description */ + private boolean enableSSL = false; + + /** Field description */ + private int sslPort = 8181; } diff --git a/scm-webapp/src/main/java/sonia/scm/filter/SSLFilter.java b/scm-webapp/src/main/java/sonia/scm/filter/SSLFilter.java new file mode 100644 index 0000000000..426160fd66 --- /dev/null +++ b/scm-webapp/src/main/java/sonia/scm/filter/SSLFilter.java @@ -0,0 +1,109 @@ +/** + * Copyright (c) 2010, Sebastian Sdorra + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of SCM-Manager; nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * http://bitbucket.org/sdorra/scm-manager + * + */ + + + +package sonia.scm.filter; + +//~--- non-JDK imports -------------------------------------------------------- + +import com.google.inject.Inject; +import com.google.inject.Singleton; + +import sonia.scm.config.ScmConfiguration; +import sonia.scm.web.filter.HttpFilter; + +//~--- JDK imports ------------------------------------------------------------ + +import java.io.IOException; + +import javax.servlet.FilterChain; +import javax.servlet.ServletException; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +/** + * + * @author Sebastian Sdorra + */ +@Singleton +public class SSLFilter extends HttpFilter +{ + + /** + * Constructs ... + * + * + * @param configuration + */ + @Inject + public SSLFilter(ScmConfiguration configuration) + { + this.configuration = configuration; + } + + //~--- methods -------------------------------------------------------------- + + /** + * Method description + * + * + * @param request + * @param response + * @param chain + * + * @throws IOException + * @throws ServletException + */ + @Override + protected void doFilter(HttpServletRequest request, + HttpServletResponse response, FilterChain chain) + throws IOException, ServletException + { + if (request.isSecure() ||!configuration.isEnableSSL()) + { + chain.doFilter(request, response); + } + else + { + StringBuilder url = new StringBuilder("https://"); + + url.append(request.getServerName()).append(":"); + url.append(configuration.getSslPort()).append(request.getRequestURI()); + response.sendRedirect(url.toString()); + } + } + + //~--- fields --------------------------------------------------------------- + + /** Field description */ + private ScmConfiguration configuration; +} diff --git a/scm-webapp/src/main/webapp/resources/js/sonia.config.js b/scm-webapp/src/main/webapp/resources/js/sonia.config.js index bcedd40277..8af3521ab4 100644 --- a/scm-webapp/src/main/webapp/resources/js/sonia.config.js +++ b/scm-webapp/src/main/webapp/resources/js/sonia.config.js @@ -104,6 +104,16 @@ Sonia.config.ScmConfigPanel = Ext.extend(Sonia.config.ConfigPanel,{ name: 'plugin-url', vtype: 'url', allowBlank: false + },{ + xtype: 'checkbox', + fieldLabel: 'Enable SSL', + name: 'enableSSL', + inputValue: 'true' + },{ + xtype: 'textfield', + fieldLabel: 'SSL Port', + name: 'sslPort', + vtype: 'alphanum' }], onSubmit: function(values){