【问题标题】:Forbidden with Spring Security and @Secured使用 Spring Security 和 @Secured 禁止
【发布时间】:2016-05-25 04:08:22
【问题描述】:

我已按如下方式设置 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);
}

在我的控制器上,我有以下内容:

@RequestMapping(method = RequestMethod.GET)
@Secured({"ADMIN"})
public List<Item> getItems(@RequestBody filter filter) {

    if (filter.hasMissingField()) {
        return new ArrayList<>();
    }

    return service.getItems(filter);
}

在登录用户详细信息对象时具有所需的角色(在调试中):

但是,我收到了 403 - Forbidden。我不明白为什么。如果我删除了@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 工作。我不知道必须为角色添加前缀。谢谢你救了我一大堆;)

标签: java spring spring-security spring-boot


【解决方案1】:

你必须把@Secured({"ROLE_ADMIN"})加上ROLE_前缀

【讨论】:

  • 谢谢.. 由于声誉问题无法投票给您,但您的 cmets 确实解决了问题。谢谢。
猜你喜欢
  • 2016-08-11
  • 2013-02-03
  • 2011-01-23
  • 2012-01-12
  • 2020-12-18
  • 1970-01-01
  • 2015-08-16
  • 2021-09-27
  • 2018-12-24
相关资源
最近更新 更多