我已经按如下方式设置了 Spring Security:
@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);
}
在我的 Controller 上有以下内容:
@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"})
加上 ROLE_
前缀
关于java - 禁止使用 Spring Security 和 @Secured,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35389195/