Невозможно получить доступ к контроллерам REST в Spring Securtiy, даже если роль соответствует - PullRequest
0 голосов
/ 30 декабря 2018

Класс WebSecurityConfig имеет следующий вид: -

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {



    @Override
    public void configure(HttpSecurity http) throws Exception {

        http.authorizeRequests()
                .antMatchers("/get*").hasAnyRole();


    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication()
                .withUser("user1").password("user1Pass")
                .authorities("USER");
                //.and().withUser("admin").password("adminPass")
                //.authorities("ADMIN");
    }


    /*@Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication().passwordEncoder(NoOpPasswordEncoder.getInstance())
                .withUser("test").password("test123").roles("USER").and().
                withUser("test1").password("test123").roles("ADMIN");
    }

}*/
    }

Ниже представлен контроллер REST: -

@RequestMapping(value="/getprofessors", method=RequestMethod.GET)
    public List<Professor> getProfessors() {

        //return service.findProfessors();

        List<Professor> Professors = professorRepo.findAll();

        return  Professors;

    }

Ниже изображение представляет вызов POSTMAN, который я выполняю: -

Postman call I'm trying to make

Ошибка получения 403 при доступе к этому контроллеру, даже если не указана роль.

1 Ответ

0 голосов
/ 30 декабря 2018

Проверяли ли вы другие методы для antMatchers ()?Я имею в виду allowAll () и другие.Возможно проблема с hasAnyRole (), потому что он принимает String [].Возможно, из-за этого возникла проблема.

...