Skip to content

Commit

Permalink
added IP to public host URL redirecting filter
Browse files Browse the repository at this point in the history
  • Loading branch information
albogdano committed Jan 9, 2024
1 parent abd54cd commit 2e1c264
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/main/java/com/erudika/scoold/ScooldServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -216,6 +220,21 @@ public ViewResolver viewResolver() {
return viewr;
}

/**
* @return redirecting filter
*/
@Bean
public FilterRegistrationBean<RedirectingFilter> redirectingFilterRegistrationBean() {
FilterRegistrationBean<RedirectingFilter> 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
*/
Expand Down
53 changes: 53 additions & 0 deletions src/main/java/com/erudika/scoold/utils/RedirectingFilter.java
Original file line number Diff line number Diff line change
@@ -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 [[email protected]]
*/
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);
}
}

}

0 comments on commit 2e1c264

Please sign in to comment.