首頁  >  問答  >  主體

java - 关于Rest风格的API的如何做权限控制

想要实现的效果是

比如如下两个接口
GET /order/{orderId}

POST /order/{orderId}/abc/{abcId}

想通过不同的角色或用户来分别限制他们能访问接口的某一个,即拥有权限的一个

现在的问题就是,通过什么样的方式能够将URL和上面的接口路径分别匹配上呢?
使用的是SpringMVC。

注:上面写的接口URL只是简单的,还有复杂的里面参数可以是正则表达式,或者两个参数通过特定字符串拼接的(如{param1}-{param2},所以匹配路径不能用正则来做,这块不太了解SpringMVC的底层是如何实现的,求大神解答。

PHPzPHPz2719 天前724

全部回覆(6)我來回復

  • 巴扎黑

    巴扎黑2017-04-17 17:10:50

    怎麼感覺你的問題內容和標題不是一個意思。你到底是想問權限控制還是路徑識別匹配?

    回覆
    0
  • PHP中文网

    PHP中文网2017-04-17 17:10:50

    你必須要用實作 WebSecurityConfigurerAdapter
    以我所知Spring security基礎的登錄是User跟Role.

    每一個URL都可以透過實作 WebSecurityConfigurerAdapter的 configure(WebSecurity web)來控制的。

    例如以下的例子帳號在內存,登錄後各資源根制可以用上hasRole()限制:

            
    @EnableWebSecurity
    @Configuration
    public class CustomWebSecurityConfigurerAdapter extends
       WebSecurityConfigurerAdapter {
      @Autowired
      public void configureGlobal(AuthenticationManagerBuilder auth) {
        auth
          .inMemoryAuthentication()
            .withUser("user")  // #1
              .password("password")
              .roles("USER")
              .and()
            .withUser("admin") // #2
              .password("password")
              .roles("ADMIN","USER");
      }
    
      @Override
      public void configure(WebSecurity web) throws Exception {
        web
          .ignoring()
             .antMatchers("/resources/**"); // #3
      }
    
      @Override
      protected void configure(HttpSecurity http) throws Exception {
        http
          .authorizeUrls()
            .antMatchers("/signup","/about").permitAll() // #4
            .antMatchers("/admin/**").hasRole("ADMIN") // #6
            .anyRequest().authenticated() // 7
            .and()
        .formLogin()  // #8
            .loginUrl("/login") // #9
            .permitAll(); // #5
      }
    }
    
    

    參考: 官方文件

    回覆
    0
  • ringa_lee

    ringa_lee2017-04-17 17:10:50

    樓主可以去學習一下shiro框架, 具體可以看​​這裡, 非常不錯的教程, 入門也簡單, 這個框架可以解決你的問題.http://jinnianshilongnian.ite...
    配合spring mvc使用起來就像是這樣

    @RestController
    @RequestMapping("material")
    public class MaterialController extends BaseController {
        @Autowired
        private MaterialService materialService;
    
        @RequestMapping(value = "{moduleId}/material", method = RequestMethod.GET)//限制了只接受get请求
        public Map queryMaterial(@PathVariable long moduleId) throws Exception {
            return resultMap(true, materialService.queryMaterial(moduleId));
        }
    
        @RequiresRoles("admin")//限制访问这个方法必须具备admin角色, 同样有RequiresPermission等其他权限注解
                               //可以根据不同的需求配置, 也可以通过其他方法实现动态权限控制
        @RequestMapping(value = "{moduleId}/preview", method = RequestMethod.GET)
        public Map preview(@PathVariable long moduleId) throws Exception {
            return resultMap(true, materialService.queryMaterialForPreview(moduleId));
        }
    }

    回覆
    0
  • PHP中文网

    PHP中文网2017-04-17 17:10:50

    可以自己寫一個方法

    回覆
    0
  • 迷茫

    迷茫2017-04-17 17:10:50

    你用laravel不就好了

    回覆
    0
  • 巴扎黑

    巴扎黑2017-04-17 17:10:50

    http base 認證! ! ! !

    回覆
    0
  • 取消回覆