@ -104,8 +104,10 @@ If we wanted to restrict access to this controller method to admin users, a deve
@@ -104,8 +104,10 @@ If we wanted to restrict access to this controller method to admin users, a deve
@ -133,8 +135,10 @@ The following configuration will protect the same URLs that Spring MVC will matc
@@ -133,8 +135,10 @@ The following configuration will protect the same URLs that Spring MVC will matc
@ -16,15 +16,25 @@ public class OAuth2LoginSecurityConfig extends WebSecurityConfigurerAdapter {
@@ -16,15 +16,25 @@ public class OAuth2LoginSecurityConfig extends WebSecurityConfigurerAdapter {
@ -58,27 +68,34 @@ public class OAuth2LoginSecurityConfig extends WebSecurityConfigurerAdapter {
@@ -58,27 +68,34 @@ public class OAuth2LoginSecurityConfig extends WebSecurityConfigurerAdapter {
@ -123,12 +140,16 @@ public class OAuth2LoginSecurityConfig extends WebSecurityConfigurerAdapter {
@@ -123,12 +140,16 @@ public class OAuth2LoginSecurityConfig extends WebSecurityConfigurerAdapter {
@ -171,10 +192,14 @@ public class OAuth2LoginSecurityConfig extends WebSecurityConfigurerAdapter {
@@ -171,10 +192,14 @@ public class OAuth2LoginSecurityConfig extends WebSecurityConfigurerAdapter {
@ -234,10 +259,14 @@ public class OAuth2LoginSecurityConfig extends WebSecurityConfigurerAdapter {
@@ -234,10 +259,14 @@ public class OAuth2LoginSecurityConfig extends WebSecurityConfigurerAdapter {
@ -280,7 +309,8 @@ public class OAuth2LoginSecurityConfig extends WebSecurityConfigurerAdapter {
@@ -280,7 +309,8 @@ public class OAuth2LoginSecurityConfig extends WebSecurityConfigurerAdapter {
@ -308,10 +338,14 @@ public class OAuth2LoginSecurityConfig extends WebSecurityConfigurerAdapter {
@@ -308,10 +338,14 @@ public class OAuth2LoginSecurityConfig extends WebSecurityConfigurerAdapter {
@ -355,10 +389,14 @@ public class OAuth2LoginSecurityConfig extends WebSecurityConfigurerAdapter {
@@ -355,10 +389,14 @@ public class OAuth2LoginSecurityConfig extends WebSecurityConfigurerAdapter {
@ -469,10 +507,14 @@ public class OAuth2LoginSecurityConfig extends WebSecurityConfigurerAdapter {
@@ -469,10 +507,14 @@ public class OAuth2LoginSecurityConfig extends WebSecurityConfigurerAdapter {
@ -501,10 +543,14 @@ public class OAuth2LoginSecurityConfig extends WebSecurityConfigurerAdapter {
@@ -501,10 +543,14 @@ public class OAuth2LoginSecurityConfig extends WebSecurityConfigurerAdapter {
@ -137,12 +137,12 @@ How does Spring Security know that we want to require all users to be authentica
@@ -137,12 +137,12 @@ How does Spring Security know that we want to require all users to be authentica
@ -163,10 +163,6 @@ You will notice that this configuration is quite similar the XML Namespace confi
@@ -163,10 +163,6 @@ You will notice that this configuration is quite similar the XML Namespace confi
</http>
----
The Java Configuration equivalent of closing an XML tag is expressed using the `and()` method which allows us to continue configuring the parent.
If you read the code it also makes sense.
I want to configure authorized requests __and__ configure form login __and__ configure HTTP Basic authentication.
[[jc-form]]
== Java Configuration and Form Login
You might be wondering where the login form came from when you were prompted to log in, since we made no mention of any HTML files or JSPs.
@ -180,12 +176,15 @@ To do so we can update our configuration as seen below:
@@ -180,12 +176,15 @@ To do so we can update our configuration as seen below:
.antMatchers("/db/**").access("hasRole('ADMIN') and hasRole('DBA')") // <4>
.anyRequest().authenticated() // <5>
)
.formLogin(withDefaults());
}
----
@ -282,14 +281,15 @@ Similar to configuring login capabilities, however, you also have various option
@@ -282,14 +281,15 @@ Similar to configuring login capabilities, however, you also have various option
@ -510,11 +510,14 @@ The first is a `WebSecurityConfigurerAdapter` that configures the app as a resou
@@ -510,11 +510,14 @@ The first is a `WebSecurityConfigurerAdapter` that configures the app as a resou
```java
protected void configure(HttpSecurity http) {
http
.authorizeRequests()
.anyRequest().authenticated()
.and()
.oauth2ResourceServer()
.jwt();
.authorizeRequests(authorizeRequests ->
authorizeRequests
.anyRequest().authenticated()
)
.oauth2ResourceServer(oauth2ResourceServer ->
oauth2ResourceServer
.jwt(withDefaults())
);
}
```
@ -527,13 +530,18 @@ Replacing this is as simple as exposing the bean within the application:
@@ -527,13 +530,18 @@ Replacing this is as simple as exposing the bean within the application:
public class MyCustomSecurityConfiguration extends WebSecurityConfigurerAdapter {
@ -565,12 +573,17 @@ An authorization server's JWK Set Uri can be configured <<oauth2resourceserver-j
@@ -565,12 +573,17 @@ An authorization server's JWK Set Uri can be configured <<oauth2resourceserver-j
public class DirectlyConfiguredJwkSetUri extends WebSecurityConfigurerAdapter {
@ -587,12 +600,17 @@ More powerful than `jwkSetUri()` is `decoder()`, which will completely replace a
@@ -587,12 +600,17 @@ More powerful than `jwkSetUri()` is `decoder()`, which will completely replace a
public class DirectlyConfiguredJwkSetUri extends WebSecurityConfigurerAdapter {
protected void configure(HttpSecurity http) {
http
.authorizeRequests()
.anyRequest().authenticated()
.and()
.oauth2ResourceServer()
.jwt()
.decoder(myCustomDecoder());
.authorizeRequests(authorizeRequests ->
authorizeRequests
.anyRequest().authenticated()
)
.oauth2ResourceServer(oauth2ResourceServer ->
oauth2ResourceServer
.jwt(jwt ->
jwt
.decoder(myCustomDecoder())
)
);
}
}
```
@ -627,13 +645,16 @@ This means that to protect an endpoint or method with a scope derived from a JWT
@@ -627,13 +645,16 @@ This means that to protect an endpoint or method with a scope derived from a JWT
public class DirectlyConfiguredJwkSetUri extends WebSecurityConfigurerAdapter {
@ -659,12 +680,17 @@ To this end, the DSL exposes `jwtAuthenticationConverter()`:
@@ -659,12 +680,17 @@ To this end, the DSL exposes `jwtAuthenticationConverter()`:
public class DirectlyConfiguredJwkSetUri extends WebSecurityConfigurerAdapter {
@ -1221,15 +1249,17 @@ For example, if you wanted to configure the `filterSecurityPublishAuthorizationS
@@ -1221,15 +1249,17 @@ For example, if you wanted to configure the `filterSecurityPublishAuthorizationS
@ -20,14 +20,18 @@ public class OAuth2ClientSecurityConfig extends WebSecurityConfigurerAdapter {
@@ -20,14 +20,18 @@ public class OAuth2ClientSecurityConfig extends WebSecurityConfigurerAdapter {
@ -245,10 +249,14 @@ public class OAuth2ClientSecurityConfig extends WebSecurityConfigurerAdapter {
@@ -245,10 +249,14 @@ public class OAuth2ClientSecurityConfig extends WebSecurityConfigurerAdapter {
@ -285,14 +293,19 @@ public class OAuth2LoginSecurityConfig extends WebSecurityConfigurerAdapter {
@@ -285,14 +293,19 @@ public class OAuth2LoginSecurityConfig extends WebSecurityConfigurerAdapter {
@ -422,10 +435,14 @@ public class OAuth2ClientSecurityConfig extends WebSecurityConfigurerAdapter {
@@ -422,10 +435,14 @@ public class OAuth2ClientSecurityConfig extends WebSecurityConfigurerAdapter {
@ -285,10 +285,11 @@ public class OAuth2LoginSecurityConfig extends WebSecurityConfigurerAdapter {
@@ -285,10 +285,11 @@ public class OAuth2LoginSecurityConfig extends WebSecurityConfigurerAdapter {
@ -18,7 +18,7 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@@ -18,7 +18,7 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
// by default uses a Bean by the name of corsConfigurationSource
.cors().and()
.cors(withDefaults())
...
}
@ -59,7 +59,7 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@@ -59,7 +59,7 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
http
// if Spring MVC is on classpath and no CorsConfigurationSource is provided,
// Spring Security will use CORS configuration provided to Spring MVC
@ -92,15 +98,17 @@ If you are using Spring Security's Java Configuration the following will only ad
@@ -92,15 +98,17 @@ If you are using Spring Security's Java Configuration the following will only ad
// do not use any default headers unless explicitly listed
.defaultsDisabled()
.cacheControl(withDefaults())
);
}
}
----
@ -126,12 +134,14 @@ If necessary, you can disable all of the HTTP Security response headers with the
@@ -126,12 +134,14 @@ If necessary, you can disable all of the HTTP Security response headers with the
@ -182,14 +192,16 @@ Similarly, you can enable only cache control within Java Configuration with the
@@ -182,14 +192,16 @@ Similarly, you can enable only cache control within Java Configuration with the
@ -263,14 +275,16 @@ If you want more control over the headers, you can explicitly specify the conten
@@ -263,14 +275,16 @@ If you want more control over the headers, you can explicitly specify the conten
@ -327,16 +341,20 @@ Similarly, you can enable only HSTS headers with Java Configuration:
@@ -327,16 +341,20 @@ Similarly, you can enable only HSTS headers with Java Configuration:
@ -399,16 +417,20 @@ Similarly, you can enable HPKP headers with Java Configuration:
@@ -399,16 +417,20 @@ Similarly, you can enable HPKP headers with Java Configuration:
@ -461,14 +483,18 @@ Similarly, you can customize frame options to use the same origin within Java Co
@@ -461,14 +483,18 @@ Similarly, you can customize frame options to use the same origin within Java Co
@ -511,14 +537,18 @@ Similarly, you can customize XSS protection within Java Configuration with the f
@@ -511,14 +537,18 @@ Similarly, you can customize XSS protection within Java Configuration with the f
@ -625,13 +655,18 @@ Similarly, you can enable the CSP header using Java configuration as shown below
@@ -625,13 +655,18 @@ Similarly, you can enable the CSP header using Java configuration as shown below
@ -643,14 +678,19 @@ To enable the CSP _'report-only'_ header, provide the following Java configurati
@@ -643,14 +678,19 @@ To enable the CSP _'report-only'_ header, provide the following Java configurati
@ -707,13 +747,18 @@ Similarly, you can enable the Referrer Policy header using Java configuration as
@@ -707,13 +747,18 @@ Similarly, you can enable the Referrer Policy header using Java configuration as
@ -757,13 +802,15 @@ Similarly, you can enable the Feature Policy header using Java configuration as
@@ -757,13 +802,15 @@ Similarly, you can enable the Feature Policy header using Java configuration as
@ -804,13 +851,15 @@ Similarly, the headers could be added to the response using Java Configuration a
@@ -804,13 +851,15 @@ Similarly, the headers could be added to the response using Java Configuration a
@ -849,13 +898,15 @@ We could also restrict framing of content to the same origin with Java configura
@@ -849,13 +898,15 @@ We could also restrict framing of content to the same origin with Java configura
@ -903,17 +954,21 @@ We could also prevent framing of content to the log in page using java configura
@@ -903,17 +954,21 @@ We could also prevent framing of content to the log in page using java configura