У меня есть настройки Spring Security следующим образом:Запретный с Spring Security и @Secured
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(securedEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private MongoUserDetailsService userServiceDetails;
@Autowired
private BCryptPasswordEncoder bCryptEncoder;
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers("/js/**", "/css/**", "/fonts/**");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.anyRequest().authenticated()
.and()
.csrf().disable()
.formLogin()
.defaultSuccessUrl("/index", true)
.loginPage("/login")
.permitAll()
.and()
.httpBasic()
.and()
.logout()
.permitAll()
.deleteCookies("JSESSIONID")
.invalidateHttpSession(true);
}
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth
.userDetailsService(userServiceDetails)
.passwordEncoder(bCryptEncoder);
}
И на мой контроллер я следующее:
@RequestMapping(method = RequestMethod.GET)
@Secured({"ADMIN"})
public List<Item> getItems(@RequestBody filter filter) {
if (filter.hasMissingField()) {
return new ArrayList<>();
}
return service.getItems(filter);
}
На лесозаготовок в деталях пользователя объекта имеет роли необходимы (в отладке):
Однако я m получает 403 - Запрещено. Я не понимаю, почему. Если я удалю @Secured, я могу получить доступ к этой странице в порядке, но с @Secured ({«ADMIN»}) он терпит неудачу.
Я прочел SO и вижу ошибки в отношении @Secured not working at all, ошибки в отношении @Secured having no effects at the Controller level, но не так, как в моем текущем сценарии, где он не может разрешить с необходимой ролью.
Если это помогает, я использую Spring Boot 1.3.2.
Любая помощь будет оценена по достоинству. Благодаря
Вы должны сказать '@Secured ({ "ROLE_ADMIN"})' вместо этого? – gerrytan
Спасибо @gerrytan, который сработал. Я не знал, что нужно было назначать роли. Спасибо, что вы спасли мне кучу;) – user1609848