You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS[Cross-Origin Resource Sharing (CORS)] is a mechanism that uses additional HTTP headers to tell a
4
+
browser to let a web application running at one origin (domain) have permission to access selected
5
+
resources from a server at a different origin. A web application executes a cross-origin HTTP
6
+
request when it requests a resource that has a different origin (domain, protocol, or port) than
7
+
its own origin.
8
+
9
+
Jooby supports CORS out of the box. By default, **CORS requests will be rejected**.
10
+
To enable processing of CORS requests, use the javadoc:CorsHandler[]:
11
+
12
+
.CorsExample
13
+
[source, java, role = "primary"]
14
+
----
15
+
import io.jooby.Jooby;
16
+
import io.jooby.CorsHandler;
17
+
...
18
+
{
19
+
20
+
decorator(new CorsHandler()); <1>
21
+
22
+
path("/api", () -> {
23
+
// API methods
24
+
});
25
+
}
26
+
----
27
+
28
+
.Kotlin
29
+
[source, kotlin, role = "secondary"]
30
+
----
31
+
import io.jooby.Jooby
32
+
import io.jooby.CorsHandler
33
+
...
34
+
{
35
+
decorator(CorsHandler()) <1>
36
+
37
+
path("/api") {
38
+
// API methods
39
+
}
40
+
}
41
+
----
42
+
43
+
<1> Install CorsHandler with defaults options
44
+
45
+
Default options are:
46
+
47
+
- origin: `*`
48
+
- credentials: `true`
49
+
- allowed methods: `GET`, `POST`
50
+
- allowed headers: `X-Requested-With`, `Content-Type`, `Accept` and `Origin`
51
+
- max age: `30m`;
52
+
53
+
To customize default options use javadoc:Cors[]:
54
+
55
+
.Cors options
56
+
[source, java, role = "primary"]
57
+
----
58
+
import io.jooby.Jooby;
59
+
import io.jooby.CorsHandler;
60
+
...
61
+
{
62
+
Cors cors = new Cors()
63
+
.setMethods("GET", "POST", "PUT"); <1>
64
+
65
+
decorator(new CorsHandler(cors)); <2>
66
+
67
+
path("/api", () -> {
68
+
// API methods
69
+
});
70
+
}
71
+
----
72
+
73
+
.Kotlin
74
+
[source, kotlin, role = "secondary"]
75
+
----
76
+
import io.jooby.Jooby
77
+
import io.jooby.CorsHandler
78
+
import io.jooby.cors
79
+
...
80
+
{
81
+
val cors = cors {
82
+
methods = listOf("GET", "POST", "PUT") <1>
83
+
}
84
+
decorator(CorsHandler(cors)) <2>
85
+
86
+
path("/api") {
87
+
// API methods
88
+
}
89
+
}
90
+
----
91
+
92
+
<1> Specify allowed methods
93
+
<2> Pass cors options to cors handler
94
+
95
+
Optionally cors options can be specified in the application configuration file:
96
+
97
+
.application.conf
98
+
[source,json]
99
+
----
100
+
cors {
101
+
origin: "*"
102
+
credentials: true
103
+
methods: [GET, POST],
104
+
headers: [Content-Type],
105
+
maxAge: 30m
106
+
exposedHeaders: [Custom-Header]
107
+
}
108
+
----
109
+
110
+
.Loading options
111
+
[source, java, role = "primary"]
112
+
----
113
+
import io.jooby.Jooby;
114
+
import io.jooby.CorsHandler;
115
+
...
116
+
{
117
+
Cors cors = Cors.from(getConfig()); <1>
118
+
119
+
decorator(new CorsHandler(cors));
120
+
121
+
path("/api", () -> {
122
+
// API methods
123
+
});
124
+
}
125
+
----
126
+
127
+
.Kotlin
128
+
[source, kotlin, role = "secondary"]
129
+
----
130
+
import io.jooby.Jooby
131
+
import io.jooby.CorsHandler
132
+
...
133
+
{
134
+
val cors = Cors.from(config) <1>
135
+
decorator(CorsHandler(cors))
136
+
137
+
path("/api") {
138
+
// API methods
139
+
}
140
+
}
141
+
----
142
+
143
+
<1> Load cors options from application configuration file
0 commit comments