故宫我已经安装的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);
}
在用户详细信息的记录对象具有角色需要(在调试):
但是,我一得到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