Есть ли способ разрешить получение и запрос роли для другого, не написав так много строк кода? или мне нужно указать каждый метод HTTP индивидуально, как я сделал ниже?
Код: Выделить всё
authorizeConfig.requestMatchers(HttpMethod.GET,"/product").permitAll();
authorizeConfig.requestMatchers(HttpMethod.POST,"/product").hasAnyRole("admin", "user");
authorizeConfig.requestMatchers(HttpMethod.DELETE,"/product").hasAnyRole("admin", "user");
Код: Выделить всё
@Bean
SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
return http
.authorizeHttpRequests(
authorizeConfig -> {
authorizeConfig.requestMatchers("/").permitAll();
authorizeConfig.requestMatchers(HttpMethod.GET,"/product").permitAll();
authorizeConfig.requestMatchers(HttpMethod.POST,"/product").hasAnyRole("admin", "user");
authorizeConfig.requestMatchers("/protected").hasRole("admin");
authorizeConfig.requestMatchers("/userinfo").hasRole("user");
authorizeConfig.anyRequest().authenticated();
})
.oauth2Login(Customizer.withDefaults())
.oauth2ResourceServer(config -> {
config.jwt(Customizer.withDefaults());
})
.cors(cors -> cors.configurationSource(corsConfigurationSource()))
.build();
}
Подробнее здесь: https://stackoverflow.com/questions/786 ... restrict-o
Мобильная версия