Skip to content

Commit 268e13a

Browse files
committed
Support setting a specific allowed origin for CORS purposes
1 parent 4641414 commit 268e13a

File tree

3 files changed

+12
-5
lines changed

3 files changed

+12
-5
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ To use an older version of ElasticSearch please download the data from [here](ht
5656

5757
Check the URL `http://localhost:2322/api?q=berlin` to see if photon is running without problems. You may want to use our [leaflet plugin](https://github.com/komoot/leaflet.photon) to see the results on a map.
5858

59-
To enable CORS (cross-site requests), use `-cors`. By default, CORS is disabled.
59+
To enable CORS (cross-site requests), use `-cors-any` to allow any origin or `-cors-origin` with a specific origin as the argument. By default, CORS support is disabled.
6060

6161
discover more of photon's feature with its usage `java -jar photon-*.jar -h`.
6262

src/main/java/de/komoot/photon/App.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ public static void main(String[] rawArgs) throws Exception {
2727
final JCommander jCommander = new JCommander(args);
2828
try {
2929
jCommander.parse(rawArgs);
30+
if (args.isCorsAnyOrigin() && args.getCorsOrigin() != null) { // these are mutually exclusive
31+
throw new ParameterException("Use only one cors configuration type");
32+
}
3033
} catch (ParameterException e) {
3134
log.warn("could not start photon: " + e.getMessage());
3235
jCommander.usage();
@@ -138,8 +141,9 @@ private static void startApi(CommandLineArgs args, Client esNodeClient) {
138141
port(args.getListenPort());
139142
ipAddress(args.getListenIp());
140143

141-
if (args.isCors()) {
142-
CorsFilter.enableCORS("*", "get", "*");
144+
String allowedOrigin = args.isCorsAnyOrigin() ? "*" : args.getCorsOrigin();
145+
if (allowedOrigin != null) {
146+
CorsFilter.enableCORS(allowedOrigin, "get", "*");
143147
} else {
144148
before((request, response) -> {
145149
response.type("application/json"); // in the other case set by enableCors

src/main/java/de/komoot/photon/CommandLineArgs.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,11 @@ public class CommandLineArgs {
5656
@Parameter(names = "-listen-ip", description = "listen to address (default '0.0.0.0')")
5757
private String listenIp = "0.0.0.0";
5858

59-
@Parameter(names = "-cors", description = "enable cross-site resource sharing (default disabled)")
60-
private boolean cors = false;
59+
@Parameter(names = "-cors-any", description = "enable cross-site resource sharing foe any origin ((default CORS not supported)")
60+
private boolean corsAnyOrigin = false;
61+
62+
@Parameter(names = "-cors-origin", description = "enable cross-site resource sharing for the specified origin (default CORS not supported)")
63+
private String corsOrigin = null;
6164

6265
@Parameter(names = "-h", description = "show help / usage")
6366
private boolean usage = false;

0 commit comments

Comments
 (0)