【问题标题】:Role based access control for REST APIsREST API 的基于角色的访问控制
【发布时间】:2020-02-06 17:12:01
【问题描述】:

如何对 REST API 进行角色验证?

我有两个角色,分别称为 adminmanager。如何使用 RBAC(基于角色的访问控制)限制 REST API 的访问?比如/usersPOST可以被admin角色访问,/usersGET可以被manager角色访问。

【问题讨论】:

    标签: spring-boot api-gateway


    【解决方案1】:

    您可以使用 Spring Security 来实现它。

    Spring 安全

    一个高度可定制的框架,Spring Security 被广泛用于处理任何基于 Java 开发的基于企业的应用程序中出现的身份验证和访问控制(授权)问题。

    例如:

    @Configuration
    @EnableWebSecurity
    @EnableGlobalMethodSecurity(prePostEnabled = true)
    public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    
        
    
        @Override
        @Bean
        public AuthenticationManager authenticationManagerBean() throws Exception {
            return super.authenticationManagerBean();
        }
    
        
        @Override
        protected void configure(HttpSecurity http) throws Exception {
    
            http.csrf().disable().authorizeRequests().antMatchers("/user/login").permitAll().antMatchers(HttpMethod.OPTIONS)
    
                    .permitAll()
                    .antMatchers(HttpMethod.GET, "/user").hasRole("ADMIN")
                    .antMatchers(HttpMethod.GET, "/user/list").hasAnyRole("MANAGER", "ADMIN")
                    .authenticated();       
    
        }   
    }
    

    【讨论】:

      猜你喜欢
      • 2010-09-11
      • 2013-08-28
      • 2013-05-08
      • 1970-01-01
      • 2012-06-25
      • 1970-01-01
      • 1970-01-01
      • 2015-12-09
      • 1970-01-01
      相关资源
      最近更新 更多