0

I am trying to apply URl and role based authentication in the following way

http
    .authorizeRequests()
        .antMatchers("/rest/**").hasRole("ADMIN")
        .and()
    .authorizeRequests()
        .antMatchers("/admin/**").hasRole("MANAGER")
        .and()
    .authorizeRequests()
        .antMatchers("/restApi/**").hasRole("USER")
        .anyRequest().authenticated()
        .and()
    .formLogin()
        .permitAll();

But after entering username and password, I am getting back default login screen provided by Spring Boot.

If I use permitAll() instead of hasRole(), then it works correctly.
Where am I wrong?

dur
  • 15,689
  • 25
  • 79
  • 125
tushar
  • 313
  • 4
  • 10

1 Answers1

1

dur answers a good way to use multiple rest endpoints here: https://stackoverflow.com/a/41527591/2566098

I tested this example with a fresh project and had to add "{noop}" in front of the password string to get it to work but it works great.

Basically we separate each endpoint into its own extension of WebSecurityConfigurerAdapter.

In this example it is:

http
    .antMatcher("/api/**")
    .authorizeRequests()
    .anyRequest().hasRole("ADMIN")
    .and().httpBasic();

While you have it reversed and use antmatchers plural and are missing anyRequest() (not sure if this makes a difference):

http
    .authorizeRequests()
    .antMatchers("/rest/**").hasRole("ADMIN")
Lukos
  • 712
  • 4
  • 5