我已经按照以下步骤设置了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);
}


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

java - Spring Security和@Secured禁止使用-LMLPHP

但是,我收到403-禁止。我不明白为什么。如果删除@Secured,则可以正常访问页面,但是使用@Secured({“ ADMIN”}),它将失败。

我进行了梳理,发现与@Secured not working at all有关的错误,与@Secured having no effects at the Controller level有关的错误,但是不像我当前的情况(在该情况下,该角色无法通过所需的当前角色进行授权)。

如果有帮助,我正在使用Spring Boot 1.3.2。

任何帮助将不胜感激。谢谢

最佳答案

您必须在@Secured({"ROLE_ADMIN"})前面加上ROLE_前缀

10-08 18:54