java - 禁止使用 Spring Security 和 @Secured

标签 java spring spring-security spring-boot

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

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

enter image description here

但是,我收到 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/

相关文章:

java - Spring 执行器 : Error configuring CloudFoundryActuator with multiple custom RestTemplateBuilder beans

spring-security - org.springframework.security.core.userdetails.User 不能转换为 MyUser

grails spring security 自定义 userDetailsS​​ervice

java - Autowiring 的 bean 为空

java - 每 N 毫秒播放一次声音

java - 套接字获取 HTML 响应不起作用

Java 的 for 循环问题

java - 在Spring Boot中使用hibernate-types-52时如何禁用Hypersistence横幅?

java - android: 在 textview 中显示 .txt 文件时出错

基于 Java 的基于 Ehcache 的缓存配置不起作用