【问题标题】:hasRole() and denyAll() method don't restrict access to resourceshasRole() 和 denyAll() 方法不限制对资源的访问
【发布时间】:2021-09-21 23:44:20
【问题描述】:

我正在开发启用授权和资源服务器的 Spring Boot 和 Spring Security Web 应用程序。我已经定义了一组分配了角色的用户,并尝试实现对 REST 端点的基于角色的访问。我能够实现对端点的基于令牌的访问,但不能限制对最终用户的访问,这将基于他们的角色。

我已经完成了两个端点:/rest/products/list/rest/products/add,并尝试使用 ADMIN 角色的用户限制对 /rest/products/add 端点的访问。

我的WebSecurityConfigurerAdapter如下:

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Override
    protected void configure(final AuthenticationManagerBuilder auth) throws Exception {
        auth
            .inMemoryAuthentication()
                .passwordEncoder(passwordEncoder)
                .withUser("user1")
                    .password(passwordEncoder.encode("user1Pass"))
                    .roles("USER")
                    .and()
                .withUser("user2")
                    .password(passwordEncoder.encode("user2Pass"))
                    .roles("USER")
                    .and()
                .withUser("admin")
                    .password(passwordEncoder.encode("adminPass"))
                    .roles("ADMIN");
    }

    @Override
    protected void configure(final HttpSecurity http) throws Exception {

        http
            .authorizeRequests()
                .antMatchers("/rest/products/add").hasAnyRole("ADMIN")
                .antMatchers("/rest/products/list").denyAll();
    }

    @Override
    @Bean
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }
}

因此,admin / adminPass 用户只能访问资源 /rest/products/add,前提是该用户具有 ADMIN 角色。但是如果尝试使用 user1 / user1Pass,它仍然可以访问:

获取 user1 邮递员屏幕的访问令牌

仅使用user1 Postman 屏幕访问ADMIN 相关端点

此外,我在配置方法中添加了(出于测试目的)以下规则.antMatchers("/products/list").denyAll(); Here 表示任何用户都不应访问/products/list。但它仍然继续响应(提供访问正确的令牌)。

在这里How to fix role in Spring Security? 的类似问题中,匹配器的顺序应该是从更具体到更少。但在我的例子中,有两个匹配器并且没有匹配器可以重叠它们。

我正在使用带有 spring-boot-starter-security 插件版本 2.5.2 的 Spring Boot。

应该进行哪些额外配置才能使.hasRole("ADMIN").denyAll() 按预期工作?

【问题讨论】:

    标签: spring-boot spring-security spring-security-oauth2


    【解决方案1】:

    终于找到解决办法了:

    Here 有一个ResourceServerConfigurerAdapter 类的例子。从这个和你的评论中,dur,我意识到我混淆了ResourceServerConfigurerAdapterWebSecurityConfigurerAdapter,试图在WebSecurityConfigurerAdapter 中定义访问限制匹配器。我通过以下方式更改了资源服务器配置:

    WebSecurityConfigurerAdapter 中的方法

     @Override
        public void configure(final HttpSecurity http) throws Exception {
    
            http.authorizeRequests()
                    .antMatchers("/rest/products/add").hasAnyRole("ADMIN")
                    .antMatchers("/rest/products/list").denyAll();
        }
    

    已移至

    @Configuration
    public class ResourceServerConfig extends ResourceServerConfigurerAdapter {
    
        @Override
        public void configure(final HttpSecurity http) throws Exception {
    
            http.authorizeRequests()
                    .antMatchers("/rest/products/add").hasAnyRole("ADMIN")
                    .antMatchers("/rest/products/list").denyAll();
        }
    
    }
    

    现在上述匹配器定义的限制按预期工作。

    【讨论】:

      猜你喜欢
      • 2013-08-28
      • 2011-05-03
      • 2011-10-14
      • 2018-01-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-13
      相关资源
      最近更新 更多