added SSLFilter

This commit is contained in:
Sebastian Sdorra
2010-12-29 14:32:55 +01:00
parent 6136c94172
commit 6c3ef84958
4 changed files with 177 additions and 1 deletions

View File

@@ -44,6 +44,8 @@ import sonia.scm.api.rest.UriExtensionsConfig;
import sonia.scm.cache.CacheManager; import sonia.scm.cache.CacheManager;
import sonia.scm.cache.EhCacheManager; import sonia.scm.cache.EhCacheManager;
import sonia.scm.config.ScmConfiguration; import sonia.scm.config.ScmConfiguration;
import sonia.scm.filter.GZipFilter;
import sonia.scm.filter.SSLFilter;
import sonia.scm.filter.SecurityFilter; import sonia.scm.filter.SecurityFilter;
import sonia.scm.plugin.DefaultPluginManager; import sonia.scm.plugin.DefaultPluginManager;
import sonia.scm.plugin.Plugin; import sonia.scm.plugin.Plugin;
@@ -83,7 +85,6 @@ import java.util.Map;
import java.util.Set; import java.util.Set;
import javax.xml.bind.JAXB; import javax.xml.bind.JAXB;
import sonia.scm.filter.GZipFilter;
/** /**
* *
@@ -92,6 +93,9 @@ import sonia.scm.filter.GZipFilter;
public class ScmServletModule extends ServletModule public class ScmServletModule extends ServletModule
{ {
/** Field description */
public static final String PATTERN_ALL = "/*";
/** Field description */ /** Field description */
public static final String PATTERN_DEBUG = "/debug.html"; public static final String PATTERN_DEBUG = "/debug.html";
@@ -184,6 +188,7 @@ public class ScmServletModule extends ServletModule
* filter(PATTERN_PAGE, * filter(PATTERN_PAGE,
* PATTERN_STATIC_RESOURCES).through(StaticResourceFilter.class); * PATTERN_STATIC_RESOURCES).through(StaticResourceFilter.class);
*/ */
filter(PATTERN_ALL).through(SSLFilter.class);
filter(PATTERN_PAGE, PATTERN_COMPRESSABLE).through(GZipFilter.class); filter(PATTERN_PAGE, PATTERN_COMPRESSABLE).through(GZipFilter.class);
filter(PATTERN_RESTAPI, PATTERN_DEBUG).through(SecurityFilter.class); filter(PATTERN_RESTAPI, PATTERN_DEBUG).through(SecurityFilter.class);

View File

@@ -76,6 +76,8 @@ public class ScmConfiguration
{ {
this.servername = other.servername; this.servername = other.servername;
this.pluginUrl = other.pluginUrl; this.pluginUrl = other.pluginUrl;
this.sslPort = other.sslPort;
this.enableSSL = other.enableSSL;
} }
//~--- get methods ---------------------------------------------------------- //~--- get methods ----------------------------------------------------------
@@ -102,8 +104,41 @@ public class ScmConfiguration
return servername; return servername;
} }
/**
* Method description
*
*
* @return
*/
public int getSslPort()
{
return sslPort;
}
/**
* Method description
*
*
* @return
*/
public boolean isEnableSSL()
{
return enableSSL;
}
//~--- set methods ---------------------------------------------------------- //~--- set methods ----------------------------------------------------------
/**
* Method description
*
*
* @param enableSSL
*/
public void setEnableSSL(boolean enableSSL)
{
this.enableSSL = enableSSL;
}
/** /**
* Method description * Method description
* *
@@ -126,6 +161,17 @@ public class ScmConfiguration
this.servername = servername; this.servername = servername;
} }
/**
* Method description
*
*
* @param sslPort
*/
public void setSslPort(int sslPort)
{
this.sslPort = sslPort;
}
//~--- fields --------------------------------------------------------------- //~--- fields ---------------------------------------------------------------
/** Field description */ /** Field description */
@@ -134,4 +180,10 @@ public class ScmConfiguration
/** Field description */ /** Field description */
private String servername = "localhost"; private String servername = "localhost";
/** Field description */
private boolean enableSSL = false;
/** Field description */
private int sslPort = 8181;
} }

View File

@@ -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;
}

View File

@@ -104,6 +104,16 @@ Sonia.config.ScmConfigPanel = Ext.extend(Sonia.config.ConfigPanel,{
name: 'plugin-url', name: 'plugin-url',
vtype: 'url', vtype: 'url',
allowBlank: false allowBlank: false
},{
xtype: 'checkbox',
fieldLabel: 'Enable SSL',
name: 'enableSSL',
inputValue: 'true'
},{
xtype: 'textfield',
fieldLabel: 'SSL Port',
name: 'sslPort',
vtype: 'alphanum'
}], }],
onSubmit: function(values){ onSubmit: function(values){