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
Copy file name to clipboardExpand all lines: config/src/main/java/org/springframework/security/config/annotation/web/configuration/HttpSecurityConfiguration.java
+10-1
Original file line number
Diff line number
Diff line change
@@ -1,5 +1,5 @@
1
1
/*
2
-
* Copyright 2002-2022 the original author or authors.
2
+
* Copyright 2002-2023 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
Copy file name to clipboardExpand all lines: config/src/test/java/org/springframework/security/config/annotation/web/configuration/HttpSecurityConfigurationTests.java
+30-1
Original file line number
Diff line number
Diff line change
@@ -1,5 +1,5 @@
1
1
/*
2
-
* Copyright 2002-2022 the original author or authors.
2
+
* Copyright 2002-2023 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
Copy file name to clipboardExpand all lines: docs/modules/ROOT/pages/servlet/integrations/cors.adoc
+91-45
Original file line number
Diff line number
Diff line change
@@ -6,65 +6,38 @@ CORS must be processed before Spring Security, because the pre-flight request do
6
6
If the request does not contain any cookies and Spring Security is first, the request determines that the user is not authenticated (since there are no cookies in the request) and rejects it.
7
7
8
8
The easiest way to ensure that CORS is handled first is to use the `CorsFilter`.
9
-
Users can integrate the `CorsFilter` with Spring Security by providing a `CorsConfigurationSource` that uses the following:
9
+
Users can integrate the `CorsFilter` with Spring Security by providing a `CorsConfigurationSource`.
10
+
For example, the following will integrate CORS support within Spring Security:
10
11
11
12
[tabs]
12
13
======
13
14
Java::
14
15
+
15
16
[source,java,role="primary"]
16
17
----
17
-
@Configuration
18
-
@EnableWebSecurity
19
-
public class WebSecurityConfig {
20
-
21
-
@Bean
22
-
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
23
-
http
24
-
// by default uses a Bean by the name of corsConfigurationSource
@@ -137,3 +110,76 @@ The following listing does the same thing in XML:
137
110
...
138
111
</http>
139
112
----
113
+
114
+
If you have more than one `CorsConfigurationSource` bean, Spring Security won't automatically configure CORS support for you, that is because it cannot decide which one to use.
115
+
If you want to specify different `CorsConfigurationSource` for each `SecurityFilterChain`, you can pass it directly into the `.cors()` DSL.
116
+
117
+
[tabs]
118
+
======
119
+
Java::
120
+
+
121
+
[source,java,role="primary"]
122
+
----
123
+
@Configuration
124
+
@EnableWebSecurity
125
+
public class WebSecurityConfig {
126
+
127
+
@Bean
128
+
@Order(0)
129
+
public SecurityFilterChain apiFilterChain(HttpSecurity http) throws Exception {
130
+
http
131
+
.securityMatcher("/api/**")
132
+
.cors((cors) -> cors
133
+
.configurationSource(apiConfigurationSource())
134
+
)
135
+
...
136
+
return http.build();
137
+
}
138
+
139
+
@Bean
140
+
@Order(1)
141
+
public SecurityFilterChain myOtherFilterChain(HttpSecurity http) throws Exception {
0 commit comments