如何在Spring HttpSecurity中检查ROLE_ADMIN中的静态服务?

时间:2018-12-19 21:18:42

标签: spring-security

我想用OAuth 2.0和Spring Security保护REST API。我正在使用Spring Boot 2。

这是我开始使用的配置:

@Override
public void configure(HttpSecurity http) throws Exception {
    http
        .authorizeRequests()
            .antMatchers(AUTH_WHITELIST).permitAll()
            .antMatchers("/**").authenticated()
            .antMatchers("/v1/**").hasAnyRole("ADMIN0", "123");
}

当前,我可以使用ROLE_USER访问/ v1 / **。如何限制ROLE_ADMIN和ROLE_123对/ v1 / **的访问,而对其他任何角色都没有限制?

1 个答案:

答案 0 :(得分:1)

可能是由于and()之间没有antMatchers

@Override
public void configure(HttpSecurity http) throws Exception {
    http
            // ... here goes your custom security configuration
            .authorizeRequests()
            .antMatchers(AUTH_WHITELIST).permitAll()  // whitelist Swagger UI resources
            // ... here goes your custom security configuration
            .and()
            .antMatchers("/**").authenticated()  // require authentication for any endpoint that's not whitelisted
            .and()
            .antMatchers("/v1/**")
                .hasAnyRole("ADMIN0", "123");
}
相关问题