There is a common practice in the Spring security oauth implementation to secure the oauth endpoints with the following line:
.requestMatchers().antMatchers("/login", "/oauth/authorize", "/oauth/confirm_access")
The entire setup looks like this:
http
.formLogin().loginPage("/login").permitAll()
.and()
.requestMatchers().antMatchers("/login", "/oauth/authorize", "/oauth/confirm_access")
.and()
.authorizeRequests().anyRequest().authenticated();
Can someone explain me why that specific line is needed, since the next line explicitly says that all the requests have to be authenticated?
Spring Security manages several Servlet filter's chains.
In modern Spring security (v3.2.x and above) each chain configured by
WebSecurityConfigurerAdapter
and applied based on@Order(...)
class annotations until first reported that it supportsHttpServletRequest
, which is configured via.requestMatchers()
DSL:Then selected chain applied to secure request.
Your setting only applied to 3 URLs leaving all other URLs unsecured if otherwise isn't configured.
The
requestMatchers
line specifies to which requests the security check applies. TheauthorizeRequests
line does the actual security check.If you leave out the
requestMatchers
line, all requests will get checked in the wayauthorizeRequests
specifies. If there are no checks for some requests, checking will succeed by default.With the
requestMatchers
line, requests that don't match will get checked by the other remaining chains.