|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2019 the original author or authors. |
| 2 | + * Copyright 2002-2020 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.
|
|
40 | 40 | import org.springframework.security.config.annotation.web.configuration.WebSecurityConfiguration;
|
41 | 41 | import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
|
42 | 42 | import org.springframework.security.config.annotation.web.configurers.AnonymousConfigurer;
|
| 43 | +import org.springframework.security.config.annotation.web.configurers.AuthorizeHttpRequestsConfigurer; |
| 44 | +import org.springframework.security.config.annotation.web.configurers.AuthorizeHttpRequestsConfigurer.AuthorizationManagerRequestMatcherRegistry; |
43 | 45 | import org.springframework.security.config.annotation.web.configurers.ChannelSecurityConfigurer;
|
44 | 46 | import org.springframework.security.config.annotation.web.configurers.CorsConfigurer;
|
45 | 47 | import org.springframework.security.config.annotation.web.configurers.CsrfConfigurer;
|
@@ -1254,6 +1256,90 @@ public HttpSecurity authorizeRequests(
|
1254 | 1256 | return HttpSecurity.this;
|
1255 | 1257 | }
|
1256 | 1258 |
|
| 1259 | + /** |
| 1260 | + * Allows restricting access based upon the {@link HttpServletRequest} using |
| 1261 | + * {@link RequestMatcher} implementations (i.e. via URL patterns). |
| 1262 | + * |
| 1263 | + * <h2>Example Configurations</h2> |
| 1264 | + * |
| 1265 | + * The most basic example is to configure all URLs to require the role "ROLE_USER". |
| 1266 | + * The configuration below requires authentication to every URL and will grant access |
| 1267 | + * to both the user "admin" and "user". |
| 1268 | + * |
| 1269 | + * <pre> |
| 1270 | + * @Configuration |
| 1271 | + * @EnableWebSecurity |
| 1272 | + * public class AuthorizeUrlsSecurityConfig extends WebSecurityConfigurerAdapter { |
| 1273 | + * |
| 1274 | + * @Override |
| 1275 | + * protected void configure(HttpSecurity http) throws Exception { |
| 1276 | + * http |
| 1277 | + * .authorizeHttpRequests((authorizeHttpRequests) -> |
| 1278 | + * authorizeHttpRequests |
| 1279 | + * .antMatchers("/**").hasRole("USER") |
| 1280 | + * ) |
| 1281 | + * .formLogin(withDefaults()); |
| 1282 | + * } |
| 1283 | + * } |
| 1284 | + * </pre> |
| 1285 | + * |
| 1286 | + * We can also configure multiple URLs. The configuration below requires |
| 1287 | + * authentication to every URL and will grant access to URLs starting with /admin/ to |
| 1288 | + * only the "admin" user. All other URLs either user can access. |
| 1289 | + * |
| 1290 | + * <pre> |
| 1291 | + * @Configuration |
| 1292 | + * @EnableWebSecurity |
| 1293 | + * public class AuthorizeUrlsSecurityConfig extends WebSecurityConfigurerAdapter { |
| 1294 | + * |
| 1295 | + * @Override |
| 1296 | + * protected void configure(HttpSecurity http) throws Exception { |
| 1297 | + * http |
| 1298 | + * .authorizeHttpRequests((authorizeHttpRequests) -> |
| 1299 | + * authorizeHttpRequests |
| 1300 | + * .antMatchers("/admin/**").hasRole("ADMIN") |
| 1301 | + * .antMatchers("/**").hasRole("USER") |
| 1302 | + * ) |
| 1303 | + * .formLogin(withDefaults()); |
| 1304 | + * } |
| 1305 | + * } |
| 1306 | + * </pre> |
| 1307 | + * |
| 1308 | + * Note that the matchers are considered in order. Therefore, the following is invalid |
| 1309 | + * because the first matcher matches every request and will never get to the second |
| 1310 | + * mapping: |
| 1311 | + * |
| 1312 | + * <pre> |
| 1313 | + * @Configuration |
| 1314 | + * @EnableWebSecurity |
| 1315 | + * public class AuthorizeUrlsSecurityConfig extends WebSecurityConfigurerAdapter { |
| 1316 | + * |
| 1317 | + * @Override |
| 1318 | + * protected void configure(HttpSecurity http) throws Exception { |
| 1319 | + * http |
| 1320 | + * .authorizeHttpRequests((authorizeHttpRequests) -> |
| 1321 | + * authorizeHttpRequests |
| 1322 | + * .antMatchers("/**").hasRole("USER") |
| 1323 | + * .antMatchers("/admin/**").hasRole("ADMIN") |
| 1324 | + * ); |
| 1325 | + * } |
| 1326 | + * } |
| 1327 | + * </pre> |
| 1328 | + * @param authorizeHttpRequestsCustomizer the {@link Customizer} to provide more |
| 1329 | + * options for the {@link AuthorizationManagerRequestMatcherRegistry} |
| 1330 | + * @return the {@link HttpSecurity} for further customizations |
| 1331 | + * @throws Exception |
| 1332 | + * @see #requestMatcher(RequestMatcher) |
| 1333 | + */ |
| 1334 | + public HttpSecurity authorizeHttpRequests( |
| 1335 | + Customizer<AuthorizeHttpRequestsConfigurer<HttpSecurity>.AuthorizationManagerRequestMatcherRegistry> authorizeHttpRequestsCustomizer) |
| 1336 | + throws Exception { |
| 1337 | + ApplicationContext context = getContext(); |
| 1338 | + authorizeHttpRequestsCustomizer |
| 1339 | + .customize(getOrApply(new AuthorizeHttpRequestsConfigurer<>(context)).getRegistry()); |
| 1340 | + return HttpSecurity.this; |
| 1341 | + } |
| 1342 | + |
1257 | 1343 | /**
|
1258 | 1344 | * Allows configuring the Request Cache. For example, a protected page (/protected)
|
1259 | 1345 | * may be requested prior to authentication. The application will redirect the user to
|
|
0 commit comments