diff --git a/src/main/java/com/erudika/scoold/ScooldServer.java b/src/main/java/com/erudika/scoold/ScooldServer.java index 093ed210..160a70c5 100644 --- a/src/main/java/com/erudika/scoold/ScooldServer.java +++ b/src/main/java/com/erudika/scoold/ScooldServer.java @@ -23,6 +23,7 @@ import com.erudika.para.core.utils.Config; import com.erudika.para.core.utils.Para; import com.erudika.para.core.utils.Utils; +import com.erudika.scoold.utils.RedirectingFilter; import com.erudika.scoold.utils.ScooldEmailer; import com.erudika.scoold.utils.ScooldRequestInterceptor; import com.erudika.scoold.utils.ScooldUtils; @@ -33,9 +34,11 @@ import java.nio.file.Files; import java.nio.file.Paths; import java.util.Arrays; +import java.util.EnumSet; import java.util.Map; import java.util.Properties; import javax.inject.Named; +import javax.servlet.DispatcherType; import org.apache.commons.lang3.StringUtils; import org.apache.velocity.runtime.RuntimeConstants; import org.slf4j.Logger; @@ -46,6 +49,7 @@ import org.springframework.boot.web.server.ErrorPage; import org.springframework.boot.web.server.ErrorPageRegistrar; import org.springframework.boot.web.server.ErrorPageRegistry; +import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; import org.springframework.context.annotation.Bean; import org.springframework.core.Ordered; @@ -216,6 +220,21 @@ public ViewResolver viewResolver() { return viewr; } + /** + * @return redirecting filter + */ + @Bean + public FilterRegistrationBean redirectingFilterRegistrationBean() { + FilterRegistrationBean frb = new FilterRegistrationBean<>(new RedirectingFilter()); + frb.setDispatcherTypes(EnumSet.of(DispatcherType.REQUEST)); + frb.setName("redirectingFilter"); + frb.setAsyncSupported(true); + frb.setMatchAfter(false); + frb.setEnabled(true); + frb.setOrder(Ordered.HIGHEST_PRECEDENCE); + return frb; + } + /** * @return Error page registry bean */ diff --git a/src/main/java/com/erudika/scoold/utils/RedirectingFilter.java b/src/main/java/com/erudika/scoold/utils/RedirectingFilter.java new file mode 100644 index 00000000..7fe92b80 --- /dev/null +++ b/src/main/java/com/erudika/scoold/utils/RedirectingFilter.java @@ -0,0 +1,53 @@ +/* + * Copyright 2013-2024 Erudika. https://erudika.com + * + * Licensed under the EULA - use is subject to license terms. + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * For issues and patches go to: https://github.com/erudika/scoold-pro + */ +package com.erudika.scoold.utils; + +import java.io.IOException; +import javax.servlet.Filter; +import javax.servlet.FilterChain; +import javax.servlet.ServletException; +import javax.servlet.ServletRequest; +import javax.servlet.ServletResponse; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import javax.ws.rs.core.HttpHeaders; +import org.apache.commons.lang3.StringUtils; + +/** + * + * @author Alex Bogdanovski [alex@erudika.com] + */ +public class RedirectingFilter implements Filter { + + @Override + public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { + HttpServletRequest httpRequest = (HttpServletRequest) req; + HttpServletResponse httpResponse = (HttpServletResponse) res; + String url = httpRequest.getRequestURL().toString(); + url = StringUtils.removeStartIgnoreCase(url, "http://"); + url = StringUtils.removeStartIgnoreCase(url, "https://"); + + if (url.matches("(?:[0-9]{1,3}\\.){3}[0-9]{1,3}(.*)") && ScooldUtils.getConfig().inProduction()) { + String path = StringUtils.substringAfter(url, "/"); + String query = httpRequest.getQueryString(); + httpResponse.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY); + httpResponse.setHeader(HttpHeaders.LOCATION, ScooldUtils.getConfig().serverUrl() + + (StringUtils.isBlank(path) ? "" : "/" + path) + + (StringUtils.isBlank(query) ? "" : "?" + query)); + } else { + chain.doFilter(req, res); + } + } + +}