forked from Stirling-Tools/Stirling-PDF
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
382 additions
and
314 deletions.
There are no files selected for viewing
104 changes: 52 additions & 52 deletions
104
src/main/java/stirling/software/SPDF/config/AppConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,53 @@ | ||
package stirling.software.SPDF.config; | ||
|
||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@Configuration | ||
public class AppConfig { | ||
|
||
|
||
@Bean(name = "loginEnabled") | ||
public boolean loginEnabled() { | ||
String appName = System.getProperty("login.enabled"); | ||
if (appName == null) | ||
appName = System.getenv("login.enabled"); | ||
|
||
return (appName != null) ? Boolean.valueOf(appName) : false; | ||
} | ||
|
||
@Bean(name = "appName") | ||
public String appName() { | ||
String appName = System.getProperty("APP_HOME_NAME"); | ||
if (appName == null) | ||
appName = System.getenv("APP_HOME_NAME"); | ||
return (appName != null) ? appName : "Stirling PDF"; | ||
} | ||
|
||
@Bean(name = "appVersion") | ||
public String appVersion() { | ||
String version = getClass().getPackage().getImplementationVersion(); | ||
return (version != null) ? version : "0.0.0"; | ||
} | ||
|
||
@Bean(name = "homeText") | ||
public String homeText() { | ||
String homeText = System.getProperty("APP_HOME_DESCRIPTION"); | ||
if (homeText == null) | ||
homeText = System.getenv("APP_HOME_DESCRIPTION"); | ||
return (homeText != null) ? homeText : "null"; | ||
} | ||
|
||
@Bean(name = "navBarText") | ||
public String navBarText() { | ||
String navBarText = System.getProperty("APP_NAVBAR_NAME"); | ||
if (navBarText == null) | ||
navBarText = System.getenv("APP_NAVBAR_NAME"); | ||
if (navBarText == null) | ||
navBarText = System.getProperty("APP_HOME_NAME"); | ||
if (navBarText == null) | ||
navBarText = System.getenv("APP_HOME_NAME"); | ||
|
||
return (navBarText != null) ? navBarText : "Stirling PDF"; | ||
} | ||
package stirling.software.SPDF.config; | ||
|
||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@Configuration | ||
public class AppConfig { | ||
|
||
|
||
@Bean(name = "loginEnabled") | ||
public boolean loginEnabled() { | ||
String appName = System.getProperty("login.enabled"); | ||
if (appName == null) | ||
appName = System.getenv("login.enabled"); | ||
|
||
return (appName != null) ? Boolean.valueOf(appName) : false; | ||
} | ||
|
||
@Bean(name = "appName") | ||
public String appName() { | ||
String appName = System.getProperty("APP_HOME_NAME"); | ||
if (appName == null) | ||
appName = System.getenv("APP_HOME_NAME"); | ||
return (appName != null) ? appName : "Stirling PDF"; | ||
} | ||
|
||
@Bean(name = "appVersion") | ||
public String appVersion() { | ||
String version = getClass().getPackage().getImplementationVersion(); | ||
return (version != null) ? version : "0.0.0"; | ||
} | ||
|
||
@Bean(name = "homeText") | ||
public String homeText() { | ||
String homeText = System.getProperty("APP_HOME_DESCRIPTION"); | ||
if (homeText == null) | ||
homeText = System.getenv("APP_HOME_DESCRIPTION"); | ||
return (homeText != null) ? homeText : "null"; | ||
} | ||
|
||
@Bean(name = "navBarText") | ||
public String navBarText() { | ||
String navBarText = System.getProperty("APP_NAVBAR_NAME"); | ||
if (navBarText == null) | ||
navBarText = System.getenv("APP_NAVBAR_NAME"); | ||
if (navBarText == null) | ||
navBarText = System.getProperty("APP_HOME_NAME"); | ||
if (navBarText == null) | ||
navBarText = System.getenv("APP_HOME_NAME"); | ||
|
||
return (navBarText != null) ? navBarText : "Stirling PDF"; | ||
} | ||
} |
166 changes: 83 additions & 83 deletions
166
src/main/java/stirling/software/SPDF/config/CleanUrlInterceptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,83 +1,83 @@ | ||
package stirling.software.SPDF.config; | ||
|
||
import java.util.List; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
import org.springframework.web.servlet.HandlerInterceptor; | ||
import org.springframework.web.servlet.ModelAndView; | ||
|
||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
|
||
import org.springframework.web.servlet.HandlerInterceptor; | ||
import org.springframework.web.servlet.ModelAndView; | ||
|
||
public class CleanUrlInterceptor implements HandlerInterceptor { | ||
|
||
private static final List<String> ALLOWED_PARAMS = Arrays.asList("lang", "endpoint", "endpoints", "logout", "error"); | ||
|
||
|
||
@Override | ||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) | ||
throws Exception { | ||
String queryString = request.getQueryString(); | ||
if (queryString != null && !queryString.isEmpty()) { | ||
String requestURI = request.getRequestURI(); | ||
|
||
Map<String, String> parameters = new HashMap<>(); | ||
|
||
// Keep only the allowed parameters | ||
String[] queryParameters = queryString.split("&"); | ||
for (String param : queryParameters) { | ||
String[] keyValue = param.split("="); | ||
System.out.print("astirli " + keyValue[0]); | ||
if (keyValue.length != 2) { | ||
continue; | ||
} | ||
System.out.print("astirli2 " + keyValue[0]); | ||
|
||
if (ALLOWED_PARAMS.contains(keyValue[0])) { | ||
parameters.put(keyValue[0], keyValue[1]); | ||
} | ||
} | ||
|
||
// If there are any parameters that are not allowed | ||
if (parameters.size() != queryParameters.length) { | ||
// Construct new query string | ||
StringBuilder newQueryString = new StringBuilder(); | ||
for (Map.Entry<String, String> entry : parameters.entrySet()) { | ||
if (newQueryString.length() > 0) { | ||
newQueryString.append("&"); | ||
} | ||
newQueryString.append(entry.getKey()).append("=").append(entry.getValue()); | ||
} | ||
|
||
// Redirect to the URL with only allowed query parameters | ||
String redirectUrl = requestURI + "?" + newQueryString; | ||
response.sendRedirect(redirectUrl); | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
@Override | ||
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, | ||
ModelAndView modelAndView) { | ||
} | ||
|
||
@Override | ||
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, | ||
Exception ex) { | ||
} | ||
} | ||
package stirling.software.SPDF.config; | ||
|
||
import java.util.List; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
import org.springframework.web.servlet.HandlerInterceptor; | ||
import org.springframework.web.servlet.ModelAndView; | ||
|
||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
|
||
import org.springframework.web.servlet.HandlerInterceptor; | ||
import org.springframework.web.servlet.ModelAndView; | ||
|
||
public class CleanUrlInterceptor implements HandlerInterceptor { | ||
|
||
private static final List<String> ALLOWED_PARAMS = Arrays.asList("lang", "endpoint", "endpoints", "logout", "error"); | ||
|
||
|
||
@Override | ||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) | ||
throws Exception { | ||
String queryString = request.getQueryString(); | ||
if (queryString != null && !queryString.isEmpty()) { | ||
String requestURI = request.getRequestURI(); | ||
|
||
Map<String, String> parameters = new HashMap<>(); | ||
|
||
// Keep only the allowed parameters | ||
String[] queryParameters = queryString.split("&"); | ||
for (String param : queryParameters) { | ||
String[] keyValue = param.split("="); | ||
System.out.print("astirli " + keyValue[0]); | ||
if (keyValue.length != 2) { | ||
continue; | ||
} | ||
System.out.print("astirli2 " + keyValue[0]); | ||
|
||
if (ALLOWED_PARAMS.contains(keyValue[0])) { | ||
parameters.put(keyValue[0], keyValue[1]); | ||
} | ||
} | ||
|
||
// If there are any parameters that are not allowed | ||
if (parameters.size() != queryParameters.length) { | ||
// Construct new query string | ||
StringBuilder newQueryString = new StringBuilder(); | ||
for (Map.Entry<String, String> entry : parameters.entrySet()) { | ||
if (newQueryString.length() > 0) { | ||
newQueryString.append("&"); | ||
} | ||
newQueryString.append(entry.getKey()).append("=").append(entry.getValue()); | ||
} | ||
|
||
// Redirect to the URL with only allowed query parameters | ||
String redirectUrl = requestURI + "?" + newQueryString; | ||
response.sendRedirect(redirectUrl); | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
@Override | ||
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, | ||
ModelAndView modelAndView) { | ||
} | ||
|
||
@Override | ||
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, | ||
Exception ex) { | ||
} | ||
} |
50 changes: 25 additions & 25 deletions
50
src/main/java/stirling/software/SPDF/config/EndpointInterceptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,26 @@ | ||
package stirling.software.SPDF.config; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.servlet.HandlerInterceptor; | ||
|
||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
|
||
@Component | ||
public class EndpointInterceptor implements HandlerInterceptor { | ||
|
||
@Autowired | ||
private EndpointConfiguration endpointConfiguration; | ||
|
||
@Override | ||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) | ||
throws Exception { | ||
String requestURI = request.getRequestURI(); | ||
if (!endpointConfiguration.isEndpointEnabled(requestURI)) { | ||
response.sendError(HttpServletResponse.SC_FORBIDDEN, "This endpoint is disabled"); | ||
return false; | ||
} | ||
return true; | ||
} | ||
package stirling.software.SPDF.config; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.servlet.HandlerInterceptor; | ||
|
||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
|
||
@Component | ||
public class EndpointInterceptor implements HandlerInterceptor { | ||
|
||
@Autowired | ||
private EndpointConfiguration endpointConfiguration; | ||
|
||
@Override | ||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) | ||
throws Exception { | ||
String requestURI = request.getRequestURI(); | ||
if (!endpointConfiguration.isEndpointEnabled(requestURI)) { | ||
response.sendError(HttpServletResponse.SC_FORBIDDEN, "This endpoint is disabled"); | ||
return false; | ||
} | ||
return true; | ||
} | ||
} |
46 changes: 23 additions & 23 deletions
46
src/main/java/stirling/software/SPDF/config/MetricsConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,24 @@ | ||
package stirling.software.SPDF.config; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
import io.micrometer.core.instrument.Meter; | ||
import io.micrometer.core.instrument.config.MeterFilter; | ||
import io.micrometer.core.instrument.config.MeterFilterReply; | ||
|
||
@Configuration | ||
public class MetricsConfig { | ||
|
||
@Bean | ||
public MeterFilter meterFilter() { | ||
return new MeterFilter() { | ||
@Override | ||
public MeterFilterReply accept(Meter.Id id) { | ||
if (id.getName().equals("http.requests")) { | ||
return MeterFilterReply.NEUTRAL; | ||
} | ||
return MeterFilterReply.DENY; | ||
} | ||
}; | ||
} | ||
package stirling.software.SPDF.config; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
import io.micrometer.core.instrument.Meter; | ||
import io.micrometer.core.instrument.config.MeterFilter; | ||
import io.micrometer.core.instrument.config.MeterFilterReply; | ||
|
||
@Configuration | ||
public class MetricsConfig { | ||
|
||
@Bean | ||
public MeterFilter meterFilter() { | ||
return new MeterFilter() { | ||
@Override | ||
public MeterFilterReply accept(Meter.Id id) { | ||
if (id.getName().equals("http.requests")) { | ||
return MeterFilterReply.NEUTRAL; | ||
} | ||
return MeterFilterReply.DENY; | ||
} | ||
}; | ||
} | ||
} |
Oops, something went wrong.