|
@@ -548,9 +548,10 @@ public final class HttpSecurity extends
|
|
* @Override
|
|
* @Override
|
|
* protected void configure(HttpSecurity http) throws Exception {
|
|
* protected void configure(HttpSecurity http) throws Exception {
|
|
* http
|
|
* http
|
|
- * .authorizeRequests()
|
|
|
|
- * .anyRequest().hasRole("USER")
|
|
|
|
- * .and()
|
|
|
|
|
|
+ * .authorizeRequests(authorizeRequests ->
|
|
|
|
+ * authorizeRequests
|
|
|
|
+ * .anyRequest().hasRole("USER")
|
|
|
|
+ * )
|
|
* .formLogin(formLogin ->
|
|
* .formLogin(formLogin ->
|
|
* formLogin
|
|
* formLogin
|
|
* .permitAll()
|
|
* .permitAll()
|
|
@@ -769,9 +770,10 @@ public final class HttpSecurity extends
|
|
* @Override
|
|
* @Override
|
|
* protected void configure(HttpSecurity http) throws Exception {
|
|
* protected void configure(HttpSecurity http) throws Exception {
|
|
* http
|
|
* http
|
|
- * .authorizeRequests()
|
|
|
|
- * .antMatchers("/**").hasRole("USER")
|
|
|
|
- * .and()
|
|
|
|
|
|
+ * .authorizeRequests(authorizeRequests ->
|
|
|
|
+ * authorizeRequests
|
|
|
|
+ * .antMatchers("/**").hasRole("USER")
|
|
|
|
+ * )
|
|
* .jee(jee ->
|
|
* .jee(jee ->
|
|
* jee
|
|
* jee
|
|
* .mappableRoles("USER", "ADMIN")
|
|
* .mappableRoles("USER", "ADMIN")
|
|
@@ -878,10 +880,10 @@ public final class HttpSecurity extends
|
|
* @Override
|
|
* @Override
|
|
* protected void configure(HttpSecurity http) throws Exception {
|
|
* protected void configure(HttpSecurity http) throws Exception {
|
|
* http
|
|
* http
|
|
- * .authorizeRequests()
|
|
|
|
- * .antMatchers("/**")
|
|
|
|
- * .hasRole("USER")
|
|
|
|
- * .and()
|
|
|
|
|
|
+ * .authorizeRequests(authorizeRequests ->
|
|
|
|
+ * authorizeRequests
|
|
|
|
+ * .antMatchers("/**").hasRole("USER")
|
|
|
|
+ * )
|
|
* .x509(withDefaults());
|
|
* .x509(withDefaults());
|
|
* }
|
|
* }
|
|
* }
|
|
* }
|
|
@@ -952,9 +954,10 @@ public final class HttpSecurity extends
|
|
* @Override
|
|
* @Override
|
|
* protected void configure(HttpSecurity http) throws Exception {
|
|
* protected void configure(HttpSecurity http) throws Exception {
|
|
* http
|
|
* http
|
|
- * .authorizeRequests()
|
|
|
|
- * .antMatchers("/**").hasRole("USER")
|
|
|
|
- * .and()
|
|
|
|
|
|
+ * .authorizeRequests(authorizeRequests ->
|
|
|
|
+ * authorizeRequests
|
|
|
|
+ * .antMatchers("/**").hasRole("USER")
|
|
|
|
+ * )
|
|
* .formLogin(withDefaults())
|
|
* .formLogin(withDefaults())
|
|
* .rememberMe(withDefaults());
|
|
* .rememberMe(withDefaults());
|
|
* }
|
|
* }
|
|
@@ -1042,6 +1045,91 @@ public final class HttpSecurity extends
|
|
.getRegistry();
|
|
.getRegistry();
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * Allows restricting access based upon the {@link HttpServletRequest} using
|
|
|
|
+ * {@link RequestMatcher} implementations (i.e. via URL patterns).
|
|
|
|
+ *
|
|
|
|
+ * <h2>Example Configurations</h2>
|
|
|
|
+ *
|
|
|
|
+ * The most basic example is to configure all URLs to require the role "ROLE_USER".
|
|
|
|
+ * The configuration below requires authentication to every URL and will grant access
|
|
|
|
+ * to both the user "admin" and "user".
|
|
|
|
+ *
|
|
|
|
+ * <pre>
|
|
|
|
+ * @Configuration
|
|
|
|
+ * @EnableWebSecurity
|
|
|
|
+ * public class AuthorizeUrlsSecurityConfig extends WebSecurityConfigurerAdapter {
|
|
|
|
+ *
|
|
|
|
+ * @Override
|
|
|
|
+ * protected void configure(HttpSecurity http) throws Exception {
|
|
|
|
+ * http
|
|
|
|
+ * .authorizeRequests(authorizeRequests ->
|
|
|
|
+ * authorizeRequests
|
|
|
|
+ * .antMatchers("/**").hasRole("USER")
|
|
|
|
+ * )
|
|
|
|
+ * .formLogin(withDefaults());
|
|
|
|
+ * }
|
|
|
|
+ * }
|
|
|
|
+ * </pre>
|
|
|
|
+ *
|
|
|
|
+ * We can also configure multiple URLs. The configuration below requires
|
|
|
|
+ * authentication to every URL and will grant access to URLs starting with /admin/ to
|
|
|
|
+ * only the "admin" user. All other URLs either user can access.
|
|
|
|
+ *
|
|
|
|
+ * <pre>
|
|
|
|
+ * @Configuration
|
|
|
|
+ * @EnableWebSecurity
|
|
|
|
+ * public class AuthorizeUrlsSecurityConfig extends WebSecurityConfigurerAdapter {
|
|
|
|
+ *
|
|
|
|
+ * @Override
|
|
|
|
+ * protected void configure(HttpSecurity http) throws Exception {
|
|
|
|
+ * http
|
|
|
|
+ * .authorizeRequests(authorizeRequests ->
|
|
|
|
+ * authorizeRequests
|
|
|
|
+ * .antMatchers("/admin/**").hasRole("ADMIN")
|
|
|
|
+ * .antMatchers("/**").hasRole("USER")
|
|
|
|
+ * )
|
|
|
|
+ * .formLogin(withDefaults());
|
|
|
|
+ * }
|
|
|
|
+ * }
|
|
|
|
+ * </pre>
|
|
|
|
+ *
|
|
|
|
+ * Note that the matchers are considered in order. Therefore, the following is invalid
|
|
|
|
+ * because the first matcher matches every request and will never get to the second
|
|
|
|
+ * mapping:
|
|
|
|
+ *
|
|
|
|
+ * <pre>
|
|
|
|
+ * @Configuration
|
|
|
|
+ * @EnableWebSecurity
|
|
|
|
+ * public class AuthorizeUrlsSecurityConfig extends WebSecurityConfigurerAdapter {
|
|
|
|
+ *
|
|
|
|
+ * @Override
|
|
|
|
+ * protected void configure(HttpSecurity http) throws Exception {
|
|
|
|
+ * http
|
|
|
|
+ * .authorizeRequests(authorizeRequests ->
|
|
|
|
+ * authorizeRequests
|
|
|
|
+ * .antMatchers("/**").hasRole("USER")
|
|
|
|
+ * .antMatchers("/admin/**").hasRole("ADMIN")
|
|
|
|
+ * );
|
|
|
|
+ * }
|
|
|
|
+ * }
|
|
|
|
+ * </pre>
|
|
|
|
+ *
|
|
|
|
+ * @see #requestMatcher(RequestMatcher)
|
|
|
|
+ *
|
|
|
|
+ * @param authorizeRequestsCustomizer the {@link Customizer} to provide more options for
|
|
|
|
+ * the {@link ExpressionUrlAuthorizationConfigurer.ExpressionInterceptUrlRegistry}
|
|
|
|
+ * @return the {@link HttpSecurity} for further customizations
|
|
|
|
+ * @throws Exception
|
|
|
|
+ */
|
|
|
|
+ public HttpSecurity authorizeRequests(Customizer<ExpressionUrlAuthorizationConfigurer<HttpSecurity>.ExpressionInterceptUrlRegistry> authorizeRequestsCustomizer)
|
|
|
|
+ throws Exception {
|
|
|
|
+ ApplicationContext context = getContext();
|
|
|
|
+ authorizeRequestsCustomizer.customize(getOrApply(new ExpressionUrlAuthorizationConfigurer<>(context))
|
|
|
|
+ .getRegistry());
|
|
|
|
+ return HttpSecurity.this;
|
|
|
|
+ }
|
|
|
|
+
|
|
/**
|
|
/**
|
|
* Allows configuring the Request Cache. For example, a protected page (/protected)
|
|
* Allows configuring the Request Cache. For example, a protected page (/protected)
|
|
* may be requested prior to authentication. The application will redirect the user to
|
|
* may be requested prior to authentication. The application will redirect the user to
|
|
@@ -1075,9 +1163,10 @@ public final class HttpSecurity extends
|
|
* @Override
|
|
* @Override
|
|
* protected void configure(HttpSecurity http) throws Exception {
|
|
* protected void configure(HttpSecurity http) throws Exception {
|
|
* http
|
|
* http
|
|
- * .authorizeRequests()
|
|
|
|
- * .antMatchers("/**").hasRole("USER")
|
|
|
|
- * .and()
|
|
|
|
|
|
+ * .authorizeRequests(authorizeRequests ->
|
|
|
|
+ * authorizeRequests
|
|
|
|
+ * .antMatchers("/**").hasRole("USER")
|
|
|
|
+ * )
|
|
* .requestCache(requestCache ->
|
|
* .requestCache(requestCache ->
|
|
* requestCache.disable()
|
|
* requestCache.disable()
|
|
* );
|
|
* );
|
|
@@ -1124,9 +1213,10 @@ public final class HttpSecurity extends
|
|
* @Override
|
|
* @Override
|
|
* protected void configure(HttpSecurity http) throws Exception {
|
|
* protected void configure(HttpSecurity http) throws Exception {
|
|
* http
|
|
* http
|
|
- * .authorizeRequests()
|
|
|
|
- * .antMatchers("/**").hasRole("USER")
|
|
|
|
- * .and()
|
|
|
|
|
|
+ * .authorizeRequests(authorizeRequests ->
|
|
|
|
+ * authorizeRequests
|
|
|
|
+ * .antMatchers("/**").hasRole("USER")
|
|
|
|
+ * )
|
|
* // sample exception handling customization
|
|
* // sample exception handling customization
|
|
* .exceptionHandling(exceptionHandling ->
|
|
* .exceptionHandling(exceptionHandling ->
|
|
* exceptionHandling
|
|
* exceptionHandling
|
|
@@ -1288,9 +1378,10 @@ public final class HttpSecurity extends
|
|
* @Override
|
|
* @Override
|
|
* protected void configure(HttpSecurity http) throws Exception {
|
|
* protected void configure(HttpSecurity http) throws Exception {
|
|
* http
|
|
* http
|
|
- * .authorizeRequests()
|
|
|
|
- * .antMatchers("/**").hasRole("USER")
|
|
|
|
- * .and()
|
|
|
|
|
|
+ * .authorizeRequests(authorizeRequests ->
|
|
|
|
+ * authorizeRequests
|
|
|
|
+ * .antMatchers("/**").hasRole("USER")
|
|
|
|
+ * )
|
|
* .formLogin(withDefaults())
|
|
* .formLogin(withDefaults())
|
|
* // sample logout customization
|
|
* // sample logout customization
|
|
* .logout(logout ->
|
|
* .logout(logout ->
|
|
@@ -1460,9 +1551,10 @@ public final class HttpSecurity extends
|
|
* @Override
|
|
* @Override
|
|
* protected void configure(HttpSecurity http) throws Exception {
|
|
* protected void configure(HttpSecurity http) throws Exception {
|
|
* http
|
|
* http
|
|
- * .authorizeRequests()
|
|
|
|
- * .antMatchers("/**").hasRole("USER")
|
|
|
|
- * .and()
|
|
|
|
|
|
+ * .authorizeRequests(authorizeRequests ->
|
|
|
|
+ * authorizeRequests
|
|
|
|
+ * .antMatchers("/**").hasRole("USER")
|
|
|
|
+ * )
|
|
* .formLogin(withDefaults());
|
|
* .formLogin(withDefaults());
|
|
* }
|
|
* }
|
|
* }
|
|
* }
|
|
@@ -1478,9 +1570,10 @@ public final class HttpSecurity extends
|
|
* @Override
|
|
* @Override
|
|
* protected void configure(HttpSecurity http) throws Exception {
|
|
* protected void configure(HttpSecurity http) throws Exception {
|
|
* http
|
|
* http
|
|
- * .authorizeRequests()
|
|
|
|
- * .antMatchers("/**").hasRole("USER")
|
|
|
|
- * .and()
|
|
|
|
|
|
+ * .authorizeRequests(authorizeRequests ->
|
|
|
|
+ * authorizeRequests
|
|
|
|
+ * .antMatchers("/**").hasRole("USER")
|
|
|
|
+ * )
|
|
* .formLogin(formLogin ->
|
|
* .formLogin(formLogin ->
|
|
* formLogin
|
|
* formLogin
|
|
* .usernameParameter("username")
|
|
* .usernameParameter("username")
|
|
@@ -1717,9 +1810,10 @@ public final class HttpSecurity extends
|
|
* @Override
|
|
* @Override
|
|
* protected void configure(HttpSecurity http) throws Exception {
|
|
* protected void configure(HttpSecurity http) throws Exception {
|
|
* http
|
|
* http
|
|
- * .authorizeRequests()
|
|
|
|
- * .antMatchers("/**").hasRole("USER")
|
|
|
|
- * .and()
|
|
|
|
|
|
+ * .authorizeRequests(authorizeRequests ->
|
|
|
|
+ * authorizeRequests
|
|
|
|
+ * .antMatchers("/**").hasRole("USER")
|
|
|
|
+ * )
|
|
* .httpBasic(withDefaults());
|
|
* .httpBasic(withDefaults());
|
|
* }
|
|
* }
|
|
* }
|
|
* }
|