I try to understand how the RequestMatcher, AntMatcher and so on are working. I read some posts and understand the basics. Actually I have this simple basic config:
@Override
protected void configure(HttpSecurity http) throws Exception {
http.requestMatchers() //1
.antMatchers("/login", "/oauth/authorize") //2
.and() //3
.authorizeRequests() //4
.anyRequest() //5
.authenticated() //6;
I really dont understand the points 1,2 and 3. From my understanding this means requests of /login
and /oauth/authorize
are mapped and should be authorized requests. All other requests needs a authenticatoin.
Means for an endpoint /user/me
I have to be authenticated because its ruled by point 5 and 6?
The call to this endpoint is working for me.
In my ohter config I try a different approach:
@Override
protected void configure(HttpSecurity http) throws Exception { // @formatter:off
http
.authorizeRequests() //1
.antMatchers("/login", "/oauth/authorize", "/img/**").permitAll() //2
.anyRequest() //3
.authenticated() //4
From my point of view, this should be the same logic than the first config. But actually the endpoint /user/me
is not accessable any more.
I would really appreciated for a clarification
Update 1:
This is my config now:
@Override
protected void configure(HttpSecurity http) throws Exception { // @formatter:off
http
.requestMatchers()
.antMatchers("/", "/login", "/oauth/authorize",
"/main", "/logout-success", "/single-logout",
"/password_forgotten", "/enter_new_password", "/img/**",
"/logout", "/access_denied")
.and().authorizeRequests()
.antMatchers("/img/**", "/logout-success", "/password_forgotten",
"/enter_new_password", "/access_denied").permitAll()
.requestMatchers(SecurityUtils::isFrameworkInternalRequest).permitAll()
.and()
.authorizeRequests()
.anyRequest()
.authenticated()
.and()
.formLogin()
.loginPage("/login")
.failureUrl("/login?error")
.defaultSuccessUrl("/main")
.permitAll()
.and()
.logout()
.logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
.logoutSuccessUrl("/logout-success")
.deleteCookies("JSESSIONID")
.invalidateHttpSession(true)
.and()
.exceptionHandling()
.accessDeniedPage("/access_denied")
.authenticationEntryPoint(new LoginUrlAuthenticationEntryPoint("/login"))
.and().csrf().disable();
and if I enter URL \user\me
as a not authenticated user I get a 401 and this message:
<oauth>
<error_description>
Vollständige Authentifikation wird benötigt um auf diese Resource zuzugreifen
</error_description>
<error>unauthorized</error>
</oauth>
Which is ok, but means any ohter SecurityFilterChain takes place for this URL, right?