【问题标题】:How to fix role in Spring Security?如何修复 Spring Security 中的角色?
【发布时间】:2017-08-20 12:50:07
【问题描述】:

我正在尝试在我的项目中使用 Spring Security,代码如下:

@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
    // TODO Auto-generated method stub
    //super.configure(auth);
    //auth.inMemoryAuthentication().withUser("admin").password("1111").roles("USER");
    auth
        .jdbcAuthentication()
            .dataSource(dataSource)
            .usersByUsernameQuery("select username, password, 1 from users where username=?")
            .authoritiesByUsernameQuery("select users_username, roles_id  from roles_users where users_username=?")
            .rolePrefix("ROLE_");
}   

@Override
protected void configure(HttpSecurity http) throws Exception {
    http
        .csrf().disable();      
    http
        .httpBasic();
    http
        .authorizeRequests()
            .anyRequest().authenticated();
    http
        .authorizeRequests()
            .antMatchers("/users/all").hasRole("admin")
            .and()
        .formLogin();
    http
        .exceptionHandling().accessDeniedPage("/403");
}

问题来了:

假设我们的数据库中有两个用户(一个具有user 角色,另一个具有admin 角色)一个管理员,第二个是用户,问题是当我以用户身份连接时(有只有user 角色)它可以访问管理资源(这不是预期的行为)。

我认为这个查询的问题:

"select username, password, 1 from users where username=?" 

根据username是主键?

如果有人知道我该如何解决这个问题?

【问题讨论】:

    标签: java spring spring-security


    【解决方案1】:

    始终应用您的第一个匹配器anyRequest(),因为匹配器的顺序很重要,请参阅HttpSecurity#authorizeRequests

    请注意,匹配器是按顺序考虑的。因此,以下是无效的,因为第一个匹配器匹配每个请求并且永远不会到达第二个映射:

    http.authorizeRequests().antMatchers("/**").hasRole("USER").antMatchers("/admin/**")
                .hasRole("ADMIN")
    

    您修改和简化的配置:

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .csrf().disable()      
            .httpBasic()
                .and()
            .authorizeRequests()
                .antMatchers("/users/all").hasRole("admin")
                .anyRequest().authenticated()
                .and()
            .formLogin()
                .and()
            .exceptionHandling().accessDeniedPage("/403");
    }
    

    【讨论】:

      【解决方案2】:

      问题在于配置HttpSecurity 时的规则顺序。发生的事情是当请求进入并点击

      authorizeRequests().anyRequest().authenticated() 
      

      并且由于用户已通过身份验证,因此它永远不会进入

      .antMatchers("/users/all").hasRole("admin")
      

      这是一个如何配置它的示例:

      @Override
      protected void configure(HttpSecurity http) throws Exception {
          http
              .csrf().disable()      
              .httpBasic()
              .and()
          .authorizeRequests()
              .antMatchers("/public").permitAll()
              .antMatchers("/user").hasRole("USER")
              .antMatchers("/admin").hasRole("ADMIN")
              .anyRequest().authenticated()
              .and()
          .formLogin()
              .and()
          .exceptionHandling().accessDeniedPage("/403");
      }
      

      它使用责任链模式。它将遍历规则链,直到找到匹配的规则。永远不会达到匹配规则之后的任何规则。一般在为认证请求编写规则时,先写更具体的规则。

      【讨论】:

        猜你喜欢
        • 2018-07-14
        • 1970-01-01
        • 1970-01-01
        • 2011-07-14
        • 1970-01-01
        • 1970-01-01
        • 2014-12-26
        相关资源
        最近更新 更多