Skip to content

Commit 34b4b10

Browse files
evgeniychebanjzheaux
authored andcommitted
Add AuthorizationManager
Closes gh-8900
1 parent 5306d4c commit 34b4b10

File tree

15 files changed

+2052
-1
lines changed

15 files changed

+2052
-1
lines changed

config/src/main/java/org/springframework/security/config/annotation/web/builders/FilterComparator.java

+2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
import org.springframework.security.web.access.ExceptionTranslationFilter;
2727
import org.springframework.security.web.access.channel.ChannelProcessingFilter;
28+
import org.springframework.security.web.access.intercept.AuthorizationFilter;
2829
import org.springframework.security.web.access.intercept.FilterSecurityInterceptor;
2930
import org.springframework.security.web.authentication.AnonymousAuthenticationFilter;
3031
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
@@ -111,6 +112,7 @@ final class FilterComparator implements Comparator<Filter>, Serializable {
111112
put(SessionManagementFilter.class, order.next());
112113
put(ExceptionTranslationFilter.class, order.next());
113114
put(FilterSecurityInterceptor.class, order.next());
115+
put(AuthorizationFilter.class, order.next());
114116
put(SwitchUserFilter.class, order.next());
115117
}
116118

config/src/main/java/org/springframework/security/config/annotation/web/builders/HttpSecurity.java

+87-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -40,6 +40,8 @@
4040
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfiguration;
4141
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
4242
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;
4345
import org.springframework.security.config.annotation.web.configurers.ChannelSecurityConfigurer;
4446
import org.springframework.security.config.annotation.web.configurers.CorsConfigurer;
4547
import org.springframework.security.config.annotation.web.configurers.CsrfConfigurer;
@@ -1254,6 +1256,90 @@ public HttpSecurity authorizeRequests(
12541256
return HttpSecurity.this;
12551257
}
12561258

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+
* &#064;Configuration
1271+
* &#064;EnableWebSecurity
1272+
* public class AuthorizeUrlsSecurityConfig extends WebSecurityConfigurerAdapter {
1273+
*
1274+
* &#064;Override
1275+
* protected void configure(HttpSecurity http) throws Exception {
1276+
* http
1277+
* .authorizeHttpRequests((authorizeHttpRequests) ->
1278+
* authorizeHttpRequests
1279+
* .antMatchers(&quot;/**&quot;).hasRole(&quot;USER&quot;)
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+
* &#064;Configuration
1292+
* &#064;EnableWebSecurity
1293+
* public class AuthorizeUrlsSecurityConfig extends WebSecurityConfigurerAdapter {
1294+
*
1295+
* &#064;Override
1296+
* protected void configure(HttpSecurity http) throws Exception {
1297+
* http
1298+
* .authorizeHttpRequests((authorizeHttpRequests) ->
1299+
* authorizeHttpRequests
1300+
* .antMatchers(&quot;/admin/**&quot;).hasRole(&quot;ADMIN&quot;)
1301+
* .antMatchers(&quot;/**&quot;).hasRole(&quot;USER&quot;)
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+
* &#064;Configuration
1314+
* &#064;EnableWebSecurity
1315+
* public class AuthorizeUrlsSecurityConfig extends WebSecurityConfigurerAdapter {
1316+
*
1317+
* &#064;Override
1318+
* protected void configure(HttpSecurity http) throws Exception {
1319+
* http
1320+
* .authorizeHttpRequests((authorizeHttpRequests) ->
1321+
* authorizeHttpRequests
1322+
* .antMatchers(&quot;/**&quot;).hasRole(&quot;USER&quot;)
1323+
* .antMatchers(&quot;/admin/**&quot;).hasRole(&quot;ADMIN&quot;)
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+
12571343
/**
12581344
* Allows configuring the Request Cache. For example, a protected page (/protected)
12591345
* may be requested prior to authentication. The application will redirect the user to

0 commit comments

Comments
 (0)